POJ 2409 Let it Bead(Polya计数原理)

题目链接

和上个题一样。。

 1 #include <stdio.h>

 2 #include <string.h>

 3 #include <math.h>

 4 #define eps 0.0000001

 5 int gcd(int a,int b)

 6 {

 7     return b == 0?a:gcd(b,a%b);

 8 }

 9 int main()

10 {

11     double sum;

12     int i,n,m;

13     __int64 s;

14     while(scanf("%d%d",&n,&m)!=EOF)

15     {

16         sum = 0;

17         if(n == 0&&m == 0) break;

18         for(i = 1; i <= m; i ++)

19         {

20             sum += pow(n,gcd(i,m));

21         }

22         if(m%2==0)

23         {

24             sum += pow(n,m/2+1)*m/2;

25             sum += pow(n,m/2)*m/2;

26         }

27         else

28         {

29             sum += pow(n,m/2+1)*m;

30         }

31         s = (__int64)(sum/2/m+eps);

32         printf("%I64d\n",s);

33     }

34     return 0;

35 }

你可能感兴趣的:(poj)