参考: http://blog.csdn.net/acm_cxlove/article/details/7860735
感觉这里需要记录一下
#include <map> #include <set> #include <list> #include <cmath> #include <ctime> #include <deque> #include <stack> #include <queue> #include <cctype> #include <cstdio> #include <string> #include <vector> #include <climits> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> #define LL long long #define PI 3.1415926535897932626 using namespace std; int gcd(int a, int b) {return a % b == 0 ? b : gcd(b, a % b);} int mason[] = {3,7,31,127,8191,131071,524287,2147483647}; int cnt[] = {2,3,5,7,13,17,19,31}; bool dp[1<<8]; int K,src[110]; int trans(int x) { int ans = 0; for (int i = 0; i < 8; i++) { if (x % mason[i] == 0) { x /= mason[i]; if (x % mason[i] == 0) return 0; ans |= (1<<i); } } if (x != 1) return 0; return ans; } int calcu(int sta) { int ans = 0; for (int i = 0; i < 8; i++) if (sta & (1<<i)) ans += cnt[i]; return ans; } int main() { while (scanf("%d",&K)!=EOF) { //if (K == 0) break; int cnt = 0; for (int i = 0; i < K; i++) { scanf("%d",&src[i]); src[i] = trans(src[i]); if (src[i] == 0) cnt++; } if (cnt == K) { puts("NO"); continue; } memset(dp,false,sizeof(dp)); dp[0] = true; for (int i = 0; i < K; i++) for (int j = 0; j < (1<<8); j++) { if (!(src[i] & j)) if (dp[j]) dp[j|src[i]] = true; } int ans = 0; for (int i = 0; i < (1<<8); i++) if (dp[i]) ans = max(ans,calcu(i)); printf("%d\n",ans); } return 0; }
原文:http://www.cnblogs.com/Commence/p/4004764.html