codeforces 488 E. Prefix Product Sequence

E. Prefix Product Sequence
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
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].

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.

Sample test(s)
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)1,2,3,4有解,>4的合数都无解,反之有解
(2)解都可以由结果1,2,3,...,n - 1,0反推
对于结论1,可以发现对于>4的合数都可以用小于它的两个数乘起来得到,所以0会出现在中间,所以>4的合数都无解。
对于结论2,可以发现,n为质数的时候,f(x)=x*inv(x-1)是一个单射,所以结论2成立
/*======================================================
# Author: whai
# Last modified: 2015-12-05 19:46
# Filename: e.cpp
======================================================*/
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <stack>

using namespace std;

#define LL __int64
#define PB push_back
#define P pair<int, int>
#define X first
#define Y second

const int N = 1e5 + 5;

bool is_pri[N];
void get_pri(int n) {
	memset(is_pri, 1, sizeof(bool) * (n + 1));
	is_pri[0] = is_pri[1] = 0;
	for (int i = 2; i <= n; ++i)
		if (is_pri[i]) {
			if (n / i < i) break;
			for (int j = i * i; j <= n; j += i) is_pri[j] = false;
		}
}

LL inv(LL a, LL m) {
	LL p = 1, q = 0, b = m, c, d;
	while (b > 0) {
		c = a / b;
		d = a; a = b; b = d % b;
		d = p; p = q; q = d - c * q;
	}
	return p < 0 ? p + m : p;
}

int ans[N];

int ny[N];

int main() {
	get_pri(N - 1);
	int n;
	while(cin>>n) {
		if(n == 1) {
			puts("YES");
			puts("1");
			continue;
		}
		if(n == 2) {
			puts("YES");
			puts("1");
			puts("2");
			continue;
		}
		if(n == 4) {
			puts("YES");
			puts("1\n3\n2\n4\n");
			continue;
		}
		if(!is_pri[n]) {
			puts("NO");
		} else {
			puts("YES");
			ans[0] = 1;
			ans[n - 1] = n;
			for(int i = 1; i < n - 1; ++i) {
				ny[i] = inv(i, n);
			}
			for(int i = 1; i < n - 1; ++i) {
				ans[i] = (LL)(i + 1) * ny[i] % n;
			}

			for(int i = 0; i < n; ++i) {
				cout<<ans[i]<<endl;
			}
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值