POJ 2154 Color(组合数学-波利亚计数,数论-欧拉函数,数论-整数快速幂)

Color

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 7693   Accepted: 2522

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

题目大意:

T组测试数据,每组一个n表示1个项链有n个颜色可以涂在n个钻石上,通过旋转相同的算一种方案,问你方案数是多少?

解题思路:

很裸的波利亚计数,转化为的公式就是 ans=sum{ n^( gcd(1,n)-1  ) ,n^( gcd(2,n)-1  ),n^( gcd(3,n)-1  ) .....n^( gcd(n,n)-1  )     },因为这个n比较大10^9,所以暴力超时。

因此枚举 gcd(k,n)=l 的有多少个,也就是 k=l*x ,n=l*y,也就是gcd(x,y)=1,也就是找到 1~y与y互质的数有多少个,答案:欧拉函数

解题代码:

#include <iostream>
#include <cstdio>
using namespace std;

typedef long long ll;
int n,p;

inline ll getPhi(int x){
    ll ans=x;
    for(int i=2;i*i<=x;i++){
        if(x%i==0){
            ans=ans/i*(i-1);
            while(x%i==0) x/=i;
        }
    }
    if(x>1) ans=ans/x*(x-1);
    return ans;
}

inline ll pow_mod(ll a,ll b){
    ll sum=1;
    while(b>0){
        if(b&1) sum=(sum*a)%p;
        a=(a*a)%p;
        b/=2;
    }
    return sum%p;
}

int main(){
    int t;
    scanf("%d",&t);
    while(t-- >0){
        scanf("%d%d",&n,&p);
        ll ans=0;
        for(int i=1;i*i<=n;i++){
            if(n%i==0){
                if(i*i==n) ans=(ans+(getPhi(i)*pow_mod(n,i-1))%p)%p;
                else{
                    ans=(ans+(getPhi(n/i)*pow_mod(n,i-1))%p)%p;
                    ans=(ans+(getPhi(i)*pow_mod(n,n/i-1))%p)%p;
                }
            }
        }
        printf("%lld\n",ans%p);
    }
    return 0;
}

POJ 2154 Color(组合数学-波利亚计数,数论-欧拉函数,数论-整数快速幂)

时间: 2024-10-11 20:21:17

POJ 2154 Color(组合数学-波利亚计数,数论-欧拉函数,数论-整数快速幂)的相关文章

poj 2154 Color(polya计数 + 欧拉函数优化)

http://poj.org/problem?id=2154 大致题意:由n个珠子,n种颜色,组成一个项链.要求不同的项链数目,旋转后一样的属于同一种,结果模p. n个珠子应该有n种旋转置换,每种置换的循环个数为gcd(i,n).如果直接枚举i,显然不行.但是我们可以缩小枚举的数目.改为枚举每个循环节的长度L,那么相应的循环节数是n/L.所以我们只需求出每个L有多少个i满足gcd(i,n)= n/L,就得到了循环节数为n/L的个数.重点就是求出这样的i的个数. 令cnt = gcd(i,n) =

欧拉函数 / 蒙哥马利快速幂 / 容斥

一:知识点 欧拉函数参考1 浅谈欧拉函数参考2 欧拉函数的定义: 在数论中,对于正整数N,少于或等于N ([1,N]),且与N互质(即gcd为1)的正整数(包括1)的个数,记作φ(n).     欧拉函数的延伸: 小于或等于n的数中,与n互质的数的总和为:φ(x) * x / 2  (n>1). 欧拉函数φ(x)模板: ll Euler(int n)//即求φ(x) { ll ret=n; for(int i=2;i<=sqrt(n);i++) if(n%i==0) { ret=ret/i*(

POJ 2480 Longge&#39;s problem (欧拉函数+乘性函数)

Longge's problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7343   Accepted: 2422 Description Longge is good at mathematics and he likes to think about hard mathematical problems which will be solved by some graceful algorithms. Now

HDU 4002 Find the maximum(数论-欧拉函数)

Find the maximum Problem Description Euler's Totient function, φ (n) [sometimes called the phi function], is used to determine the number of numbers less than n which are relatively prime to n . For example, as 1, 2, 4, 5, 7, and 8, are all less than

欧拉函数性质与求法 [数论][欧拉函数]

n的欧拉函数值用符号φ(n)表示 欧拉函数的定义是,对于一个正整数n,小于n且与n互质的数的数目(包括1,特殊地,φ(1)=1 ). 设p1,p2,p3,...,pr为n的全部r个质因数,则有φ(n)=n*(1-1/p1)*(1-1/p2)*(1-1/p3)*(1-1/p4)…..(1-1/pr). 显然,用这个方法来计算单个欧拉函数是可以求解的. 附上代码: 1 int get_phi(int x){ 2 int re=x; 3 for(int i=2;i*i<=x;i++) 4 if(x%i

hdu1395 数论 欧拉函数

hdu1395 数论   欧拉函数对于给出的每一个n 求最小正整数 x 满足 2^x mod n = 1 1.如果给出的n 是偶数或者 1 则一定无解2.如果是奇数 首先根据欧拉定理 我们可知 phi(n)一定是满足要求的 然后答案一定是 phi( i ) 的因数 然后我们就可以 O(sqrt(phi(i))的时间内 枚举每个因数 然后快速幂验证就行了 1 #include <bits/stdc++.h> 2 using namespace std ; 3 4 const double eps

题解报告:poj 2480 Longge&#39;s problem(欧拉函数)

Description Longge is good at mathematics and he likes to think about hard mathematical problems which will be solved by some graceful algorithms. Now a problem comes: Given an integer N(1 < N < 2^31),you are to calculate ∑gcd(i, N) 1<=i <=N. 

数论 - 欧拉函数的运用 --- poj 3090 : Visible Lattice Points

Visible Lattice Points Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5636   Accepted: 3317 Description A lattice point (x, y) in the first quadrant (x and y are integers greater than or equal to 0), other than the origin, is visible fr

数论 - 欧拉函数模板题 --- poj 2407 : Relatives

Relatives Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11372   Accepted: 5544 Description Given n, a positive integer, how many positive integers less than n are relatively prime to n? Two integers a and b are relatively prime if ther