hdu-2865-polya+dp+矩阵+euler函数
Birthday Toy
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 866 Accepted Submission(s): 456
The “Toy” is in bulk and AekdyCoin has to make one by him. Let’s assume that the “Toy” has N small white beads and one Big bead .If someone want to make a “Toy”, he (or she) must always puts the Big bead in center, and then connect the other N small beads around it by using N sticks with equal length, and then the N small beads must be connected by N sticks with equal length, and it could be seen as a regular polygon. Figure 1 shows a “Toy” with 8 small white beads and one big white bead.
Now AekdyCoin has C kinds of available color, say blue, green, yellow, pink …etc. He wants to color these beads, but he thinks that must be too boring and stupid. So he colors these beads with one role: any adjacent beads couldn’t have same color. Figure 2 shows a legal situation, and Figure 3 shows an illegal situation.
It seems that the “Toy” becomes more interesting for AekdyCoin right now; however, he wants to color the big bead in center. Of course, he should follow the role above.
Now AekdyCoin begins to play with the “Toy”, he always colors the big beads and then the other small beads. He should color under the rule above. After several minutes, AekdyCoin finally makes a perfect “Toy”. Figure 4 shows a situation that is under the color rule.
AekdyCoin now want to know the different method to color the “Toy” whit at most K color. (“Toy” contains N small beads and one big bead.)
But, no, the problem is not so easy .The repetitions that are produced by rotation around the center of the circular necklace are all neglected. Figure 5 shows 8 “Toy”, they are regard as one method.
Now AekdyCoin will give you N and K, he wants you to help him calculate the number of different methods, because the number of method is so huge, so AekdyCoin just want you to tell him the remainder when divided by M.
In this problem, M = 1,000,000,007.
Every case has only two integers indicating N, K
(3<=N<=10^9, 4<=K<=10^9)
3 5
3 17
162 78923
40
19040
19469065
N个小珠子加一个大珠子,大珠子放在中间,小的围着她形成一个等分的圆形,有k种颜色,在满足任意相邻珠子都不能同色的情况下的涂色方案数是多少,通过旋转能达到的算作一种方案。
不难想到先给大珠子一个颜色,然后求k-1种颜色涂n个小珠子的方案个数,最后乘上一个k就是答案。
ans=k/n * SUM{ C(g) } ,现在的问题是求C(g) ,也就是不动点个数,在朴素的题目里就是 k^x,但这里要求相邻珠子颜色不同就不能这么做了。先考虑把置换g分解成循环的形势,如果循环个数是1的话,不动点数量应该是0。f(i)=C(i) ,ans=k/n * SUM{f(gcd(i,n) | gcd(i,n)!=1 }
很容易想到对gcd分组利用欧拉函数减少运算次数。然后就是计算f了,这个f代表的问题等价于用k-1种颜色涂一个i珠子形成圆,相邻元素不同的方案数,用dp来求 f[i]=f[i-1]*(k-3)+f[i-2]*(k-2) (i>3) ,一个表示前一个环首尾元素不同,一个表示首尾元素相同,涵盖了所有情况。
n很大,这个递推式要用矩阵幂优化。
注意答案最后要减去gcd(i,n)==1的情况。
#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)
LL mod=1e9+,N,K,F[];
vector<LL>prime;
bool isp[];
struct matrix{
LL a[][];
matrix(){
memset(a,,sizeof(a));
}
matrix operator*(matrix &tmp){
matrix ans;
for(int i=;i<;++i){
for(int j=;j<;++j){
for(int k=;k<;++k){
ans.a[i][j]+=a[i][k]*tmp.a[k][j];
}
ans.a[i][j]%=mod;
}
}
return ans;
}
}A,U;
matrix qpow(matrix A,int b){
matrix ans=U;
while(b){
if(b&) ans=ans*A;
A=A*A;
b>>=;
}
return ans;
}
LL inv(LL n){
if(n<=) return n;
else return (mod-mod/n)*inv(mod%n)%mod;
}
LL phi(LL n){
LL ans=n;
for(int i=;prime[i]<=n;++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;
}
void init(){
for(int i=;i<=;++i){
if(!isp[i]) prime.push_back(i);
for(int j=;j<prime.size()&&i*prime[j]<=;++j){
isp[i*prime[j]]=;
if(i%prime[j]==) break;
}
}
U.a[][]=U.a[][]=;
}
LL f(LL n){
if(n<=)return F[n];
matrix X=qpow(A,n-);
return (X.a[][]*F[]%mod+X.a[][]*F[]%mod)%mod;
}
int main()
{
int t,i,j,k,d;
init();
while(scanf("%lld%lld",&N,&K)!=EOF){ A.a[][]=K-;
A.a[][]=;
A.a[][]=K-;
A.a[][]=;
F[]=K-;
F[]=(K-)*(K-)%mod;
F[]=F[]*(K-)%mod;
LL ans=;
for(i=;i*i<N;++i){
if(N%i==){
ans=(ans+phi(N/i)*f(i))%mod;
ans=(ans+phi(i)*f(N/i))%mod;
}
}
if(i*i==N){
ans=(ans+phi(i)*f(i))%mod;
}
ans=((ans-f()*phi(N))%mod+mod)%mod;
ans=ans*K%mod;
ans=ans*inv(N)%mod;
cout<<ans<<endl;
}
return ;
}
hdu-2865-polya+dp+矩阵+euler函数的更多相关文章
- hdu 2865 Polya计数+(矩阵 or 找规律 求C)
Birthday Toy Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- HDU 2239 polya计数 欧拉函数
这题模数是9937还不是素数,求逆元还得手动求. 项链翻转一样的算一种相当于就是一种类型的置换,那么在n长度内,对于每个i其循环节数为(i,n),但是由于n<=2^32,肯定不能直接枚举,所有考 ...
- HDU 5434 Peace small elephant 状压dp+矩阵快速幂
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5434 Peace small elephant Accepts: 38 Submissions: ...
- HDU 5607 graph(矩阵优化+概率DP)
该题非常easy想到求概率的转移方程:用d[i][j]表示第i步,走到j点的概率. 可是该题的k高达1e9.所以依照套路.要用矩阵相乘来优化. 第一次写矩阵相乘. 大概的意思就是利用矩阵实现递推. 而 ...
- hdu 5868 Polya计数
Different Circle Permutation Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 262144/262144 K ...
- bnuoj 34985 Elegant String DP+矩阵快速幂
题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=34985 We define a kind of strings as elegant s ...
- hdu 4123 树形DP+RMQ
http://acm.hdu.edu.cn/showproblem.php? pid=4123 Problem Description Bob wants to hold a race to enco ...
- hdu 4507 数位dp(求和,求平方和)
http://acm.hdu.edu.cn/showproblem.php?pid=4507 Problem Description 单身! 依旧单身! 吉哥依旧单身! DS级码农吉哥依旧单身! 所以 ...
- hdu 3709 数字dp(小思)
http://acm.hdu.edu.cn/showproblem.php?pid=3709 Problem Description A balanced number is a non-negati ...
随机推荐
- 案例1:写一个压缩字符串的方法,例如aaaabbcxxx,则输出a4b2c1x3。
public static String zipString(String str){ String result = "";//用于拼接新串的变量 char last = str ...
- node.js模块本地代理模块(将自己本机/局域网)服务 代理到外网可以访问
npm 模块 localtunnel模块可以. lt --port 7000 其中7000是本地服务端口 会分配一个动态的url. 局域网中查看本地ip用ifconfg 或者在系统偏好设置里面查看网 ...
- git项目,VSCode显示不同颜色块的含义
一. 概念 代码里的左侧颜色标识: 红色,未加入版本控制; (刚clone到本地) 绿色,已经加入版本控制暂未提交; (新增部分) 蓝色,加入版本控制,已提交,有改动: (修改部分) 白色,加入版本控 ...
- selenium 在电脑浏览器中用手机模式打开
import requests from selenium import webdriver from selenium.webdriver.common.action_chains import A ...
- jmeter线程组之间传参
背景介绍: 使用jmeter做登录和搜索接口的测试: 登录接口请求头为:Content-Type: application/x-www-form-urlencoded; charset=UTF-8 搜 ...
- react面试问题总结
1. 在生命周期中的哪一步你应该发起 AJAX 请求? 我们应当将AJAX 请求放到 componentDidMount 函数中执行,主要原因有下: 放到componentWillMount不好. ...
- Visual Studio常用设置
●Visual Studio 2008以后,修改代码中大括号的颜色:Tools-->Environment-->Fonts and Colors-->右边[Display items ...
- 一个sql实现查询并且插入到另一个表中
两种不同方法,结果不同 方法一.查询的user表中3个元素,name为user表中的字段,1000,0,是往department中要赋的值(给id赋值) ,`name`,' 方法二(推荐使用方法二): ...
- Unity Shader入门精要之 screen post-processing effect
本篇记录了学习Unity Shader入门精要的屏幕后处理的一些知识点. OnRenderImage(RenderTexture src, RenderTexture dest) 以上函数是Unity ...
- 第 6 章 存储 - 043 - data-packed volume container
data-packed volume container 将数据打包到镜像中,然后通过 docker managed volume 共享 1.先用Dockerfile 构建镜像 ADD 将静态文件添加 ...