HDU 2865
和上题一样,但K较大,不能直接用矩阵来写。这个矩阵必定是这个形式的。
0 1 1 1
1 0 1 1
1 1 0 1
1 1 1 0
分成对角线上元素B与非对角线上元素A
k: 1 2 3 4 ...
A: 1 2 7 20...
B: 0 3 6 21...
按上题经验,我们可以知道,B才是要求的。那么
Bn=(k-1)An-1
An=(k-2)An-1+Bn-1
于是可以得到
An=(k-2)An-1+(k-1)*An-2.这里就可以使用到矩阵连乘了。
可以通过A求得B。注意,这里能用的只能是K-1种颜色,因为中间一种颜色已不可用。于是,按上题同样的思路即可。
PS:今天不知为什么,内心特别烦闷。这道题的思路,昨天就想到的,本以为留着今天来写。可是,没想到,一点心情也没有。。。以后也不做这种蠢事,一定要一有思路,马上就写。。。。
贴个别人的代码吧。。(他是求出了Bn的表达式的,但我找不出来)
#include<cstdio>
#include<cstring>
#include<vector>
#define P 1000000007
#define MAXM 2
#define MAXN 32000
typedef long long LL;
using namespace std;
bool p[MAXN];
vector<int> factor;
vector<int> prime;
struct Matrix {
LL mat[MAXM][MAXM];
void Zero() {
memset(mat, 0, sizeof(mat));
}
void Unit() {
Zero();
mat[0][0] = mat[1][1] = 1;
}
void Build(int k) {
Zero();
mat[0][1] = 1;
mat[0][0] = k - 2;
mat[1][0] = k - 1;
}
};
Matrix operator *(Matrix &a, Matrix &b) {
int i, j, k;
Matrix tmp;
tmp.Zero();
for (i = 0; i < MAXM; i++) {
for (j = 0; j < MAXM; j++) {
for (k = 0; k < MAXM; k++)
tmp.mat[i][j] += a.mat[i][k] * b.mat[k][j];
tmp.mat[i][j] %= P;
}
}
return tmp;
}
Matrix operator ^(Matrix a, int k) {
Matrix tmp;
for (tmp.Unit(); k; k >>= 1) {
if (k & 1)
tmp = tmp * a;
a = a * a;
}
return tmp;
}
void Factor(int n) {
int i;
factor.clear();
for (i = 1; i * i < n; i++) {
if (n % i == 0) {
factor.push_back(i);
factor.push_back(n / i);
}
}
if (i * i == n)
factor.push_back(i);
}
LL ExtGcd(LL a, LL b, LL &x, LL &y) {
LL t, d;
if (b == 0) {
x = 1;
y = 0;
return a;
}
d = ExtGcd(b, a % b, x, y);
t = x;
x = y;
y = t - a / b * y;
return d;
}
LL InvMod(LL a, LL n) {
LL x, y;
ExtGcd(a, n, x, y);
return (x % n + n) % n;
}
int Count(int x) {
int res, i;
res = x;
for (i = 0; prime[i] * prime[i] <= x; i++) {
if (x % prime[i] == 0) {
res -= res / prime[i];
while (x % prime[i] == 0)
x /= prime[i];
}
}
if (x > 1)
res -= res / x;
return res;
}
LL F(int n, int k) {
LL res;
if (n == 1)
res = 0;
else if (n == 2)
res = (LL) k * (k - 1);
else if (n == 3)
res = (LL) k * (k - 1) % P * (k - 2);
else {
Matrix g;
g.Build(k);
g = g ^ (n - 3);
res = g.mat[0][0] * k % P * (k - 1) % P * (k - 2);
res += g.mat[1][0] * k % P * (k - 1);
}
return res % P;
}
LL Burnside(int n, int k) {
LL ans;
int i;
Factor(n);
for (i = ans = 0; i < (int) factor.size(); i++) {
ans += F(factor[i], k) * Count(n / factor[i]) % P;
if (ans >= P)
ans -= P;
}
return ans * InvMod(n, P) % P;
}
void Init() {
int i, j;
memset(p, true, sizeof(p));
for (i = 2; i < 180; i++) {
if (p[i]) {
for (j = i * i; j < MAXN; j += i)
p[j] = false;
}
}
prime.clear();
for (i = 2; i < MAXN; i++) {
if (p[i])
prime.push_back(i);
}
}
int main() {
int n, k;
Init();
while (~scanf("%d%d", &n, &k))
printf("%I64d\n", Burnside(n, k - 1) * k % P);
return 0;
}
HDU 2865的更多相关文章
- hdu 2865 Polya计数+(矩阵 or 找规律 求C)
Birthday Toy Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- HDU 2865 Birthday Toy [Polya 矩阵乘法]
传送门 题意: 相邻珠子不能相同,旋转等价.$n$个珠子$k$中颜色,求方案数 首先中间珠子$k$种选择,$k--$如果没有相邻不同的限制,就和$POJ\ 2154$一样了$|C(f)|=k^{\#( ...
- HDU 2865 Birthday Toy
题目链接 题意:n个小珠子组成的正n边形,中间有一个大珠子.有木棍相连的两个珠子不能有相同的颜色,旋转后相同视为相同的方案,求着色方案数. \(\\\) 先选定一种颜色放在中间,剩下的\(k-1\)种 ...
- HDU 5643 King's Game 打表
King's Game 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5643 Description In order to remember hi ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
随机推荐
- rails undefined method error_messages
rails undefined method error_messages 学习了:http://stackoverflow.com/questions/10002140/use-error-mess ...
- SQL语法精讲(包括建库、建表、建视图、查询、增加、删除、)
SQL语法精讲(包括建库.建表.建视图.查询.增加.删除.修改) SQL分类: DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE) DML—数据操纵语言(SELECT,DELE ...
- Common webpart properties in kentico
https://devnet.kentico.com/docs/7_0/devguide/index.html?common_web_part_properties.htm HTML Envelope ...
- Thread.setDaemon设置说明
转载地址:http://blog.csdn.net/m13666368773/article/details/7245570 Thread.setDaemon的用法,经过学习以后了解: 1. setD ...
- docker初安装的血泪史
最近docker很火,不管是朋友圈内还是公司内聊天都离不开docker,于是对docker产生了极大的好奇心,凭着一颗程序猿的好奇心开始了docker的安装血泪史. 首先我有一台从公司退役的本本x22 ...
- Spring《六》管理Bean
BeanWrapper BeanFactory ApplicationContext 1.通常情况下使用BeanFactory.ApplicationContext 2.ApplicationCont ...
- Centos7 minimal 系列之桥接模式联网(二)
一.桥接模式联网 之前用NAT模式连接网络,Centos是可以上网,而且Centos可以ping通主机,但是主机ping不通虚拟机.后来发现Nat模式只能由内而外. 1.1设置虚拟机的网络适配器 1. ...
- PHP函数十进制、二进制、八进制和十六进制转换函数说明
1.十进制转二进制 decbin() 函数,如下实例 echo decbin(12); //输出 1100 echo decbin(26); //输出 11010 2.十进制转八进制 decoct( ...
- log4j:WARN Please initialize the log4j system properly.解决方案
在使用quarz任务调度框架时的错误,实际上这个问题很常见,并不影响程序的使用,只是缺少日志输出,完整错误信息: log4j:WARN No appenders could be found for ...
- Ubuntu 16.04安装Caffe的记录及FCN官方代码的配置
相关内容搜集自官方文档与网络,既无创新性,也不求甚解,我也不了解Caffe,仅仅搭上之后做个记录,方便以后重装 安装依赖项sudo apt-get install libprotobuf-dev li ...