poj-2154-polya+euler函数
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 11758 | Accepted: 3783 |
Description
You only need to output the answer module a given number P.
Input
Output
Sample Input
5
1 30000
2 30000
3 30000
4 30000
5 30000
Sample Output
1
3
11
70
629
Source
#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
#define LL long long
#define PI acos(-1.0)
#define N 33333
int mod;
bool isp[N+];
vector<int>prime;
void init(){
int m=sqrt(N+0.5);
for(int i=;i<=N;i++){
if(!isp[i]){
prime.push_back(i);
for(int j=i*i;j<=N;j+=i)isp[j]=;
}
}
}
int qpow(int a,int b,int m){
a%=m;
int r=;
while(b){
if(b&) r=r*a%m;
b>>=;
a=a*a%m;
}
return r%m;
}
int euler(int n){
int m=sqrt(n+0.5);
int ans=n;
for(int i=;prime[i]<=m;++i){
if(n%prime[i]==){
ans=ans/prime[i]*(prime[i]-);
while(n%prime[i]==) n/=prime[i];
}
}
if(n>) ans=ans/n*(n-);
return ans%mod;
}
int main()
{
int t,n,i,j,k,d;
init();
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&mod);
int ans=;
for(i=;i*i<n;++i){
if(n%i==){
(ans+=(euler(n/i)*qpow(n,i-,mod)%mod))%=mod; (ans+=(euler(i)*qpow(n,n/i-,mod)%mod))%=mod;
}
}
if(i*i==n) (ans+=(euler(i)*qpow(n,i-,mod)%mod))%=mod;
cout<<ans<<endl;
}
return ;
}
poj-2154-polya+euler函数的更多相关文章
- POJ burnside&&polya整理练习
POJ 2409 Let it Bead 这题就是polya公式的直接套用,唯一麻烦的是置换群的种类数,由于可以翻转,所以除了要加上pow(c,gcd(s,i))这些平面旋转的置换群,还要加上翻转的. ...
- POJ 2407.Relatives-欧拉函数O(sqrt(n))
欧拉函数: 对正整数n,欧拉函数是少于或等于n的数中与n互质的数的数目. 对于一个正整数N的素数幂分解N=P1^q1*P2^q2*...*Pn^qn. Euler函数表达通式:euler(x)=x(1 ...
- 2021.08.10 Euler函数总结
2021.08.10 Euler函数总结 知识: 记 φ(n) 表示在 [1,n] 中与 n互质的数的个数. 1.p为质数,则 \[φ(p^l)=p^l-p=p^{l-1}(p-1) \] 注:每p个 ...
- poj 2154 Color(polya计数 + 欧拉函数优化)
http://poj.org/problem?id=2154 大致题意:由n个珠子,n种颜色,组成一个项链.要求不同的项链数目.旋转后一样的属于同一种.结果模p. n个珠子应该有n种旋转置换.每种置换 ...
- [ACM] POJ 2154 Color (Polya计数优化,欧拉函数)
Color Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7630 Accepted: 2507 Description ...
- poj 2154 Color【polya定理+欧拉函数】
根据polya定理,答案应该是 \[ \frac{1}{n}\sum_{i=1}^{n}n^{gcd(i,n)} \] 但是这个显然不能直接求,因为n是1e9级别的,所以推一波式子: \[ \frac ...
- poj2409 & 2154 polya计数+欧拉函数优化
这两个题都是项链珠子的染色问题 也是polya定理的最基本和最经典的应用之一 题目大意: 用m种颜色染n个珠子构成的项链,问最终形成的等价类有多少种 项链是一个环.通过旋转或者镜像对称都可以得到置换 ...
- POJ 2154 color (polya + 欧拉优化)
Beads of N colors are connected together into a circular necklace of N beads (N<=1000000000). You ...
- POJ 2154 【POLYA】【欧拉】
前记: TM终于决定以后干啥了.这几天睡的有点多.困饿交加之间喝了好多水.可能是灌脑了. 切记两件事: 1.安心当单身狗 2.顺心码代码 题意: 给你N种颜色的珠子,串一串长度问N的项链,要求旋转之后 ...
- POJ 2154 Color [Polya 数论]
和上题一样,只考虑旋转等价,只不过颜色和珠子$1e9$ 一样的式子 $\sum\limits_{i=1}^n m^{gcd(i,n)}$ 然后按$gcd$分类,枚举$n$的约数 如果这个也化不出来我莫 ...
随机推荐
- (zhuan) Paper Collection of Multi-Agent Reinforcement Learning (MARL)
this blog from: https://github.com/LantaoYu/MARL-Papers Paper Collection of Multi-Agent Reinforcemen ...
- Dependency Injection2
IoC容器和Dependency Injection 模式 使用 Service Locator 依赖注入的最大好处在于:它消除了MovieLister类对具体 MovieFinder实现类的依赖 ...
- linux mysql操作命令大全
1.linux下启动mysql的命令:mysqladmin start/ect/init.d/mysql start (前面为mysql的安装路径) 2.linux下重启mysql的命令:mysqla ...
- Latex citation using natbib and footnotesize
References: Natbib bibliography styles How to change font size for bibliography? Latex citation usin ...
- CentOS7使用firewalld和selinux
转载自莫小安的博客:https://www.cnblogs.com/moxiaoan/p/5683743.html 如何查看和使用selinux https://blog.csdn.net/edide ...
- 2、Python函数详解(0601)
函数的基础概念 1.函数是python为了代码最大程度的重用和最小化代码冗余而提供的基本程序结构: 2.函数是一种设计工具,它能让程序员将复杂的系统分解为可管理的部件: 3.函数用于将相关功能打包并参 ...
- 51nod 1052 最大M子段和
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1052 题意: 思路:设$dp[i][j]$表示前j个数构成i个字段时的最 ...
- Centos6.8安装redis(一)
最近有在学习会话共享的配置,其中一种呢是 nginx+redis+tomcat 的会话共享配置,在记录此会话共享配置之前呢先记录下redis等的安装.这篇先简单记录下redis的安装,是其中一种方式, ...
- RedHat(Linux)下安装Python3步骤
1. 下载解压.$ wget https://www.python.org/ftp/python/3.4.1/Python-3.4.1.tgz$ tar zxvf Python-3.4.1.tgz 2 ...
- python测试框架&&数据生成&&工具最全资源汇总
xUnit frameworks 单元测试框架frameworks 框架unittest - python自带的单元测试库,开箱即用unittest2 - 加强版的单元测试框架,适用于Python 2 ...