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.

Example

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.

要求给出1~n的排列,使得前1,2...n项之积模n恰好包含[0,n-1]中所有数

显然n要放最后,不然乘个n之后后面的积都是0了(不过这句话好像并没有卵用)

显然[0,n-1]每个数不能出现超过1次(好像也没有卵用)

如果n是质数,非常简单,令模n之后分别是1,2,3,4...n-1,0,

倒推可知第一个数是1,第二个数是2乘(1关于n的逆元),第三个数是3乘(2关于n的逆元)……最后一个是n

如果n不是质数,,,太恶心了

先给结论:如果n==4,发现有一解1,3,2,4是可以的,否则不行。

证明:当n为合数且n!=4,不妨假设n=p*q(p>2,p>=q)

那么1~pq当中有至少三个不同的数q,2q,pq。

显然n=pq是一定要放在排列最后的,再分类讨论:

p==q,n=p^2,而且p,2p的位置要在在p^2之前,显然p*2p=2p^2=2n,所以在p处,或者2p处前k项的积就是0了,p^2处也是0,显然不行

p!=q,p和q都在pq之前,同理,在p处,或者q处前k项的积是pq的倍数,所以也是0,而pq处也是0,也不行

*所以只有p==q==2的时候,没有p和2p同时出现,所以找到了一组解

 #include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<ctime>
#define LL long long
#define inf 0x7ffffff
#define pa pair<int,int>
#define mkp(a,b) make_pair(a,b)
#define pi 3.1415926535897932384626433832795028841971
#define mod 100007
using namespace std;
inline LL read()
{
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n;
LL ni[];
inline LL quickpow(LL a,int b)
{
LL s=;
while (b)
{
if (b&)s=(s*a)%n;
a=(a*a)%n;
b>>=;
}
return s;
}
int main()
{
n=read();
if (n==){puts("YES\n1");return ;}
if (n==){puts("YES\n1\n3\n2\n4\n");return ;}
for (int i=;i<=sqrt(n);i++)if (n%i==){puts("NO");return ;}
for (int i=;i<n;i++)ni[i]=quickpow(i,n-);
puts("YES");
puts("");
for (int i=;i<n;i++)printf("%d\n",i*ni[i-]%n);
printf("%d\n",n);
}

cf487C

cf487C Prefix Product Sequence的更多相关文章

  1. Codeforces 487C. Prefix Product Sequence 逆+结构体

    意甲冠军: 对于数字n, 他询问是否有1~n置换 这种布置能够在产品上模每个前缀n 有可能0~n-1 解析: 通过观察1肯定要在首位,n一定要在最后 除4意外的合数都没有解 其它质数构造 a[i]=i ...

  2. codeforces 487C C. Prefix Product Sequence(构造+数论)

    题目链接: C. Prefix Product Sequence time limit per test 1 second memory limit per test 256 megabytes in ...

  3. Codeforces.487C.Prefix Product Sequence(构造)

    题目链接 \(Description\) 对于一个序列\(a_i\),定义其前缀积序列为\(a_1\ \mathbb{mod}\ n,\ (a_1a_2)\ \mathbb{mod}\ n,...,( ...

  4. Prefix Product Sequence CodeForces - 487C (数论,构造)

    大意: 构造一个[1,2,...n]的排列, 使得前缀积模n为[0,1,...,n-1]的排列 这种构造都好巧妙啊, 大概翻一下官方题解好了 对于所有>=6的合数$n$, 有$(n-1)! \e ...

  5. 487C Prefix Product Sequence

    传送门 题目大意 分析 因为n为质数所以i-1的逆元唯一 因此ai唯一 代码 #include<iostream> #include<cstdio> #include<c ...

  6. [CF 487C Prefix Product Sequence]

    题意 将1~n的正整数重排列,使得它的前缀积在模n下形成0~n-1的排列,构造解或说明无解.n≤1E5. 思考 小范围内搜索解,发现n=1,n=4和n为质数时有解. 不难发现,n一定会放在最后,否则会 ...

  7. codeforces 练习

    codeforces 627 D. Preorder Test 二分 + 树dp 做logn次树dp codeforces 578D.LCS Again 给出一个字符串str,长度n<=10^6 ...

  8. Subarray Product Less Than K LT713

    Your are given an array of positive integers nums. Count and print the number of (contiguous) subarr ...

  9. Adding Pagination 添加分页

    本文来自: http://www.bbsmvc.com/MVC3Framework/thread-206-1-1.html You can see from Figure 7-16 that all ...

随机推荐

  1. 从零开发分布式数据库中间件 二、构建MyBatis的读写分离数据库中间件

    在上一节 从零开发分布式数据库中间件 一.读写分离的数据库中间件 中,我们讲了如何通过ThreadLocal来指定每次访问的数据源,并通过jdbc的连接方式来切换数据源,那么这一节我们使用我们常用的数 ...

  2. vue+element ui项目总结点(四)零散细节概念巩固如vue父组件调用子组件的方法、拷贝数据、数组置空问题 等

    vue config下面的index.js配置host: '0.0.0.0',共享ip (假设你的电脑启动了这个服务我电脑一样可以启动)-------------------------------- ...

  3. Vue v-if与v-show的区别

    用了 viewjs  预览图片的时候 发现 用着两个 还是有区别的, 相同点==== v-if与v-show都可以动态控制dom元素显示隐藏 不同点 = ====v-if显示隐藏是将dom元素整个添加 ...

  4. C#背景图片自适应

    1.选中窗体修改属性 2.在load添加代码 private void Form1_Load(object sender, EventArgs e) { this.BackgroundImageLay ...

  5. chosen选择框加载数据

    1.单选$(select).val($("#id").val());$(select).trigger("chosen:updated"); 2.多选 func ...

  6. java集合测试类等

    package demo.mytest; import java.lang.ref.SoftReference;import java.lang.ref.WeakReference;import ja ...

  7. Spring框架针对dao层的jdbcTemplate操作crud之query查询数据操作 —— 查询表,返回结果为对象的list集合

    用JdbcTemplate的方法完成, 查询数据库表,把用户表sw_user所有数据以List<User>集合返回 在JdbcTemplateDemo类中增加查询返回所有对象集合的方法qu ...

  8. java代码解析二维码

    java代码解析二维码一般步骤 本文采用的是google的zxing技术进行解析二维码技术,解析二维码的一般步骤如下: 一.下载zxing-core的jar包: 二.创建一个BufferedImage ...

  9. perl学习二:简单变量

    字符串变量:${}1.单引号:不进行变量替换,不进行转义,字符串可以跨行.2.双引号:变量替换(贪婪匹配原则).支持转义字符(转义字符可以另外看)3.反引号 字符串的特殊表示方法:qq(...) q( ...

  10. Python_编程题集_001_词法解析

    1.词法解析: 我的是名字是ths,今年18岁 语法分析后得到结果如下: 数字:18 中文:我的名字是 今年 岁 拼音:ths 符号:,. 请编写程序实现该词法分析功能 string模块解: impo ...