SGU 108 – “Fake Prime Generator”

/* A very very very good problem
 * WA & MLE at least 10 times...WTF...
 * Need rolling array to reduce memeory to 4MB
 * Need pre-calc digit sum to reduce time
 * Cannot store whole a[MAXN] for 1..10^7 since memory is not enough
 * Checking while calculating and dupliated data should be handled
 * A problem similar to compute prime.
 */
 
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
 
bool is[65]={0};
int  digitSum[10000]={0};
int  maxn, m;
int  r[5005],id[5005],ans[5005];
 
bool cmp(const int& i,const int& j)
{
	return r[i]<r[j];
}
int main()
{	
	int i;
	//Compute digitSum of 0-9999
	for(i=0;i<10000;i++) digitSum[i]=digitSum[i/10]+i%10;	
 
	while(scanf("%d %d",&maxn,&m)!=EOF)
	{		
		memset(is,0,sizeof(is));
		for(i=0;i<m;i++) {
			scanf("%d",&r[i]);
			id[i]=i;
		}
 
		sort(id,id+m,cmp);		
 
		int pos=0,n=0,pr=0,tmp;
		for(i=1;i<=maxn;i++)
		{			
			if(!is[pos]) {
				++n;						
				while(pr<m && r[id[pr]]==n) ans[id[pr++]]=i; // Same for multiple, WA: missing pr<m !!!
			}
			is[pos]=false;
			tmp=digitSum[i%10000]+digitSum[i/10000];
			if(tmp+i<=maxn) {			
				is[(pos+tmp)%64]=true;
			}
			pos=(pos+1)%64;
		}			
		printf("%d\n",n); //Forget to output!!! WTF
		for(int i=0;i<m;i++)
		{
			if(i) printf(" ");
			printf("%d",ans[i]);
		}
		printf("\n");
	}
	return 0;
}

Leave a Reply

Your email address will not be published.