Codeforces 487C 数论+构造

26 篇文章 0 订阅
6 篇文章 0 订阅

Prefix Product Sequence
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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].

Input

The only input line contains an integer n (1 ≤ n ≤ 105).

Output

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.

Examples
input
7
output
YES
1
4
3
6
5
2
7
input
6
output
NO
Note

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;
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值