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

题目链接: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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值