zoukankan      html  css  js  c++  java
  • 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
    ",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;
    }
    



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

  • 相关阅读:
    Weblogic魔法堂:AdminServer.lok被锁导致启动、关闭域失败
    CSS魔法堂:盒子模型简介
    .Net魔法堂:提取注释生成API文档
    CSS魔法堂:Position定位详解
    CMD魔法堂:获取进程路径和PID值的方法集
    CentOS6.5菜鸟之旅:识别NTFS分区
    CentOS6.5菜鸟之旅:安装rpmforge软件库
    CMD魔法堂:CMD进入指定目录
    CentOS6.5菜鸟之旅:VIM插件NERDtree初探
    CSS魔法堂:选择器及其优先级
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/4669398.html
Copyright © 2011-2022 走看看