• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            coreBugZJ

            此 blog 已棄。

            Color,POJ 2154

            Color
            Time Limit: 2000MS
            Memory Limit: 65536K
            Total Submissions: 3175
            Accepted: 1084

            Description

            Beads of N colors are connected together into a circular necklace of N beads (N<=1000000000). Your job is to calculate how many different kinds of the necklace can be produced. You should know that the necklace might not use up all the N colors, and the repetitions that are produced by rotation around the center of the circular necklace are all neglected.

            You only need to output the answer module a given number P.

            Input

            The first line of the input is an integer X (X <= 3500) representing the number of test cases. The following X lines each contains two numbers N and P (1 <= N <= 1000000000, 1 <= P <= 30000), representing a test case.

            Output

            For each test case, output one line containing the answer.

            Sample Input

            5
            1 30000
            2 30000
            3 30000
            4 30000
            5 30000

            Sample Output

            1
            3
            11
            70
            629

            Source

            POJ Monthly,Lou Tiancheng


            Polya,只有旋轉(zhuǎn),沒(méi)有反射,歐拉函數(shù)優(yōu)化。


             1 #include <stdio.h>
             2 #include <string.h>
             3 
             4 #define  L  50000
             5 
             6 int nPrime, prime[ L ];
             7 
             8 void initPrime() {
             9         int i, j;
            10         nPrime = 0;
            11         memset( prime, 0sizeof(prime) );
            12         for ( i = 2; i < L; ++i ) {
            13                 if( prime[ i ] == 0 ) {
            14                         prime[ nPrime++ ] = i;
            15                         for ( j = i+i; j < L; j+=i ) {
            16                                 prime[ j ] = 1;
            17                         }
            18                 }
            19         }
            20 }
            21 
            22 int phi( int n ) {
            23         int i, ans = n;
            24         for ( i = 0; (i<nPrime)&&(prime[i]*prime[i]<=n); ++i ) {
            25                 if ( n % prime[ i ] == 0 ) {
            26                         ans = ans / prime[ i ] * ( prime[ i ] - 1 );
            27                         do {
            28                                 n /= prime[ i ];
            29                         } while ( n % prime[ i ] == 0 );
            30                 }
            31         }
            32         if ( n != 1 ) {
            33                 ans = ans / n * (n-1);
            34         }
            35         return ans;
            36 }
            37 
            38 int power( int a, int b, int m ) {
            39         int t = 0, ans = 1;
            40         a %= m;
            41         while ( (1<<t) < b ) {
            42                 ++t;
            43         }
            44         while ( t >= 0 ) {
            45                 ans = ( ans * ans ) % m;
            46                 if ( (1<<t) & b ) {
            47                         ans = ( ans * a ) % m;
            48                 }
            49                 --t;
            50         }
            51         return ans;
            52 }
            53 
            54 int solve( int n, int p ) {
            55         int i, ans = 0;
            56         for ( i = 1; i*< n; ++i ) {
            57                 if ( n % i == 0 ) {
            58                         ans = ( ans + phi( i   ) % p * power( n, n/i-1, p ) ) % p;
            59                         ans = ( ans + phi( n/i ) % p * power( n, i-1,   p ) ) % p;
            60                 }
            61         }
            62         if ( i*== n ) {
            63                 ans = ( ans + phi( i ) % p * power( n, i-1, p ) ) % p;
            64         }
            65         return ans;
            66 }
            67 
            68 int main() {
            69         int td, n, p;
            70         initPrime();
            71         scanf( "%d"&td );
            72         while ( td-- > 0 ) {
            73                 scanf( "%d%d"&n, &p );
            74                 printf( "%d\n", solve( n, p ) );
            75         }
            76         return 0;
            77 }
            78 



            posted on 2011-04-18 22:24 coreBugZJ 閱讀(388) 評(píng)論(0)  編輯 收藏 引用 所屬分類: ACM

            日韩精品久久无码中文字幕| 国产99精品久久| 久久婷婷五月综合国产尤物app| 一本久久综合亚洲鲁鲁五月天亚洲欧美一区二区 | 亚洲国产精品无码久久| 久久久久AV综合网成人 | 久久久久久A亚洲欧洲AV冫| 亚洲一级Av无码毛片久久精品| 久久免费的精品国产V∧| 久久久噜噜噜久久中文字幕色伊伊| 99久久国产综合精品女同图片| 色综合合久久天天综合绕视看 | 69久久夜色精品国产69| 久久久久久久综合日本| 99国产欧美久久久精品蜜芽| 国产精品乱码久久久久久软件| 青青草原1769久久免费播放| 久久精品国产AV一区二区三区| 久久se精品一区精品二区国产 | 国产精品久久久久久福利69堂| 中文精品99久久国产| 精品久久久久久国产三级| 久久综合久久自在自线精品自| 思思久久99热免费精品6| 国产成人久久777777| aaa级精品久久久国产片| 久久人妻少妇嫩草AV无码专区| 亚洲国产天堂久久综合| 久久人人爽人人爽人人片AV东京热| 久久精品免费一区二区三区| 久久久久久久97| 久久久久久国产精品免费无码 | 久久亚洲精品成人av无码网站| 久久这里都是精品| 久久国产欧美日韩精品免费| 久久久久亚洲av成人无码电影| 99久久亚洲综合精品网站| 久久精品国产精品青草app| 亚洲国产成人久久综合碰碰动漫3d| 97久久精品国产精品青草| 麻豆精品久久精品色综合|