hdu 2865 Polya计数+(矩阵 or 找规律 求C)
Birthday Toy
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 644 Accepted Submission(s): 326
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
- /*
- hdu 2865 Polya计数+(矩阵 or 找规律 求C)
- 给你n个小球全部连在一个大球上然后对他们进行染色,要求相连的球颜色不一样
- 首先确定大球为一种颜色(k种可能)。然后用剩下k-1种用Polya去处理小球即可
- 在计算循环节长度为i的可能数时由于k很大,矩阵快速幂很明显不行诶
- 1.可以考虑递推,假设第一个颜色是x,用f[i][1]表示当前颜色是x,f[i][0]表示当前颜色非x。
- f[i][1] = f[i-1][0]
- f[i][0] = (k-2)*f[i-1][0]+(k-1)*f[i-1][1]
- 2.假设用3种颜色染循环节长度为len小球,构建出来的矩阵是
- 0 1 1 2 1 1 2 3 3 6 5 5 10 11 11
- 1 0 1 -> 1 2 1 -> 3 2 3 -> 5 6 5 -> 11 10 11
- 1 1 0 1 1 2 3 3 2 5 5 6 11 11 10
- ——参考自cxlove大神.
- 可以发现是有规律的:(2 = 3-1)
- n = 1 -> 0 (2^n - 2)
- n = 2 -> 6 (2^n + 2)
- n = 3 -> 6 (2^n - 2)
- n = 4 -> 18 (2^n + 2)
- n = 5 -> 30 (2^n - 2)
- 代码为注释部分
- hhh-2016-04-22 10:12:35
- */
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- #include <functional>
- using namespace std;
- #define lson (i<<1)
- #define rson ((i<<1)|1)
- typedef long long ll;
- using namespace std;
- const ll mod = 1e9 + 7;
- const int maxn = 40010;
- int num;
- int prime[maxn];
- int isPrime[maxn];
- void get_prime()
- {
- num = 0;
- memset(isPrime,0,sizeof(isPrime));
- for(int i = 2; i <= maxn-10; i++)
- {
- if(!isPrime[i])
- {
- prime[num++] = i;
- for(int j = i+i; j <= maxn-10; j+=i)
- isPrime[j] = 1;
- }
- }
- }
- ll euler(ll cur)
- {
- ll ans = cur;
- ll x = cur;
- for(int i = 0; i < num && prime[i]*prime[i] <= cur; i++)
- {
- if(x % prime[i] == 0)
- {
- ans = ans/prime[i]*(prime[i]-1);
- while(x % prime[i] == 0)
- x /= prime[i];
- }
- }
- if(x > 1)
- {
- ans = ans/x*(x-1);
- }
- return ans%mod;
- }
- ll pow_mod(ll a,ll n)
- {
- ll ret = 1;
- a %= mod;
- while(n)
- {
- if(n & 1) ret = ret*a%mod;
- a = a*a%mod;
- n >>= 1;
- }
- return ret%mod;
- }
- /*
- another:
- ll solve(ll p,ll k)
- {
- ll ans=pow_mod(p-1,k);
- if(k&1)
- ans=(ans+mod-(p-1))%mod;
- else
- ans=(ans+p-1)%mod;
- return ans;
- }
- */
- struct Matrix
- {
- ll ma[3][3];
- Matrix()
- {
- memset(ma,0,sizeof(ma));
- }
- };
- Matrix mult(Matrix ta,Matrix tb)
- {
- Matrix tc;
- for(int i = 0 ; i < 2; i ++)
- {
- for(int j = 0; j < 2; j++)
- {
- for(int k = 0; k < 2; k++)
- tc.ma[i][j] = (tc.ma[i][j] + ta.ma[i][k]*tb.ma[k][j]%mod)%mod;
- }
- }
- return tc;
- }
- Matrix Mat_mod(Matrix a,int n)
- {
- Matrix cnt;
- for(int i = 0; i < 2; i++)
- cnt.ma[i][i] = 1;
- while(n)
- {
- if(n & 1 ) cnt = mult(cnt,a);
- a = mult(a,a);
- n >>= 1;
- }
- return cnt;
- }
- Matrix mat;
- Matrix begi;
- ll solve(ll p,ll k)
- {
- begi.ma[0][1] = 1,begi.ma[0][0] = 0,begi.ma[1][0] = 0,begi.ma[1][1] = 0;
- mat.ma[0][0] = p - 2, mat.ma[1][0] = p - 1, mat.ma[0][1] = 1, mat.ma[1][1] = 0;
- mat = Mat_mod(mat,k-1);
- Matrix tp = mult(begi,mat);
- return p*tp.ma[0][0]%mod;
- }
- ll cal(ll n,ll k)
- {
- ll ans = 0;
- for(int i = 1; i*i <= n; i++)
- {
- if(n % i == 0)
- {
- ans = (ans + solve(k,n/i)*euler(i)%mod)%mod;
- if(n != i*i)
- ans = (ans + solve(k,i)*euler(n/i)%mod)%mod;
- }
- }
- return (ans*pow_mod(n,mod-2))%mod;
- }
- ll N,k;
- int main()
- {
- get_prime();
- while(scanf("%I64d%I64d",&N,&k) != EOF)
- {
- printf("%I64d\n",k*cal(N,k-1)%mod);
- }
- return 0;
- }
hdu 2865 Polya计数+(矩阵 or 找规律 求C)的更多相关文章
- hdu 5868 Polya计数
Different Circle Permutation Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 262144/262144 K ...
- 【poj 3090】Visible Lattice Points(数论--欧拉函数 找规律求前缀和)
题意:问从(0,0)到(x,y)(0≤x, y≤N)的线段没有与其他整数点相交的点数. 解法:只有 gcd(x,y)=1 时才满足条件,问 N 以前所有的合法点的和,就发现和上一题-- [poj 24 ...
- 2018年东北农业大学春季校赛 B wyh的矩阵【找规律】
链接:https://www.nowcoder.com/acm/contest/93/B来源:牛客网 题目描述 给你一个n*n矩阵,按照顺序填入1到n*n的数,例如n=5,该矩阵如下 1 2 3 4 ...
- HDU 4388 Stone Game II 博弈论 找规律
http://acm.hdu.edu.cn/showproblem.php?pid=4388 http://blog.csdn.net/y1196645376/article/details/5214 ...
- HDU 4349 Xiao Ming's Hope 找规律
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4349 Xiao Ming's Hope Time Limit: 2000/1000 MS (Java/ ...
- HDU 4731 Minimum palindrome 打表找规律
http://acm.hdu.edu.cn/showproblem.php?pid=4731 就做了两道...也就这题还能发博客了...虽然也是水题 先暴力DFS打表找规律...发现4个一组循环节.. ...
- HDU 4588 Count The Carries(找规律,模拟)
题目 大意: 求二进制的a加到b的进位数. 思路: 列出前几个2进制,找规律模拟. #include <stdio.h> #include <iostream> #includ ...
- HDU 3032 (SG打表找规律)
题意: 有n堆石子,alice先取,每次可以选择拿走一堆石子中的1~x(该堆石子总数) ,也可以选择将这堆石子分成任意的两堆.alice与bob轮流取,取走最后一个石子的人胜利. 思路: 因为数的范围 ...
- 2017ACM暑期多校联合训练 - Team 1 1011 HDU 6043 KazaQ's Socks (找规律)
题目链接 Problem Description KazaQ wears socks everyday. At the beginning, he has n pairs of socks numbe ...
随机推荐
- 第十一条:谨慎的覆盖clone()方法
一个类要想实现克隆,需要实现Cloneable接口,表明这个类的对象具有克隆的功能. Cloneable接口是一个mixin接口,它里面并没有任何的抽象方法,类似的接口有Serializable接口, ...
- Error contacting service. It is probably not running.
平台:centos-6.3-i386 jdk-7u51 storm 0.9.1 python 2.6.6 hadoop 1.2.1 运行zookeeperd后显示启动成功: JMX enabled ...
- 虚拟机Vmware成功安装Ubuntu Server 16.04中文版
最近想在Linux下学习Python的爬虫开发技术,经过认真考虑优先选择在在Ubuntu环境下进行学习Python的开发,虽然Ubuntu Server 16.04 LTS版本已经集成了Python ...
- 微信浏览器的页面在PC端访问
微信浏览器的页面在PC端访问: 普通的在微信浏览器看的页面如果不在php代码中解析一下,然后复制链接在PC打开就出现无法访问,因为它复制的地址是: https://open.weixin.qq.com ...
- Mego开发文档 - 建模高级主题
建模高级主题 在建模过程中我们还有许多其他情况,这里列出本框架中的有用特性来用于解决此类问题. 函数映射 我们可以将指定的CLR函数映射到数据库中的系统函数或自定义函数,该特性用于补充框架中未提供的数 ...
- python 字符串的方法
capitalize() 把字符串的第一个字符改为大写 casefold() 把整个字符串的所有字符改为小写 center(width) 将字符串居中,并使用空格填充至长度 width 的新字符串 c ...
- HTTP协议扫盲(七)请求报文之 GET、POST-FORM 和 POST-FILE
一.get 1.页面代码 2.请求报文 3.小结 get请求没有报文体,所以请求报文没有content-type url上的query参数param11=val11¶m12=val12 ...
- 新概念英语(1-13)A new dress
What colour is Anna's hat? A:What colour is your new dress? B:It's green.Come upstairs and see it. A ...
- Django form表单
Form介绍 之前在HTML页面中利用form表单向后端提交数据时,都会写一些获取用户输入的标签并且用form标签把它们包起来.与此同时我们在好多场景下都需要对用户的输入做校验,比如校验用户是否输入, ...
- 通过wget工具下载指定文件中的URLs对应的资源并保存到指定的本地目录中去并进行文件完整性与可靠性校验
创建URLs文件在终端输入cd target_directory回车,便把当前文件夹切换到了目标文件夹target_directory,此后创建的文件都会丢它里面在终端输入cat > URLs回 ...