Codeforces 487C. Prefix Product Sequence 逆+结构体
意甲冠军:
对于数字n, 他询问是否有1~n置换 这种布置能够在产品上模每个前缀n 有可能0~n-1
解析:
通过观察1肯定要在首位,n一定要在最后
除4意外的合数都没有解
其它质数构造 a[i]=i*inv[i-1] , 这样用逆元把前面每一个数的影响都消除掉
1 second
256 megabytes
standard input
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].
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.
/* ***********************************************
Author :CKboss
Created Time :2015年03月12日 星期四 19时58分14秒
File Name :CF487C.cpp
************************************************ */ #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map> using namespace std; typedef long long int LL; const int maxn=100100; int n;
LL a[maxn],inv[maxn]; bool isprime(int x)
{
if(x==2||x==1) return true;
if(x%2==0) return false;
for(int i=3;i*i<=x;i+=2) if(x%i==0) return false;
return true; } void solve()
{
inv[1]=1LL;
for(int i=2;i<=n;i++) inv[i]=inv[n%i]*(n-n/i)%n;
a[1]=1LL; a[n]=n;
for(int i=2;i<n;i++) a[i]=(i*inv[i-1])%n;
for(int i=1;i<=n;i++) printf("%I64d\n",a[i]);
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout); scanf("%d",&n);
if(n==4)
{
puts("YES"); puts("1"); puts("3"); puts("2"); puts("4");
return 0;
}
if(isprime(n)==false) puts("NO");
else
{
puts("YES");
solve();
} return 0;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
Codeforces 487C. Prefix Product Sequence 逆+结构体的更多相关文章
- Codeforces.487C.Prefix Product Sequence(构造)
题目链接 \(Description\) 对于一个序列\(a_i\),定义其前缀积序列为\(a_1\ \mathbb{mod}\ n,\ (a_1a_2)\ \mathbb{mod}\ n,...,( ...
- 487C Prefix Product Sequence
传送门 题目大意 分析 因为n为质数所以i-1的逆元唯一 因此ai唯一 代码 #include<iostream> #include<cstdio> #include<c ...
- [CF 487C Prefix Product Sequence]
题意 将1~n的正整数重排列,使得它的前缀积在模n下形成0~n-1的排列,构造解或说明无解.n≤1E5. 思考 小范围内搜索解,发现n=1,n=4和n为质数时有解. 不难发现,n一定会放在最后,否则会 ...
- codeforces 487C C. Prefix Product Sequence(构造+数论)
题目链接: C. Prefix Product Sequence time limit per test 1 second memory limit per test 256 megabytes in ...
- cf487C Prefix Product Sequence
Consider a sequence [a1, a2, ... , an]. Define its prefix product sequence . Now given n, find a per ...
- Prefix Product Sequence CodeForces - 487C (数论,构造)
大意: 构造一个[1,2,...n]的排列, 使得前缀积模n为[0,1,...,n-1]的排列 这种构造都好巧妙啊, 大概翻一下官方题解好了 对于所有>=6的合数$n$, 有$(n-1)! \e ...
- Go语言中结构体的使用-第2部分OOP
1 概述 结构体的基本语法请参见:Go语言中结构体的使用-第1部分结构体.结构体除了是一个复合数据之外,还用来做面向对象编程.Go 语言使用结构体和结构体成员来描述真实世界的实体和实体对应的各种属性. ...
- Codeforces Round #681 (Div. 2, based on VK Cup 2019-2020 - Final) C. The Delivery Dilemma (贪心,结构体排序)
题意:你要买\(n\)份午饭,你可以选择自己去买,或者叫外卖,每份午饭\(i\)自己去买需要消耗时间\(b_i\),叫外卖需要\(a_i\),外卖可以同时送,自己只能买完一份后回家再去买下一份,问最少 ...
- Codeforces Round #531 (Div. 3) B. Array K-Coloring (结构体排序)
题意:给你\(n\)个数字,用\(k\)种颜色给他们涂色,要求每个数字都要涂,每种颜色都要用,相同的数字不能涂一样的颜色. 题解:用结构体读入每个数字和它的位置,然后用桶记录每个数字出现的次数,判断是 ...
随机推荐
- 【ArcGIS 10.2新特性】ArcGIS 10.2 for Desktop 新特性(一)
ArcGIS 10.2 for Desktop是在10.1的成功基础上进行的改进,它的改进包括:性能提升.附加的安全性.40多个新的分析工具.3D功能提高.栅格增强.新的地理数据管理能力以及其它更多的 ...
- hdu3501
要我们求小于n并且不与n互素的数字的和, 那么可以转化为1->(n-1)的和减去小于n且与n互素的数字的和 首先,有gcd(n,i)=1, 那么gcd(n,n-i)=1, 这是因为如果a%s=0 ...
- 将ACCESS数据库迁移到SQLSERVER数据库
原文:将ACCESS数据库迁移到SQLSERVER数据库 将ACCESS数据库迁移到SQLSERVER数据库 ACCESS2000文件 用ACCESS2007打开,并迁移到SQLSERVER2005里 ...
- python可变交换性能优化
离许多新的压力python性能优化见交换两个变量值可以使用 a,b = b,a 这样能够提高性能 >>> from timeit import Timer >>> ...
- php:修改NetBeans默认字体
在Netbeans中由于使用了Swing进行开发,所以其中界面的字体也是由Java虚拟机进行配置而不是随操作系统的.在安装完Netbeans后默认的字体大小是11px.而在Windows下的宋体最小支 ...
- 重新想象 Windows 8 Store Apps (5) - 控件之集合控件: ComboBox, ListBox, FlipView, ItemsControl, ItemsPresenter
原文:重新想象 Windows 8 Store Apps (5) - 控件之集合控件: ComboBox, ListBox, FlipView, ItemsControl, ItemsPresente ...
- HTML与XML总结
阅览<孙欣HTML>和<刘炜XML>过了一段时间,在这里学到的内容用思维导图来概括. HTML与XML都是标记语言. 同样点: HTML文档与XML文档有类似的结构. 前者是( ...
- Android:自己定义PopupMenu的样式(显示图标/设置RadioButton图标)
PopupMenu是Android中一个十分轻量级的组件.与PopupWindow相比,PopupMenu的可自己定义的能力较小,但使用更加方便. 先上效果图: 本例要实现的功能例如以下: 1.强制显 ...
- Apache与Tomcat整合(转)
一 Apache与Tomcat比较联系 apache支持静态页,tomcat支持动态的,比如servlet等. 一般使用apache+tomcat的话,apache只是作为一个转发,对jsp的处理是由 ...
- WPF 绑定
WPF里分三种Binding:Binding, PriorityBinding, MultiBinding,这三种Binding的基类都是BindingBase,而BindingBase又继承于Mar ...