思路

polya定理的模板题,但是还要加一些优化

题目的答案就是

\[\frac{\sum_{i=1}^n n^{gcd(i,n)}}{n}
\]

考虑上方的式子怎么求

因为\(gcd(i,n)\)肯定有很多重复,枚举\(gcd(i,n)\),因为\(gcd(i,n)\)是\(n\)的约数,所以枚举约数

\[\begin{align}&\sum_{d|n}^nn^d\sum_{k=1}^n[gcd(n,k)=d]\\=&\sum_{d|n}^nn^d\sum_{k=1}^{\lfloor\frac{n}{d}\rfloor}[gcd(\lfloor\frac{n}{d}\rfloor,k)=1]\\=&\sum_{d|n}^nn^d\phi(\lfloor\frac{n}{d}\rfloor) \end{align}
\]

然后做完了

一个求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定理的更多相关文章

  1. [洛谷P4980]【模板】Polya定理

    题目大意:给一个$n$个点的环染色,有$n$中颜色,问有多少种涂色方案是的旋转后本质不同 题解:$burnside$引理:$ans=\dfrac1{|G|}\sum\limits_{g\in G}A_ ...

  2. 等价类计数:Burnside引理 & Polya定理

    提示: 本文并非严谨的数学分析,有很多地方是自己瞎口胡的,仅供参考.有错误请不吝指出 :p 1. 群 1.1 群的概念 群 \((S,\circ)\) 是一个元素集合 \(S\) 和一种二元运算 $ ...

  3. [wikioi2926][AHOI2002]黑白瓷砖(Polya定理)

    小可可在课余的时候受美术老师的委派从事一项漆绘瓷砖的任务.首先把n(n+1)/2块正六边形瓷砖拼成三角形的形状,右图给出了n=3时拼成的“瓷砖三角形”.然后把每一块瓷砖漆成纯白色或者纯黑色,而且每块瓷 ...

  4. HDU 3923 Invoker 【裸Polya 定理】

    参考了http://blog.csdn.net/ACM_cxlove?viewmode=contents           by---cxlove 的模板 对于每一种染色,都有一个等价群,例如旋转, ...

  5. Necklace of Beads (polya定理的引用)

    Beads of red, blue or green colors are connected together into a circular necklace of n beads ( n &l ...

  6. poj1286 Necklace of Beads—— Polya定理

    题目:http://poj.org/problem?id=1286 真·Polya定理模板题: 写完以后感觉理解更深刻了呢. 代码如下: #include<iostream> #inclu ...

  7. poj2154 Color ——Polya定理

    题目:http://poj.org/problem?id=2154 今天学了个高端的东西,Polya定理... 此题就是模板,然而还是写了好久好久... 具体看这个博客吧:https://blog.c ...

  8. Necklace of Beads(polya定理)

    http://poj.org/problem?id=1286 题意:求用3种颜色给n个珠子涂色的方案数.polya定理模板题. #include <stdio.h> #include &l ...

  9. poj 1286 polya定理

    Necklace of Beads Description Beads of red, blue or green colors are connected together into a circu ...

随机推荐

  1. SiteCore Experience Analytics-体验分析

    体验分析   Sitecore Experience Analytics为营销人员和营销分析师提供仪表板和报告,以识别从其网站和可能的其他外部数据源收集的体验数据的模式和趋势. 体验分析报告示例:   ...

  2. 缓存 Memached

    https://github.com/enyim/EnyimMemcached http://www.newasp.net/soft/63735.html#downloaded/ http://blo ...

  3. python 将文件描述符包装成文件对象

    有一个对应于操作系统上一个已打开的I/O 通道(比如文件.管道.套接字等)的整型文件描述符,你想将它包装成一个更高层的Python 文件对象. 一个文件描述符和一个打开的普通文件是不一样的.文件描述符 ...

  4. PLSA主题模型

    主题模型 主题模型这样理解一篇文章的生成过程: 1.          确定文章的K个主题. 2.          重复选择K个主题之一,按主题-词语概率生成词语. 3.          所有词语 ...

  5. highchart应用示例2-上:圆角柱状图,下:多指标曲线图

    1.ajax调用接口获取数据 function getCityData() { var date1 = $('#datetimepicker1').val(); var date2 = $('#dat ...

  6. POJ 1789 Truck History (Kruskal 最小生成树)

    题目链接:http://poj.org/problem?id=1789 Advanced Cargo Movement, Ltd. uses trucks of different types. So ...

  7. JustOj 1486: Hello, world!

    题目描述 This is the first problem for test. Since all we know the ASCII code, your job is simple: Input ...

  8. mongoose与express

    一.mongoose的使用1.先创建一个项目目录,初始化:npm init -y2.创建一个server.js文件,在该目录下安装mongoose:cnpm install mongoose3.引入m ...

  9. 从实践出发:微服务布道师告诉你Spring Cloud与Spring Boot他如何选择

    背景 随着公司业务量的飞速发展,平台面临的挑战已经远远大于业务,需求量不断增加,技术人员数量增加,面临的复杂度也大大增加.在这个背景下,平台的技术架构也完成了从传统的单体应用到微服务化的演进. 系统架 ...

  10. RedisLive安装

    环境安装 Python2.7 [root@ ~]# yum install -y readline readline-devel [root@ ~]# yum install sqlite-devel ...