zoukankan      html  css  js  c++  java
  • [ACM] POJ 2154 Color (Polya计数优化,欧拉函数)

    Color
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 7630   Accepted: 2507

    Description

    Beads of N colors are connected together into a circular necklace of N beads (N<=1000000000). Your job is to calculate how many different kinds of the necklace can be produced. You should know that the necklace might not use up all the N colors, and the repetitions that are produced by rotation around the center of the circular necklace are all neglected.

    You only need to output the answer module a given number P.

    Input

    The first line of the input is an integer X (X <= 3500) representing the number of test cases. The following X lines each contains two numbers N and P (1 <= N <= 1000000000, 1 <= P <= 30000), representing a test case.

    Output

    For each test case, output one line containing the answer.

    Sample Input

    5
    1 30000
    2 30000
    3 30000
    4 30000
    5 30000
    

    Sample Output

    1
    3
    11
    70
    629
    

    Source

    POJ Monthly,Lou Tiancheng


    解题思路:

    转载:http://blog.csdn.net/tsaid/article/details/7366708

    题意:给出两个整数n和p,代表n个珠子,n种颜色,要求不同的项链数,翻转置换不考虑。

    结果模p.

    题解:

    我们知道gcd(i,n)表示了循环节的个数。

    比如gcd(2,6) = 2, 它的详细过程为:[1。3。5] [2。4,6]

    对于随意一个循环置换,他全部循环节的长度为 n / gcd(i,n),在上面的样例中: 循环节长度 = 6 / gcd(2,6) = 3


    为了方便说明。用L表示循环节的长度,显然 L | n

    假设我们枚举L,求出对于每个L有多少个i, 使得 L = n / gcd (i,n), 那么我们实际上也得到了循环节个数为 n / L 的置换个数。


    将L = n / gcd (i,n)转换一下得到:n / L = gcd(i,n )

    设 cnt = n / L = gcd(i, n)  注:cnt表示循环节个数,L表示每个循环节的长度

    由于 cnt | i, 所以可令 i = cnt * t; ( 由于0 <= i < n, 所以0 <= t < n / cnt = L )

    又由于 cnt = n / L, 所以 n = cnt * L;

    则 gcd ( i, n ) = gcd ( cnt*t, cnt*L ) = cnt;  ①

    可知当 gcd ( t, L ) = 1 时 ① 式成立。

    因为 gcd ( t, L ) = 1 的个数就是 Euler(L)的个数。

    所以我们能够得到结论:循环节个数为n/L的置换有Euler(L)个

    代码:

    #include <iostream>
    #include <string.h>
    #include <stdio.h>
    using namespace std;
    bool isprime[50001];
    int prime[50001];
    int len=0;;
    int n,p;
    
    void sieve()
    {
        for(int i=0;i<=50000;i++)
            isprime[i]=1;
        isprime[0]=isprime[1]=0;
        for(int i=2;i<=50000;i++)
        {
            if(isprime[i])
            {
                prime[len++]=i;
                for(int j=2*i;j<=50000;j+=i)
                    isprime[j]=0;
            }
        }
    }
    
    int euler(int n)
    {
        int res=n;
        for(int i=0;i<len&&prime[i]*prime[i]<=n;i++)
        {
            if(n%prime[i]==0)
            {
                res=res/prime[i]*(prime[i]-1);
                while(n%prime[i]==0)
                    n/=prime[i];
            }
        }
        if(n>1)
            res=res/n*(n-1);
        return res;
    }
    
    int pow(int p,int n,int mod)
    {
        int ans=1;
        p=p%mod;
        while(n)
        {
            if(n&1)
                ans=ans*p%mod;
            p=p*p%mod;
            n/=2;
        }
        return ans;
    }
    
    int main()
    {
        sieve();
        int t;
        scanf("%d",&t);
        while(t--)
        {
            int ans=0;
            scanf("%d%d",&n,&p);
            for(int i=1;i*i<=n;i++)
                if(n%i==0)
                {
                    ans=(ans+euler(i)%p*pow(n,n/i-1,p))%p;//注意取余的位置。 euler(i)%p不取余就WA
                    if(i*i==n)//仅仅要一个i就能够了
                        break;
                    ans=(ans+euler(n/i)%p*pow(n,i-1,p))%p;
                }
            printf("%d
    ",ans);
        }
        return 0;
    }
    



  • 相关阅读:
    Kibana 基本操作
    kibana安装
    es7.2版本安装ik分词
    Elastic Search闪退问题
    Elastic Search安装-windows
    1-ES简单介绍
    vue项目1-pizza点餐系统12-设计购物车
    vue项目1-pizza点餐系统11-设计menu页面
    前端:table、thead、th、tr、td
    某二分图匹配题的题解
  • 原文地址:https://www.cnblogs.com/brucemengbm/p/6907466.html
Copyright © 2011-2022 走看看