#include <cstdio> #include <iostream> using namespace std; #define sz 10001 #define hi 101 #define nSuperPrime 201 bool prime[sz]; int primeTable[1230],nPrime,superPrime[nSuperPrime]; void sieve(){ int i,j; for( i=4; i < sz ;i += 2) prime[i] = true; primeTable[nPrime++] = 2; for( i=3; i <= hi;i += 2){ if(!prime[i]){ primeTable[nPrime++] = i; for(j = i+i; j < sz; j += i) prime[j] = true; } } for( i= hi+2; i < sz; i += 2) if(!prime[i]) primeTable[nPrime++] = i; } typedef struct{ int cnt,prev; }data; data tmp[sz]; int main(){ int i,j,t,high,n; sieve(); for(i = 0; i < sz; i++)//used for precalculation tmp[i].cnt = sz,tmp[i].prev = 0; for(i=0; primeTable[i] <= nPrime; i++){//saving superPrime superPrime[i] = primeTable[primeTable[i]-1]; } tmp[0].cnt = 0; for(i = 0; i < nSuperPrime; i++){//finding out ans high = sz - superPrime[i]; for(j = 0; j < high; j++){ if(tmp[j].cnt != sz && ( tmp[j].cnt + 1 < tmp[j+superPrime[i]].cnt)){ tmp[j+superPrime[i]].cnt = tmp[j].cnt + 1; tmp[j+superPrime[i]].prev = j; } } } while(scanf("%d",&n) == 1){ if(tmp[n].cnt == sz) printf("0\n"); else{ printf("%d\n",tmp[n].cnt); printf("%d",n - tmp[n].prev); t = tmp[n].prev; while(t){ printf(" %d",t - tmp[t].prev); t = tmp[t].prev; } printf("\n"); } } return 0; }
Pages
▼
▼
No comments:
Post a Comment