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

     Color the necklace
时间限制:2000 ms | 内存限制:65535 KB
难度:0
描述
As we all know, girls love necklaces, especially nice necklaces. Recently, huicpc229 has fallen in love with a girl; he wants to bring her a necklace as a gift. Now he has n colors, and the necklace is circular and it has n beads, huicpc229 can use his colors to color the necklace. he wants to know how many different kinds of necklaces he can make,he may not use all the n colors. In this problem, the necklace is same when you rotate it around the center of the necklace or turn it over.

输入
First line is the testcase T.
Following T lines, each line is an integer n ( 1≤n ≤ 10^9 )
输出
Output the answer mod 20090531
样例输入
4
1
2
3
4
样例输出
1
3
10
55
来源
3rd Central South China Programming ContestColor the necklace



代码:
#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);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值