即求P1^n1+P2^n2 + ... + Pk^nk <= n,其中Pk为素数的所有可能组合。
思路是DP。1~1000的素数就不到200个。
dp[i][j]表示上式和不超过且当前最小素数为P[j]的所有可能情况。注意dp[i][0]+1即为所求。
1 /* 4345 */ 2 #include <iostream> 3 #include <sstream> 4 #include <string> 5 #include <map> 6 #include <queue> 7 #include <set> 8 #include <stack> 9 #include <vector> 10 #include <deque> 11 #include <algorithm> 12 #include <cstdio> 13 #include <cmath> 14 #include <ctime> 15 #include <cstring> 16 #include <climits> 17 #include <cctype> 18 #include <cassert> 19 #include <functional> 20 #include <iterator> 21 #include <iomanip> 22 using namespace std; 23 //#pragma comment(linker,"/STACK:102400000,1024000") 24 25 #define sti set<int> 26 #define stpii set<pair<int, int> > 27 #define mpii map<int,int> 28 #define vi vector<int> 29 #define pii pair<int,int> 30 #define vpii vector<pair<int,int> > 31 #define rep(i, a, n) for (int i=a;i<n;++i) 32 #define per(i, a, n) for (int i=n-1;i>=a;--i) 33 #define clr clear 34 #define pb push_back 35 #define mp make_pair 36 #define fir first 37 #define sec second 38 #define all(x) (x).begin(),(x).end() 39 #define SZ(x) ((int)(x).size()) 40 #define lson l, mid, rt<<1 41 #define rson mid+1, r, rt<<1|1 42 43 const int maxn = 1005; 44 bool isPrime[maxn]; 45 int P[maxn], pn = 0; 46 __int64 dp[maxn][205]; 47 __int64 ans[maxn]; 48 49 void init() { 50 memset(isPrime, true, sizeof(isPrime)); 51 rep(i, 2, maxn) { 52 if (isPrime[i]) { 53 P[pn++] = i; 54 for (int j=i*i; j<maxn; j+=i) 55 isPrime[j] = false; 56 } 57 } 58 59 #ifndef ONLINE_JUDGE 60 printf("pn = %d ", pn); 61 #endif 62 ans[1] = 1; 63 rep(i, 2, 1001) { 64 rep(j, 0, pn) { 65 if (P[j] > i) 66 break; 67 for (int k=P[j]; k<=i; k*=P[j]) { 68 dp[i][j] += dp[i-k][j+1] + 1; 69 } 70 } 71 per(j, 0, 200) 72 dp[i][j] += dp[i][j+1]; 73 ans[i] = dp[i][0] + 1; 74 } 75 76 #ifndef ONLINE_JUDGE 77 rep(i, 1, 1001) 78 printf("%d: %I64d ", i, ans[i]); 79 #endif 80 } 81 82 int main() { 83 ios::sync_with_stdio(false); 84 #ifndef ONLINE_JUDGE 85 freopen("data.in", "r", stdin); 86 freopen("data.out", "w", stdout); 87 #endif 88 89 int n; 90 91 init(); 92 93 while (scanf("%d", &n)!=EOF) 94 printf("%I64d ", ans[n]); 95 96 #ifndef ONLINE_JUDGE 97 printf("time = %d. ", (int)clock()); 98 #endif 99 100 return 0; 101 }