标签:
Description
Input
Output
Sample Input
Sample Output
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <algorithm> 5 using namespace std; 6 typedef long long LL; 7 int num[30], n, m, a[30]; 8 LL res; 9 LL gcd(LL a, LL b) 10 { 11 if (a == 0) 12 return b; 13 return gcd(b % a, a); 14 } 15 void dfs(int cur, int snum, int cnt) 16 { 17 if (snum == cnt) 18 { 19 int temp = n; 20 int mult = 1; 21 for (int i = 0; i < snum; i++) 22 mult = mult / gcd(mult, a[i]) * a[i]; // 防爆 23 if (mult == 0) 24 return; 25 if (temp % mult == 0) 26 res += temp / mult - 1; 27 else 28 res += temp / mult; 29 return; 30 } 31 for (int i = cur; i < m; i++) 32 { 33 a[snum] = num[i]; 34 dfs(i + 1, snum + 1, cnt); 35 } 36 } 37 int main() 38 { 39 int tm; 40 while (scanf("%d%d", &n, &tm) != EOF) 41 { 42 m = 0; 43 for (int i = 0; i < tm; i++) 44 { 45 int temp; 46 scanf("%d", &temp); // 去0 47 if (temp) 48 num[m++] = temp; 49 } 50 LL sum = 0; 51 for (int i = 1; i <= m; i++) 52 { 53 res = 0; 54 dfs(0, 0, i); 55 if (i & 1) 56 sum += res; 57 else 58 sum -= res; 59 } 60 printf("%I64d\n", sum); 61 } 62 return 0; 63 }
HDU 1796How many integers can you find(容斥原理)
标签:
原文地址:http://www.cnblogs.com/zhaopAC/p/5506773.html