nyoj-Color the necklace(Ploya定理 + 欧拉函数 + 扩展欧几里得(求逆元))

来源:互联网 发布:淘宝旧版本4.0.0下载 编辑:程序博客网 时间:2024/05/01 15:41

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=688

此题题解 不太懂,因为对这些概念,定理太模糊,理解起来比较困难,不过想想还是应该把代码写出来;


题意:给你一个数 n ,代表 n 种颜色和n个珠子,问你可以组合多少种长度为n的项链;不需要用掉n种颜色,项链的旋转和翻转都是为同一条

题解: http://pan.baidu.com/s/1ntwPVkd#dir/path=%2FACM%E5%A5%97%E9%A2%98%2F09%E5%B9%B4%20-%203rd%20Central%20South%20China%20Programming%20Contest%2Fcsc2009%2FC%2Fsolution

点进去之后点击C.ppt


公式:


#include<stdio.h>#include<math.h>#include<string.h>#include<algorithm>using namespace std;typedef long long LL;const LL mod = 20090531;LL x,y;LL euler(LL p)//欧拉定理{    LL res = p;    for(LL i = 2;i <= sqrt(p);i++)    {        if(p%i==0)        {            while(p%i==0) p/=i;            res = res/i*(i-1);        }    }    if(p > 1) res = res / p * (p-1);    return res%mod;}LL fast_pow(LL a,LL b)//快速幂{    LL res = 1;    while(b)    {        if(b&1) res = (res * a) % mod;        a = (a * a) % mod;        b >>= 1;    }    return res;}void edgcd(LL a,LL b)//求逆元{    if(b == 0)    {x = 1;y = 0;return;}    edgcd(b,a%b);    LL temp = x;    x = y;y = temp - a/b*y;}void solve(LL m){    LL res = 0;    for(LL i = 1;i <= sqrt(m);i++)    {        if(m%i==0)        {            if(m/i != i) res = (res + euler(i)*fast_pow(m,m/i-1))%mod;            res = (res + euler(m/i)*fast_pow(m,i-1))%mod;        }    }    if(m&1)    {        res = (fast_pow(m,(m+1)/2) + res) % mod;        edgcd(2,mod);    }    else    {        res = (fast_pow(m,m/2+1) + fast_pow(m,m/2) + 2 * res) % mod;        edgcd(4,mod);    }    while(x < 0) x = (x + mod) % mod;    printf("%lld\n",x * res % mod);}int main(){    int n;    LL m;    scanf("%d",&n);    while(n--)    {        scanf("%lld",&m);        solve(m);    }}


0 0