P4980 【模板】Polya定理
思路
polya定理的模板题,但是还要加一些优化
题目的答案就是
\]
考虑上方的式子怎么求
因为\(gcd(i,n)\)肯定有很多重复,枚举\(gcd(i,n)\),因为\(gcd(i,n)\)是\(n\)的约数,所以枚举约数
\]
然后做完了
一个求phi的方式
\(\phi(i)=i\prod_{p是k的质因数}(1-\frac{1}{p})\)
如果有\(p|n\)且\(p^2|n\),则\(\phi(n)=\phi(n/p)\times p\)
如果有\(p|n\)且\(p^2\not|n\),则\(\phi(n)=\phi(n/p)\times (p-1)\)
代码
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#define int long long
using namespace std;
int n,p,t,ans=0;
int pow(int a,int b){
int ans=1;
while(b){
if(b&1)
ans=(ans*a)%p;
a=(a*a)%p;
b>>=1;
}
return ans;
}
int get_phi(int x){
int ans=x;
int top=sqrt(x+0.5);
for(int i=2;i<=top;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;
}
signed main(){
scanf("%d",&t);
p=1000000007;
while(t--){
scanf("%lld",&n);
ans=0;
int top=sqrt(n+0.5);
for(int i=1;i<=top&&i*i!=n;i++){
if(n%i==0){
ans=(ans+pow(n,i)*get_phi(n/i)%p+pow(n,n/i)*get_phi(i)%p)%p;
}
}
if(top*top==n)
ans=(ans+pow(n,top)*get_phi(top)%p)%p;
printf("%lld\n",ans*pow(n,p-2)%p);
}
}
P4980 【模板】Polya定理的更多相关文章
- [洛谷P4980]【模板】Polya定理
题目大意:给一个$n$个点的环染色,有$n$中颜色,问有多少种涂色方案是的旋转后本质不同 题解:$burnside$引理:$ans=\dfrac1{|G|}\sum\limits_{g\in G}A_ ...
- 等价类计数:Burnside引理 & Polya定理
提示: 本文并非严谨的数学分析,有很多地方是自己瞎口胡的,仅供参考.有错误请不吝指出 :p 1. 群 1.1 群的概念 群 \((S,\circ)\) 是一个元素集合 \(S\) 和一种二元运算 $ ...
- [wikioi2926][AHOI2002]黑白瓷砖(Polya定理)
小可可在课余的时候受美术老师的委派从事一项漆绘瓷砖的任务.首先把n(n+1)/2块正六边形瓷砖拼成三角形的形状,右图给出了n=3时拼成的“瓷砖三角形”.然后把每一块瓷砖漆成纯白色或者纯黑色,而且每块瓷 ...
- HDU 3923 Invoker 【裸Polya 定理】
参考了http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 的模板 对于每一种染色,都有一个等价群,例如旋转, ...
- Necklace of Beads (polya定理的引用)
Beads of red, blue or green colors are connected together into a circular necklace of n beads ( n &l ...
- poj1286 Necklace of Beads—— Polya定理
题目:http://poj.org/problem?id=1286 真·Polya定理模板题: 写完以后感觉理解更深刻了呢. 代码如下: #include<iostream> #inclu ...
- poj2154 Color ——Polya定理
题目:http://poj.org/problem?id=2154 今天学了个高端的东西,Polya定理... 此题就是模板,然而还是写了好久好久... 具体看这个博客吧:https://blog.c ...
- Necklace of Beads(polya定理)
http://poj.org/problem?id=1286 题意:求用3种颜色给n个珠子涂色的方案数.polya定理模板题. #include <stdio.h> #include &l ...
- poj 1286 polya定理
Necklace of Beads Description Beads of red, blue or green colors are connected together into a circu ...
随机推荐
- ubuntu安装rvm
sudo apt-get install curl git-core bash -s stable < <(curl -s https://raw.github.com/wayneeseg ...
- C++的类型转换
一.类型转换名称和语法 1.C风格的强制类型转换(Type Cast)很简单,不管什么类型的转换统统是: TYPE b = (TYPE)a 2.C++风格的类型转换提供了4种类型转换操作符来应对 ...
- hdu2262 高斯消元
题目:有一个地图,一个人从某个点出发,问走到花园的期望步数为多少 设某点的期望步数为Ei. 那么目标的Ei=0. Ei=(Enext1+Enext2……Enextk)/k+1. 为什么是这个公式 因为 ...
- Python - 1. Built-in Atomic Data Types
From:http://interactivepython.org/courselib/static/pythonds/Introduction/GettingStartedwithData.html ...
- Javascript创建类的七种方法
/* 第一种定义类的方法 */var cls = new Object();cls.name = "wyf";cls.showName = function(){console.l ...
- 听 Fabien Potencier 谈Symfony2 之 《What is Dependency Injection ?》
听 Fabien Potencier 谈Symfony2 之 <What is Dependency Injection ?> 什么是依赖注入?从PHP实现角度来分析依赖注入,因为PH ...
- LoggerFactory.getLogger用法
使用指定类初始化日志对象,在日志输出的时候,可以打印出日志信息所在类 如:Logger logger = LoggerFactory.getLogger(com.lz.Test.class); ...
- 原生态JDBC
原生态JDBC JDBC(Java DataBase Connectivity,java数据库连接)是一种用于执行SQL语句的Java API.JDBC是java访问数据库的标准规范,可以为不同的关系 ...
- NGINX转发代理情况下,获取客户单真实IP
编译时加上http_realip_module 模块 realip模块生效的前提是:直接连接nginx的ip是在set_real_ip_from中指定的. 原机配置: set_real_ip_from ...
- 汇编语言教材assembly language
https://en.wikipedia.org/wiki/Assembly_language https://baike.baidu.com/item/%E6%B1%87%E7%BC%96%E8%A ...