POJ 2409 Let it Bead

思路

同这道题,只是颜色数从3变成c

代码

#include <cstdio>
#include <algorithm>
#include <cstring>
#define int long long
using namespace std;
int gcd(int a,int b){
    return (b==0)?a:gcd(b,a%b);
}
int pow(int a,int b){
    int ans=1;
    while(b){
        if(b&1)
            ans=(ans*a);
        a=(a*a);
        b>>=1;
    }
    return ans;
}
int n,m;
signed main(){
    while(scanf("%lld %lld",&n,&m)==2){
        if(n==0&&m==0)
            break;
        int ans=0,cnt=0;
        for(int i=0;i<m;i++){
            ans=(ans+pow(n,gcd(m,i)));
            cnt++;
        }
        if(m%2){
            ans=(ans+pow(n,(m-1)/2+1)*m);
            cnt+=m;
        }
        else{
            ans=(ans+pow(n,(m-2)/2+2)*(m/2));
            cnt+=(m/2);
            ans=(ans+pow(n,m/2)*(m/2));
            cnt+=(m/2);
        }
        printf("%lld\n",ans/cnt);
    }
    return 0;
}
posted @ 2019-03-24 20:25  dreagonm  阅读(107)  评论(0编辑  收藏  举报