1. 程式人生 > >HDU 1796 (容斥原理)

HDU 1796 (容斥原理)

容斥原理練習題,忘記處理gcd 和 lcm,wa了幾發0.0.
#include<iostream>
#include<cstdio>
#include<cstring>
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; }