思路

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. fzu2204 dp

    2015-10-06 19:31:05 n个有标号的球围成一个圈.每个球有两种颜色可以选择黑或白染色.问有多少种方案使得没有出现连续白球7个或连续黑球7个. 每组包含n,表示球的个数.(1 <= ...

  2. 安装vm tools时出现如下问题 The path "/usr/bin/gcc" is not valid path to the

    sudo suapt-get updateapt-get dist-upgradeapt-get install open-vm-tools-desktop fusereboot https://bl ...

  3. 了解一下UTF-16

    1)先啰嗦一下 UTF-16是一种编码格式.啥是编码格式?就是怎么存储,也就是存储的方式. 存储啥?存二进制数字.为啥要存二进制数字? 因为Unicode字符集里面把二进制数字和字符一一对应了,存二进 ...

  4. python os.path.expanduser()

    # Expand the user's home directory

  5. qt5 移植 交叉编译出现错误

    类似这样的错误,当时没有完整的记下来,undefined reference to `std::__detail::_List_node_base@GLIBCXX_3.4.10 当时是在编译qt5cl ...

  6. MongoDB With Spark遇到的2个错误,不能初始化和sample重复的key

    1.$sample stage could not find a non-duplicate document while using a random cursor 这个问题比较难解决,因为我用mo ...

  7. 原生JavaScript插件编写指南(转载)

    原生js开发指南 https://www.jianshu.com/p/e65c246beac1 在jQuery大量使用的环境下,目前网上的众多jQuery插件也能基本满足要求,但是在项目具体需求下,有 ...

  8. Bluetooth_FTP_SPEC: 蓝牙FTP介绍

    FTP(Bluetooth File Transfer Profile) defines howfolders and files on a server device can be browsed ...

  9. What’s WOYO PDR-007 Paintless Dent Repair Heat Induction?

    when you car body with dent, which tools can fixing this problem? is there a device which with the c ...

  10. Centos7 安装 apache + php7.0 环境

    安装apache rpm -qa|grep httpd   查看是否安装 yum install httpd  安装 service httpd start  启动服务 测试是否 启动       I ...