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. 基于51单片机个LCD1602的万年历程序

    小白 第一次跟新博客 基于51单片机和LCD1602的万年历程序 可实现走时和调时功能 有简单的1602菜单制作 欢迎大家交流 LCD1602和51单片机的连接方法 RS = P3^5; //数据/命 ...

  2. CAD控件界面显示与隐藏(网页版)

    控件界面工具栏的显示或隐藏,js代码实现如下: 1 2 3 4 5 6 7 8 9 //隐藏/显示工具栏       mxOcx.ShowToolBar("常用工具",isShow ...

  3. OpenCV2:直方图

    一.简介 在一个单通道的灰度图像中,每个像素的值介于0(黑色)~255(白色)之间,灰色图像的直方图有256个条目(或称为容器)

  4. [BZOJ3631]:[JLOI2014]松鼠的新家(LCA+树上差分)

    题目传送门 题目描述: 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他居然真的住在“树”上.松鼠想邀 ...

  5. ios 团购信息客户端demo(三)

    接上二篇的内容,今天我们就来介绍一下如何将解析出来的数据放入AQGridView中显示出来,因为我们的工程中已经将AQGridView导入了,所以我们在KKFirstViewController中直接 ...

  6. Cocos2d-X研究之3.0 场景切换特效汇总

    Cocos2d-X研究之3.0 场景切换特效汇总 2014-08-05      0个评论    来源:游戏编程    收藏    我要投稿 cocos2d-x 3.0中场景切换特效比较多,而且游戏开 ...

  7. PAT 乙级 1005

    题目 题目地址:PAT 乙级 1005 题解 本题主要就在于将一个数的一系列计算结果不重复地存储起来并便于检索,考虑到STL中的集合有相似的特性,使用set可以有效地简化代码和运算. 过程如下: (初 ...

  8. 【Java_多线程并发编程】基础篇——线程状态扭转函数

    1. wait() sleep() yield() join()用法与区别 本文提到的当前线程是指:当前时刻,获得CPU资源正在执行的线程. 1.1 wait()方法 wait()方法定义在Objec ...

  9. java代码生成二维码

    java代码生成二维码一般步骤 常用的是Google的Zxing来生成二维码,生成的一般步骤如下: 一.下载zxing-core的jar包: 二.需要创建一个MatrixToImageWriter类, ...

  10. Perl学习之四:语句(续)

    循环控制:1.last 退出标签的语句块2.next 3.redo不推荐,循环次数不可控 4.goto不推荐.***************************************标签: 先 ...