CodeForces 487C Prefix Product Sequence
来源:互联网 发布:政府服务平台软件 编辑:程序博客网 时间:2024/05/17 09:12
题意:
构造一个1~n的排列 使得n个前缀积%n是一个0~n-1的排列
思路:
首先确定n一定放最后 要不然会有%n会有多个0 这时n-1位置的前缀积为(n-1)!
接着讨论n n为合数时只有n=4有解 因为如果n为合数一定可以拆成p*q的形式 明显pq|(n-1)!
然后构造ai=(i+1)*inv[i] 因为(i+1)*inv[i] == (j+1)*inv[j]时一定有i==j 所以这样构造满足ai是唯一的 也就是说是一个排列
而且这样构造使得前缀积 a1*a2*a3... = 1 * 2 * inv[1] * 3 * inv[2]... 那么%n的结果也是一个排列
最后输出答案即可 1~n的逆元可以打表求出 递推公式为 (mod-mod/i)*inv[mod%i]%mod
PS:队友说构造的想法来自于 “这种题%n后的排列一定很特殊 所以尝试 1 2 3 4 5 .... 即可”
代码:
#include<cstdio>#include<iostream>#include<cstring>#include<string>#include<algorithm>#include<map>#include<set>#include<vector>#include<queue>#include<cstdlib>#include<ctime>#include<cmath>using namespace std;typedef long long LL;#define N 100010int inv[N];int n;int main() { scanf("%d", &n); if (n == 1) { puts("YES"); printf("1\n"); return 0; } else if (n == 4) { puts("YES"); printf("1\n3\n2\n4\n"); return 0; } for (int i = 2; i * i <= n; i++) { if (n % i == 0) { puts("NO"); return 0; } } puts("YES"); puts("1"); inv[1] = 1; for (int i = 2; i < n; i++) { inv[i] = (LL) (n - n / i) * inv[n % i] % n; printf("%d\n", (int) ((LL) (i) * inv[i - 1] % n)); } printf("%d\n", n); return 0;}
0 0
- CodeForces 487C Prefix Product Sequence
- codeforces 487C Prefix Product Sequence
- codeforces 487C C. Prefix Product Sequence(数论+构造)
- codeforces 487C Prefix Product Sequence (模逆元+构造)
- Codeforces 487C Prefix Product Sequence(逆元)
- Codeforces 487C. Prefix Product Sequence 逆元+构造
- codeforces 487 C Prefix Product Sequence 构造+逆元
- CodeForces 487 C.Prefix Product Sequence(数论+构造)
- codeforces 488 E. Prefix Product Sequence
- cf 487C Prefix Product Sequence 构造+逆元
- Codeforces Round #278 (Div. 1) C. Prefix Product Sequence 模逆元,构造 2017/1/25
- Codeforces Round #278 (Div. 2) E. Prefix Product Sequence
- CodeForces 405C Unusual Product
- CodeForces 405C Unusual Product
- Codeforces 405C Unusual Product
- Codeforces 405C Unusual Product
- codeforces 405C Unusual Product
- CodeForces 13C Sequence
- 使用gdb调试postfix
- Chrome浏览器安装开发者模式的扩展程序方法
- Linux多线程编程 - sleep 和 pthread_cond_timedwait
- 进制转换与原码反码补码
- makefile文件名函数使用
- CodeForces 487C Prefix Product Sequence
- iOS:navigationItem自定义时无法紧贴左侧
- Demo讲解之Lua和C++牵手
- performSelector may cause a leak because its selector is unknown
- HDFS配置项
- eclipse java代码和xml智能提示
- 好好学习 天天向上
- ps 历史记录画笔工具 实现抠图效果 实例 && 利用蒙版工具
- LR接口性能测试提示Code - 60990 Error: Two Way Communication Error: Function two_way_comm_post_message / two_