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

简介:

意甲冠军:

对于数字n, 他询问是否有1~n置换 这种布置能够在产品上模每个前缀n 有可能0~n-1


解析:

通过观察1肯定要在首位,n一定要在最后

除4意外的合数都没有解

其它质数构造 a[i]=i*inv[i-1] , 这样用逆元把前面每一个数的影响都消除掉

C. 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.



/* ***********************************************
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;
}



版权声明:本文博客原创文章,博客,未经同意,不得转载。




本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/4669398.html,如需转载请自行联系原作者


相关文章
|
11月前
|
存储
【CSAPP】HW1 | 位向量的应用 Application of bit vectors | Adressing and Byte Ordering
【CSAPP】HW1 | 位向量的应用 Application of bit vectors | Adressing and Byte Ordering
24 0
【CSAPP】HW1 | 位向量的应用 Application of bit vectors | Adressing and Byte Ordering
UVa787 - Maximum Sub-sequence Product(最大连续乘积子串)
UVa787 - Maximum Sub-sequence Product(最大连续乘积子串)
44 0
|
人工智能 测试技术
PAT (Basic Level) Practice (中文) B1008 数组元素循环右移问题 (20 分)
PAT (Basic Level) Practice (中文) B1008 数组元素循环右移问题 (20 分)
98 0
PAT (Basic Level) Practice (中文) B1008 数组元素循环右移问题 (20 分)
codeforces1509 D. Binary Literature (构造+指针)
codeforces1509 D. Binary Literature (构造+指针)
64 0
PAT (Basic Level) Practice (中文) 1010 一元多项式求导 (25 分)
PAT (Basic Level) Practice (中文) 1010 一元多项式求导 (25 分)
94 0
PAT (Basic Level) Practice (中文)- 1051 复数乘法(15 分)
PAT (Basic Level) Practice (中文)- 1051 复数乘法(15 分)
99 0
【1085】Perfect Sequence (25 分)
【1085】Perfect Sequence (25 分) 【1085】Perfect Sequence (25 分)
87 0
|
Java C++ 索引
# Leetcode 67:Add Binary(二进制求和)
Leetcode 67:Add Binary(二进制求和) (python、java) Given two binary strings, return their sum (also a binary string). The input strings are both non-empty and contains only characters 1 or 0. 给定两个二进制字符串,返回他们的和(用二进制表示)。
1376 0