poj 2409 Let it Bead 夜

http://poj.org/problem?id=2409

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<map>
#include<cmath>
#define LL long long

using namespace std;

int gcd(int a,int b)
{
    if(a%b==0)
    return b;
    return gcd(b,a%b);
}
LL ploya(int n,int m)
{
    if(n==0)
    return 0;
    LL ans=0;
    for(int i=1;i<=n;++i)
    {
        ans+=(LL)(pow(double(m),gcd(n,i)));
    }//cout<<ans<<endl;
    if(n%2==1)
    {
        ans+=(n*((LL)(pow(double(m),(n+1)/2))));
    }else
    {
        ans+=(n/2*(LL)(pow(double(m),n/2)+pow(double(m),(n+2)/2)));
    }
    return ans/n/2;
}
int main()
{
    int n,m;
    while(scanf("%d %d",&m,&n)!=EOF)
    {
        if(n==0&&m==0)
        break;
        cout<<ploya(n,m)<<endl;
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/liulangye/p/2607707.html