codeforces 487C C. Prefix Product Sequence(构造+数论)
题目链接:
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 题意: 能否找到一个[1,n]的一个排列.使得前缀积是一个关于n的完全剩余系; 思路: 假设这个完全剩余系最后是1,2,3,...n-1,0;我们看能不能找到这样的排列,由逆元我们知道, i=1*inv[1]*2*inv[2]*...(i-1)*inv[i-1]*i;交错排列取出来,ans[i]=inv[i-1]*i;最后一个是ans[n]=n;这样就好了; AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} //const LL mod=1e9+7;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=1e5+20;
const int maxn=4e3+220;
const double eps=1e-12; int n;
LL ans[N]; int isprime(int x)
{
int le=(int)sqrt(x*1.0+0.5);
for(int i=2;i<=le;i++)
{
if(x%i==0)return 0;
}
return 1;
}
LL pow_mod(int x,int y,int mod)
{
LL s=1,base=x;
while(y)
{
if(y&1)s=s*base%mod;
base=base*base%mod;
y>>=1;
}
return s;
}
int main()
{ read(n);
if(n==1)printf("YES\n1\n");
else if(n==4)printf("YES\n1\n3\n2\n4\n");
else
{
if(!isprime(n)){cout<<"NO\n";return 0;}
cout<<"YES\n";
ans[1]=1;
For(i,2,n-1)ans[i]=(LL)(i)*pow_mod(i-1,n-2,n)%n;
ans[n]=n;
For(i,1,n)print(ans[i]); }
return 0;
}
codeforces 487C C. Prefix Product Sequence(构造+数论)的更多相关文章
- Codeforces.487C.Prefix Product Sequence(构造)
题目链接 \(Description\) 对于一个序列\(a_i\),定义其前缀积序列为\(a_1\ \mathbb{mod}\ n,\ (a_1a_2)\ \mathbb{mod}\ n,...,( ...
- Codeforces 487C. Prefix Product Sequence 逆+结构体
意甲冠军: 对于数字n, 他询问是否有1~n置换 这种布置能够在产品上模每个前缀n 有可能0~n-1 解析: 通过观察1肯定要在首位,n一定要在最后 除4意外的合数都没有解 其它质数构造 a[i]=i ...
- 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 ...
- [CF 487C Prefix Product Sequence]
题意 将1~n的正整数重排列,使得它的前缀积在模n下形成0~n-1的排列,构造解或说明无解.n≤1E5. 思考 小范围内搜索解,发现n=1,n=4和n为质数时有解. 不难发现,n一定会放在最后,否则会 ...
- 487C Prefix Product Sequence
传送门 题目大意 分析 因为n为质数所以i-1的逆元唯一 因此ai唯一 代码 #include<iostream> #include<cstdio> #include<c ...
- Codeforces Beta Round #13 C. Sequence (DP)
题目大意 给一个数列,长度不超过 5000,每次可以将其中的一个数加 1 或者减 1,问,最少需要多少次操作,才能使得这个数列单调不降 数列中每个数为 -109-109 中的一个数 做法分析 先这样考 ...
- CF45G Prime Problem 构造+数论
正解:构造+数论 解题报告: 传送门! maya这题好神仙啊我jio得,,,反正我当初听的时候是没有太懂的,,, 首先这题你要知道一些必要的数学姿势 比如哥德巴赫猜想巴拉巴拉的 然后直接讲题趴QAQ ...
- Codeforces Round #604 (Div. 2) D. Beautiful Sequence(构造)
链接: https://codeforces.com/contest/1265/problem/D 题意: An integer sequence is called beautiful if the ...
随机推荐
- 细说Mysql四种安装方法及自动化部署
一.简介 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库, 每个数据库都有一个或多个不同的API用于创建,访问,管理,搜索和复制所保存的数据. 我们也可以将数据存储在文件中,但是 ...
- Sigleton 单例模式 的简单应用
需求:一个简单的后台java程序,收集信息,并将信息发送到远端服务器. 实现:实现一个后台线程,实时处理发送过来的信息,并将信息发送到服务器. 技术要点: 1.单例模式 2.队列 并没有实现全部代码, ...
- Android笔记——什么是json?json如何使用?
什么是json 什么是json,json是什么,json如何使用 JSON是JavaScript Object Notation的缩写,可见JSON来源于JavaScript.JSON数据是一系列键值 ...
- asp.net mvc Html.BeginForm()方法
Html.BeginForm()方法将会输出<form>标签,而且必须以using包起来,如此便可在using程序代码最后退出时,让asp.net mvc帮你补上<form>标 ...
- swift学习笔记之-继承
//继承 import UIKit /* 继承(Inheritance): 1.一个类可以继承(inherit)另一个类的方法(methods).属性(properties)和其它特性.当一个类继承其 ...
- ListView属性整理
stackFromBottom属性,这只该属性之后你做好的列表就会显示你列表的最下面,值为true和false android:stackFromBottom="true" 第 ...
- iOS开发中的一些细节BUG的解决
这篇博客里我将不定期更新自己遇到的一些细节上的BUG,并提供解决方法,遇到同样问题的童鞋们可以参考交流一下. 1.关于tableView的tableHeaderView 请注意,我这里说的是table ...
- iOS之通过PaintCode快速实现交互动画的最方便方法 未解问题
需求: 问题: 源码百度云下载链接: http://pan.baidu.com/s/1o7r4hCm 密码: 8atd 其他学习链接:http://www.jianshu.com/p/90d6cd35 ...
- iphone,pad等真机不被xcode识别,解决方法
问题现象:xcode 的项目在进行真机调试时,xcode不能识别真机, 调测情况分析: 1 设备正常,itunes 能够识别 2 在apple developer 中创建项目的Provisioning ...
- 《大话移动APP测试:Android与iOS应用测试指南》
<大话移动app测试:android与ios应用测试指南> 基本信息 作者: 陈晔 出版社:清华大学出版社 ISBN:9787302368793 上架时间:2014-7-7 出版日期:20 ...