Cod sursa(job #466849)

Utilizator AndreyPAndrei Poenaru AndreyP Data 27 iunie 2010 18:47:00
Problema Congr Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.87 kb
#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <algorithm>
using namespace std;
#define P 300005

int p;
int a[P<<1];
int b[P],c[P];
int sum=0;
inline void citire()
{
	scanf("%d",&p);
	for(int i=1; i<=p; ++i)
	{
		scanf("%d",&a[i]);
		a[i]%=p;
		sum+=a[i];
		if(sum>=p)
			sum-=p;
		b[i]=i;
	}
	for(int i=1; i<p; ++i)
	{
		scanf("%d",&a[i+p]);
		a[i+p]%=p;
		c[i]=i+p;
	}
}

inline void rezolva()
{
	srand(time(NULL));
	int x,y;
	while(sum!=0)
	{
		x=rand()%p+1;
		y=rand()%(p-1)+1;
		sum-=a[b[x]];
		if(sum<0)
			sum+=p;
		sum+=a[c[y]];
		if(sum>=p)
			sum-=p;
		swap(b[x],c[y]);
	}
}

inline void scrie()
{
	for(int i=1; i<=p; ++i)
		printf("%d ",b[i]);
	printf("\n");
}

int main()
{
	freopen("congr.in","r",stdin);
	freopen("congr.out","w",stdout);
	
	citire();
	rezolva();
	scrie();
	
	return 0;
}