容斥原理练习题,忘记处理gcd 和 lcm,wa了几发0.0.
#include#include #include using namespace std;typedef long long ll;ll Num[30];ll gcd(ll a,ll b){ return b == 0 ? a : gcd(b,a%b);}int main(){ ll N, M; while(scanf("%lld%lld",&N,&M)!=EOF) { N--; ll num = 0; ll tmp,Cnt = 0; for(int i = 0; i < M; ++i) { scanf("%lld",&tmp); if(tmp == 0) continue; else { Num[Cnt++] = tmp; } } for(int i = 1; i < (1 << Cnt) ; ++i) { ll cnt = 0, tmp = 1; for(int j = 0; j < Cnt; ++j) { if(i & (1 << j)) { cnt++; tmp =tmp /gcd(tmp,Num[j]) * Num[j]; } //printf("%lld\n",tmp); } if(cnt & 1) num += (N/tmp); else num -= (N/tmp); } printf("%lld\n",num); } return 0;}