Submission #828525


Source Code Expand

#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cstring>
#include <cassert>
#define SIZE 100005

using namespace std;
typedef long long int ll;

int to[SIZE];
bool use[SIZE];
int cnt[SIZE];
int ans[SIZE],ret[SIZE];
int nd[SIZE];

ll gcd(ll a,ll b)
{
	if(a==0) return b;
	return gcd(b%a,a);
}
int main()
{
	int n;
	ll K;
	scanf("%d %lld",&n,&K);
	for(int i=0;i<n;i++)
	{
		scanf("%d",&to[i]);
		to[i]--;
	}
	if(K==1)
	{
		for(int i=0;i<n;i++)
		{
			if(i!=0) printf(" ");
			printf("%d",to[i]+1);
		}puts("");
		return 0;
	}
	for(int i=0;i<n;i++)
	{
		if(!use[i])
		{
			int now=i;
			int len=0;
			do
			{
				use[now]=true;
				len++;
				now=to[now];
			}while(now!=i);
			cnt[len]++;
		}
	}
	bool up=true;
	for(int i=1;i<=n;i++)
	{
		if(cnt[i]>0)
		{
			ll t=1,zan=K;
			int s=i;
			for(int j=2;j*j<=s;j++)
			{
				if(s%j==0)
				{
					while(s%j==0) s/=j;
					while(zan%j==0)
					{
						zan/=j;
						t*=(ll) j;
					}
				}
			}
			if(s!=1)
			{
				while(zan%s==0)
				{
					zan/=s;
					t*=(ll) s;
				}
			}
			//printf("%d %lld : %d\n",i,t,cnt[i]);
			if(cnt[i]%t!=0)
			{
				puts("Impossible");
				return 0;
			}
			if(t>=2) up=false;//i>=2
			else if(t==1)
			{
				for(int j=2;j<=cnt[i];j++)
				{
					if(K%j==0)
					{
						up=false;
						break;
					}
				}
			}
		}
	}
	if(!up)
	{
		puts("Ambiguous");
		return 0;
	}
	//全部一種類からなるcycle
	memset(use,false,sizeof(use));
	for(int i=0;i<n;i++)
	{
		if(!use[i])
		{
			int now=i;
			int L=0;
			do
			{
				use[now]=true;
				nd[L++]=now;
				now=to[now];
			}while(now!=i);
			int z=(int) (K%L);
			assert(gcd(K,L)==1);
			int k=0;
			while((ll) z*(ll) k%L!=1) k++;
			//ans[i]=to^k[i]=nd[pos[i]+k] -> ans[nd[i]]=nd[i+k]
			for(int j=0;j<L;j++) ans[nd[j]]=nd[(j+k)%L];
		}
	}
	//for(int i=0;i<n;i++) printf("%d ",ans[i]);puts("");
	//for(int i=0;i<n;i++) ret[ans[i]]=i;
	for(int i=0;i<n;i++)
	{
		if(i!=0) printf(" ");
		printf("%d",ans[i]+1);
	}puts("");
	return 0;
}

Submission Info

Submission Time
Task H - Shuffling Machine
User yutaka1999
Language C++ (GCC 4.4.7)
Score 0
Code Size 2134 Byte
Status TLE
Exec Time 1544 ms
Memory 2160 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:27: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result
./Main.cpp:30: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result

Judge Result

Set Name All
Score / Max Score 0 / 100
Status
AC × 40
TLE × 8
Set Name Test Cases
All 1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 3, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 4, 40, 41, 42, 43, 44, 45, 46, 47, 48, 5, 6, 7, 8, 9
Case Name Status Exec Time Memory
1 AC 37 ms 948 KB
10 AC 27 ms 876 KB
11 AC 37 ms 1132 KB
12 AC 27 ms 796 KB
13 AC 39 ms 1132 KB
14 AC 48 ms 1300 KB
15 AC 74 ms 2020 KB
16 AC 45 ms 1256 KB
17 AC 48 ms 1260 KB
18 AC 47 ms 1256 KB
19 AC 71 ms 1896 KB
2 AC 31 ms 804 KB
20 AC 73 ms 1892 KB
21 AC 49 ms 1248 KB
22 AC 76 ms 1772 KB
23 AC 48 ms 1260 KB
24 AC 50 ms 1260 KB
25 TLE 1536 ms 2032 KB
26 AC 49 ms 1256 KB
27 AC 49 ms 1260 KB
28 TLE 1536 ms 2160 KB
29 AC 48 ms 1260 KB
3 AC 29 ms 796 KB
30 AC 47 ms 1324 KB
31 AC 73 ms 2004 KB
32 AC 49 ms 1264 KB
33 AC 48 ms 1348 KB
34 AC 49 ms 1256 KB
35 AC 27 ms 928 KB
36 AC 27 ms 928 KB
37 AC 27 ms 1000 KB
38 AC 27 ms 924 KB
39 AC 31 ms 872 KB
4 AC 27 ms 880 KB
40 AC 29 ms 932 KB
41 AC 29 ms 864 KB
42 AC 28 ms 808 KB
43 AC 28 ms 876 KB
44 AC 28 ms 876 KB
45 TLE 1536 ms 996 KB
46 TLE 1537 ms 1000 KB
47 TLE 1544 ms 996 KB
48 TLE 1537 ms 1004 KB
5 AC 27 ms 924 KB
6 AC 26 ms 928 KB
7 AC 30 ms 932 KB
8 TLE 1533 ms 1004 KB
9 TLE 1536 ms 1004 KB