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 ...
随机推荐
- NLog——ElasticSearch——Kibana
Nlog.elasticsearch.Kibana以及logstash在项目中的应用(一) Nlog.elasticsearch.Kibana以及logstash在项目中的应用(二) ASP.NET ...
- 洛谷P2637第一次,第二次,成交! 模拟?DP?
今天水来一天,就贴道水吧.. 原题>>https://www.luogu.org/problem/show?pid=2637<< 题目描述 因为奶牛们的节食运动(奶牛还节食?) ...
- Wordpress搭建
Install Environment apt install apache2 php mysql-server apt install php-mysql php-fpm Config mysql ...
- Xshell5中常用linux服务器命令集合
简易版:http://www.zhimengzhe.com/linux/84546.html 详细版:http://www.cnblogs.com/peida/tag/%E6%AF%8F%E6%97% ...
- 使用lombok 找不到方法
在setting里面查找并设置就好了
- DAY1 计算机组成和操作系统
一.编程与编程目的 1.编程语言的定义 编程语言是人与计算机之间沟通的介质 2.什么是编程 编程就是程序员通过编程语言让计算机实现所想做的事 3.编程的目的 解放人力,让计算机按照人的逻辑思维进行工作 ...
- ssh REMOTE HOST IDENTIFICATION HAS CHANGED!
连接到docker的时候,有时因为image重新buid过,就提示 It is also possible that a host key has just been changed. 不让连接. 解 ...
- vue element-ui 日期选择器组件 日期时间格式化
vue element-ui 组件开发大大提高了我们的效率,但有时候并不能满足我们的需求,例如时间,日期组件: element-ui 日期返回的格式是这样的,看下图: 但我们要的是另一个格式 , 如下 ...
- Redis的安装及命令返回值
Linux下安装Reids : http://redis.io/download 下载最新稳定版本 wget http://download.redis.io/releases/redis-3.0.7 ...
- 转载:删除github上文件夹的两种方式
http://www.jianshu.com/p/286be61bb9b8 删除github上文件夹的两种方式(解决已经加入ignore的文件夹无法从远程仓库删除的问题) 如果此文件夹已被加入git追 ...