Codeforces 487C. Prefix Product Sequence 逆+结构体
意甲冠军:
对于数字n, 他询问是否有1~n置换 这种布置能够在产品上模每个前缀n 有可能0~n-1
解析:
通过观察1肯定要在首位,n一定要在最后
除4意外的合数都没有解
其它质数构造 a[i]=i*inv[i-1] , 这样用逆元把前面每一个数的影响都消除掉
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
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;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
Codeforces 487C. Prefix Product Sequence 逆+结构体的更多相关文章
- Codeforces.487C.Prefix Product Sequence(构造)
题目链接 \(Description\) 对于一个序列\(a_i\),定义其前缀积序列为\(a_1\ \mathbb{mod}\ n,\ (a_1a_2)\ \mathbb{mod}\ n,...,( ...
- 487C Prefix Product Sequence
传送门 题目大意 分析 因为n为质数所以i-1的逆元唯一 因此ai唯一 代码 #include<iostream> #include<cstdio> #include<c ...
- [CF 487C Prefix Product Sequence]
题意 将1~n的正整数重排列,使得它的前缀积在模n下形成0~n-1的排列,构造解或说明无解.n≤1E5. 思考 小范围内搜索解,发现n=1,n=4和n为质数时有解. 不难发现,n一定会放在最后,否则会 ...
- codeforces 487C C. Prefix Product Sequence(构造+数论)
题目链接: C. Prefix Product Sequence time limit per test 1 second memory limit per test 256 megabytes in ...
- 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 ...
- Go语言中结构体的使用-第2部分OOP
1 概述 结构体的基本语法请参见:Go语言中结构体的使用-第1部分结构体.结构体除了是一个复合数据之外,还用来做面向对象编程.Go 语言使用结构体和结构体成员来描述真实世界的实体和实体对应的各种属性. ...
- Codeforces Round #681 (Div. 2, based on VK Cup 2019-2020 - Final) C. The Delivery Dilemma (贪心,结构体排序)
题意:你要买\(n\)份午饭,你可以选择自己去买,或者叫外卖,每份午饭\(i\)自己去买需要消耗时间\(b_i\),叫外卖需要\(a_i\),外卖可以同时送,自己只能买完一份后回家再去买下一份,问最少 ...
- Codeforces Round #531 (Div. 3) B. Array K-Coloring (结构体排序)
题意:给你\(n\)个数字,用\(k\)种颜色给他们涂色,要求每个数字都要涂,每种颜色都要用,相同的数字不能涂一样的颜色. 题解:用结构体读入每个数字和它的位置,然后用桶记录每个数字出现的次数,判断是 ...
随机推荐
- Openfire开发配置,Openfire源码配置,OpenFire二次开发配置
1.下载源码:http://www.igniterealtime.org/downloads/source.jsp 2.把源码解压出的openfire_src目录放至eclipse workplace ...
- LAMBDA表达式常用 (全)
这里主要是将数据库中的常用操作用LAMBDA表达式重新表示了下,用法不多,但相对较常用,等有时间了还会扩展,并将查询语句及LINQ到时也一并重新整理下: 1.select语句:books.Select ...
- jdk并发包 CopyOnWriteArrayList源代码分析
CopyOnWriteArrayList是jdk1.5并法包里面用于处理高并发下.读多写少的情况下.减少锁等待的集合类.以下对该类实现做一个简要的分析 1,首先CopyOnWriteArrayList ...
- Codeforces 432 D. Prefixes and Suffixes
用扩展KMP做简单省力..... D. Prefixes and Suffixes time limit per test 1 second memory limit per test 256 meg ...
- JAVA程序生成XML标准化的文件格式,缩进,美化。
//他开始Document映射到文件 TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer t ...
- js+html+css简单的互动功能页面(2015知道几乎尖笔试题)http://v.youku.com/v_show/id_XMTI0ODQ5NTAyOA==.html?from=y1.7-1.2
js+html+css实现简单页面交互功能(2015知乎前端笔试题) http://v.youku.com/v_show/id_XMTI0ODQ5NTAyOA==.html? from=y1.7-1. ...
- 11gR2更换OCR和VOTE
11gR2开始,OCR和VOTE它们被存储在ASM磁盘组,因此,更换OCR有两种方法,第一是使用ASM磁盘组drop disk数据重组后,另一种方法是OCR迁移到另一个磁盘组 第一种:add disk ...
- linux 下上传 datapoint数据到yeelink 修改版本
/*client.c*/ #include <stdio.h> #include <stdlib.h> #include <string.h> #include ...
- Sql Server远程查询db 表中的数据,以本地
step 1: sp_configure 'show advanced options', 1; RECONFIGURE; sp_configure 'Ad Hoc Distributed Queri ...
- POJ - 3249 Test for Job (DAG+topsort)
Description Mr.Dog was fired by his company. In order to support his family, he must find a new job ...