Consider a sequence [a1, a2, ... , an]. Define its prefix product sequence .
Now given n, find a permutation of [1, 2, ..., n], such that its prefix product sequence is a permutation of [0, 1, ..., n - 1].
The only input line contains an integer n (1 ≤ n ≤ 105).
In the first output line, print "YES" if such sequence exists, or print "NO" if no such sequence exists.
If any solution exists, you should output n more lines. i-th line contains only an integer ai. The elements of the sequence should be different positive integers no larger than n.
If there are multiple solutions, you are allowed to print any of them.
7
YES 1 4 3 6 5 2 7
6
NO
For the second sample, there are no valid sequences.
题意:给你一个n,构造出1-n的排列使得a1%n a1a2%n .... a1a2...an%n 生成的序列是0-(n-1)的一个排列
题解:特判1和4 如果n是合数就为NO,因为不管怎么排列前面项乘起来都会变成n的倍数,造成出现多个0,
因此n这个数也必须放到最后面,即an=n。
如果n是素数
令 ai=i/(i-1)(mod n)
i乘上i-1的逆元即可
a1*a2*a3*....ai=1*inv(1)*2*inv(2)....*i-1*inv(i-1)*i=i
所以此序列也是合法序列
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
ll judge(ll n){
for(ll i=2;i*i<=n;i++){
if(n%i==0)return 0;
}
return 1;
}
ll quick(ll a,ll k,ll mod){
ll ans=1;
while(k){
if(k&1)ans=ans*a%mod;
a=a*a%mod;
k/=2;
}
return ans;
}
int main(){
ll n;
scanf("%lld",&n);
if(n==4){
printf("YES\n1\n3\n2\n4\n");
return 0;
}
else if(n==1){
printf("YES\n1\n");
return 0;
}
if(judge(n)){
ll now=1;
printf("YES\n1\n");
for(ll i=2;i<n;i++){
now=quick(i-1,n-2,n);
printf("%lld\n",i*now%n);
}
printf("%lld\n",n);
}
else{
printf("NO\n");
}
return 0;
}