题目链接


\(Description\)

对于一个序列\(a_i\),定义其前缀积序列为\(a_1\ \mathbb{mod}\ n,\ (a_1a_2)\ \mathbb{mod}\ n,...,(a_1a_2...a_n)\ \mathbb{mod}\ n\)。

给定\(n\),求一个\(n\)的排列,使得该排列的前缀积序列是\([0,1,2,...,n-1]\)的一个排列。无解输出\(NO\)。

\(n\leq10^5\)。

\(Solution\)

考虑无解的情况。因为\(n!\equiv0\ (\mathbb{mod}\ n)\),所以\((n-1)\not\equiv0\ (\mathbb{mod}\ n)\)。

\(n\)为质数显然可以满足。否则设\(n=pq\)。

若\(p\neq q\),那么有\((n-1)!\equiv0\ (\mathbb{mod}\ n)\),GG了。

若\(p=q\),当\(n>4\)时,\(2p<n\),所以也有\((n-1)!\equiv0\ (\mathbb{mod}\ n)\),GG。

所以\(n\)为大于\(4\)的合数时无解。特判一下\(n=4\)。

首先\(a_1\)要填\(1\),\(a_n\)要填\(n\)。

考虑能不能直接让前缀积序列变成\(1,2,...,0\)。那么\(a_i=\frac{i}{i-1}\ \mathbb{mod}\ n,\ i>1\)。

只需要判断是否有\(\frac{a}{a-1}=\frac{b}{b-1},\ 1\lt a\neq b\lt n\)。

稍微化一下,\(\frac{a}{a-1}=1+\frac1a,\ \frac{b}{b-1}=1+\frac1b\),而我们知道每个数的逆元是唯一的,所以这么做就OK啦。


//46ms	600KB
#include <cstdio>
#include <algorithm>
typedef long long LL;
const int N=1e5+5; int A[N],inv[N]; bool IsPrime(int x)
{
int t=0;
for(int i=2; x!=1; ++i)
while(!(x%i))
{
x/=i;
if(++t>1) return 0;
}
return 1;
} int main()
{
int n; scanf("%d",&n);
if(n==4) return printf("YES\n1\n3\n2\n4\n"),0;
if(!IsPrime(n)) return puts("NO"),0;
A[1]=1, A[n]=n, inv[1]=1;
for(int i=2; i<n; ++i) inv[i]=1ll*(n-n/i)*inv[n%i]%n, A[i]=1ll*i*inv[i-1]%n;
puts("YES");
for(int i=1; i<=n; ++i) printf("%d\n",A[i]); return 0;
}

Codeforces.487C.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. [CF 487C Prefix Product Sequence]

    题意 将1~n的正整数重排列,使得它的前缀积在模n下形成0~n-1的排列,构造解或说明无解.n≤1E5. 思考 小范围内搜索解,发现n=1,n=4和n为质数时有解. 不难发现,n一定会放在最后,否则会 ...

  4. 487C Prefix Product Sequence

    传送门 题目大意 分析 因为n为质数所以i-1的逆元唯一 因此ai唯一 代码 #include<iostream> #include<cstdio> #include<c ...

  5. cf487C Prefix Product Sequence

    Consider a sequence [a1, a2, ... , an]. Define its prefix product sequence . Now given n, find a per ...

  6. Prefix Product Sequence CodeForces - 487C (数论,构造)

    大意: 构造一个[1,2,...n]的排列, 使得前缀积模n为[0,1,...,n-1]的排列 这种构造都好巧妙啊, 大概翻一下官方题解好了 对于所有>=6的合数$n$, 有$(n-1)! \e ...

  7. codeforces 1042C Array Product【构造】

    题目:戳这里 题意:n个数,两种操作,第一种是a[i]*a[j],删掉a[i],第一种是直接删除a[i](只能用一次)剩下的数序列号不变.操作n-1次,使最后剩下的那个数最大化. 解题思路: 正数之间 ...

  8. Codeforces 486E LIS of Sequence(线段树+LIS)

    题目链接:Codeforces 486E LIS of Sequence 题目大意:给定一个数组.如今要确定每一个位置上的数属于哪一种类型. 解题思路:先求出每一个位置选的情况下的最长LIS,由于開始 ...

  9. CodeForces 837F - Prefix Sums | Educational Codeforces Round 26

    按tutorial打的我血崩,死活挂第四组- - 思路来自FXXL /* CodeForces 837F - Prefix Sums [ 二分,组合数 ] | Educational Codeforc ...

随机推荐

  1. java----String解析

    String在内存中的分析: public class Demo { public static void main(String[] args){ String a = new String(&qu ...

  2. OAuth2 token

    1.资源服务器 package com.ruhuanxingyun.config; import com.fasterxml.jackson.databind.ObjectMapper; import ...

  3. shell 判断目录是否存在

    判断/data/www/logs/wos_log/crontab_log是否, 如果不存在则新建 if [ ! -d "/data/www/logs/wos_log/crontab_log& ...

  4. SyntaxError: EOL while scanning string literal

    在Python 中,这个提示,一般是因为特殊字符引起的,比如换行符,比如 \ 等. 下面有几个示例: 1. 换行符 # 源错误代码 get_tabs="select b.owner,b.ta ...

  5. Html列表分页算法

    public class PageHelper { /// <summary> /// 标签 /// </summary> public string Tag { get; s ...

  6. vue.js学习:1.0到2.0的变化(区别)

    一.生命周期 1.1.0的生命周期: 周期 解释 init 组件刚刚被创建,但Data.method等属性还没被计算出来 created 组件创建已经完成,但DOM还没被生成出来 beforeComp ...

  7. 将Elasticsearch的快照备份到HDFS

    1.安装Elasticsearch插件repository-hdfs 下载地址:https://artifacts.elastic.co/downloads/elasticsearch-plugins ...

  8. ExceptionLess的MVC调用

    引用 <package id="Exceptionless" version="4.2.1989" targetFramework="net46 ...

  9. P2860 [USACO06JAN]冗余路径Redundant Paths

    题解: 首先要边双缩点这很显然 然后变成树上问题 发现dp,dfs好像不太对 考虑一下度数 发现只要在度数为1的点之间连边 但我好像不太会证明这个东西.. 网上也没有看到比较正确的证明方法和连边策略. ...

  10. Codeforces Gym100543G Virus synthesis 字符串 回文自动机 动态规划

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF-100543G.html 题目传送门 - CF-Gym100543G 题意 你可以对一个字符串进行以下两种操 ...