Codeforces 487C 数论+构造
来源:互联网 发布:c语言入门自学书籍推荐 编辑:程序博客网 时间:2024/05/21 22:13
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
YES1436527
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;}
- Codeforces 487C 数论+构造
- codeforces 487C C. Prefix Product Sequence(数论+构造)
- CodeForces 487 C.Prefix Product Sequence(数论+构造)
- codeforces 449C C. Jzzhu and Apples(数论+构造)
- codeforces 121C C. Lucky Permutation(数论+构造)
- codeforces 222C C. Reducing Fractions(数论+构造)
- Codeforces #323 div2 C. GCD Table 数论 构造
- CodeForces 449 C.Jzzhu and Apples(构造+数论)
- Codeforces 303A 构造+数论
- CodeForces 7C 【数论】
- 【cf 487C】【数论+构造】【根据前缀积取模构造序列】
- CodeForces 399C Cards (数论)
- Codeforces 696C PLEASE(数论)
- codeforces round # 412 c(数论)
- Codeforces gym 101353 C 数论
- CodeForces 600C【构造】
- CodeForces 110C 【构造】
- Codeforces 141C【构造】
- libIconv库实现中文中字符串与GBK、Unicode、UTF-8三种编码互转
- 汽车VIN码图像识别/汽车车架号OCR识别
- 2016年终总结
- 第二十五天(简单标签)
- Codeforces 762A-k-th divisor
- Codeforces 487C 数论+构造
- 点击隐藏Div代码
- awk处理跨行(多行)记录的文件
- HRBUST2381:MOD(二分)
- 使用bootstrap实现轮播图的触屏函数
- php扩展vld的安装
- Activity启动模式图文详解:standard, singleTop, singleTask 以及 singleInstance
- stl中list容器的嵌套
- 【Python】学习笔记——-Python2 和 Python3的区别