CodeForces 487C Prefix Product Sequence

题意:

构造一个1~n的排列  使得n个前缀积%n是一个0~n-1的排列

思路:

首先确定n一定放最后  要不然会有%n会有多个0  这时n-1位置的前缀积为(n-1)!

接着讨论n  n为合数时只有n=4有解  因为如果n为合数一定可以拆成p*q的形式  明显pq|(n-1)!

然后构造ai=(i+1)*inv[i]  因为(i+1)*inv[i] == (j+1)*inv[j]时一定有i==j  所以这样构造满足ai是唯一的  也就是说是一个排列

而且这样构造使得前缀积 a1*a2*a3... = 1 * 2 * inv[1] * 3 * inv[2]...  那么%n的结果也是一个排列

最后输出答案即可  1~n的逆元可以打表求出  递推公式为 (mod-mod/i)*inv[mod%i]%mod

PS:队友说构造的想法来自于 “这种题%n后的排列一定很特殊  所以尝试 1 2 3 4 5 ....  即可”

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<cstdlib>
#include<ctime>
#include<cmath>
using namespace std;
typedef long long LL;
#define N 100010

int inv[N];
int n;

int main() {
    scanf("%d", &n);
    if (n == 1) {
        puts("YES");
        printf("1\n");
        return 0;
    } else if (n == 4) {
        puts("YES");
        printf("1\n3\n2\n4\n");
        return 0;
    }
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) {
            puts("NO");
            return 0;
        }
    }
    puts("YES");
    puts("1");
    inv[1] = 1;
    for (int i = 2; i < n; i++) {
        inv[i] = (LL) (n - n / i) * inv[n % i] % n;
        printf("%d\n", (int) ((LL) (i) * inv[i - 1] % n));
    }
    printf("%d\n", n);
    return 0;
}
时间: 2024-08-07 09:13:22

CodeForces 487C Prefix Product Sequence的相关文章

codeforces 487C Prefix Product Sequence (模逆元+构造)

转自http://blog.csdn.net/houserabbit/article/details/41513745 题解写的真棒.. 题目链接:http://codeforces.com/problemset/problem/487/C 题目大意:构造一个1~n的排列  使得n个前缀积对n取余是一个0~n-1的排列 题目分析:好题,首先我们通过简单的分析可以得到n肯定是最后一个数,因为如果n在前面,前缀积肯定不止1个是n的倍数,也就是说对n取模至少有两个0,显然不满足排列,也就是说取模得到排

[CF 487C Prefix Product Sequence]

题意 将1~n的正整数重排列,使得它的前缀积在模n下形成0~n-1的排列,构造解或说明无解.n≤1E5. 思考 小范围内搜索解,发现n=1,n=4和n为质数时有解. 不难发现,n一定会放在最后,否则会多出很多的0. 1.n≥4且n为合数:由于n能写成pq的形式,其中pq|(n-1)!,因此第n-1的位置上一定为0,故无解. 2.n为质数:按以下方式构造.令答案ai=i*(i-1)-1(若i-1=0,令其逆元为1).则其前缀积为1,2,3,...,n-1,0.由于i*(i-1)-1=j*(j-1)

cf487C Prefix Product Sequence

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 intege

CodeForces 837F - Prefix Sums | Educational Codeforces Round 26

按tutorial打的我血崩,死活挂第四组- - 思路来自FXXL /* CodeForces 837F - Prefix Sums [ 二分,组合数 ] | Educational Codeforces Round 26 题意: 设定数组 y = f(x) 使得 y[i] = sum(x[j]) (0 <= j < i) 求初始数组 A0 经过多少次 f(x) 后 会有一个元素 大于 k 分析: 考虑 A0 = {1, 0, 0, 0} A1 = {1, 1, 1, 1} -> {C(

Codeforces 374D Inna and Sequence 二分+树状数组

题目链接:点击打开链接 给定n个操作,m长的序列a 下面n个数 if(co>=0)则向字符串添加一个co (开始是空字符串) else 删除字符串中有a的下标的字符 直接在序列上搞,简单模拟 #include<stdio.h> #include<iostream> #include<string.h> #include<set> #include<vector> #include<map> #include<math.h&

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

题目链接:Codeforces 486E LIS of Sequence 题目大意:给定一个数组,现在要确定每个位置上的数属于哪一种类型. 解题思路:先求出每个位置选的情况下的最长LIS,因为开始的想法,所以求LIS直接用线段树写了,没有改,可以用 log(n)的算法直接求也是可以的.然后在从后向前做一次类似LIS,每次判断A[i]是否小于f[dp[i]+1],这样就可以确定该位 置是否属于LIS序列.然后为第三类的则说明dp[i] = k的只有一个满足. #include <cstdio>

Codeforces 1132A. Regular Bracket Sequence

原题链接:Codeforces 1132A. Regular Bracket Sequence 题目大意:你有\({cnt}_1,{cnt}_2,{cnt}_3,{cnt}_4\)个"((","()",")(","))",问能否将这些字符串组成一个合法的括号序列. 题解:这一道题,很明显的\({cnt}_2\)是不需要管的,对于第三种情况,它并不改变左右括号的数量差,只有第一.四情况改变,那么,很明显\({cnt}_1={cn

Codeforces GYM 100114 C. Sequence 打表

C. Sequence Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Description Integer sequences are very interesting mathematical objects. Let us examine a sequence generated with the use of two operations: doubling and “digit

【codeforces 623E】 Transforming Sequence

http://codeforces.com/problemset/problem/623/E (题目链接) 题意 长度为${n}$的满足前缀按位或为单调递增的${k}$位序列.要求每个位置为${[1,2^k-1]}$之间的整数,求方案数. Solution 毛爷爷论文题,然而论文上的${dp}$方程都是错的,坑爹啊!! 首先,每个数的二进制位上一定存在一位为${1}$,且之前的数的这一位上都为${0}$,这样才能保证按位或的前缀和单调递增.那么当${n>k}$时,显然答案是等于${0}$的,所以