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 ...
随机推荐
- devstack 部署 openstack(pick/mitaka)
链接出处: https://blog.csdn.net/qiqishuang/article/details/51990662 报错处理出处:https://blog.csdn.net/wang114 ...
- tomcat的JVM调优
1.error场景 Tomcat 长期运行过程遇到Caused by: java.lang.OutOfMemoryError: PermGen space或java.lang.OutOfMemoryE ...
- 【2017-2-23】C#switch case分支语句,for循环语句
switch case分支语句 switch(一个变量值) { case 值:要执行的代码段;break; case 值:要执行的代码段;break; … default:代码段;break;(def ...
- mark_save
class BookConfig(ModelStark): def edit(self, obj=None, is_header=False): if is_header: return " ...
- Mybatis 在 insert 之后想获取自增的主键 id
记录一次傻逼的问题, 自己把自己蠢哭:Mybatis 在 insert 之后想获取自增的主键 id,但却总是返回1 错误说明: 返回的1是影响的行数,并不是自增的主键id: 想要获取自增主键id,需要 ...
- 前端框架VUE----学习vue前的准备工作
起步 1.扎实的HTML/CSS/Javascript基本功,这是前置条件. 2.不要用任何的构建项目工具,只用最简单的<script>,把教程里的例子模仿一遍,理解用法.不推荐上来就直接 ...
- excel 方框打钩
将光标定位于需要打钩的地方,选择[插入]→[符号]→[其他符号] 在弹出的符号栏里,字体一定要改成[Windings2] 然后在符号栏便可以找到现成的打钩样式,点击插入,再关闭即可 提示:如果需要打叉 ...
- script 跳出小窗口
sss
- Golang面向对象_继承
package main import "fmt" type Person struct { name string //名字 sex byte //性别 age int //年龄 ...
- js DOM常见事件
js事件命名为on+动词 1.onclick事件,点击鼠标时触发,ondbclick双击事件 <h1 onclick="this.innerHTML='点击后文本'"> ...