**题意:**有N个座位,人可以选座位,但选的座位不能相邻,且旋转不同构的坐法有几种。如4个座位有3种做法。\\( 1≤N≤1000000000 (10^9) \\).
**题解:**首先考虑座位不相邻的选法问题,如果不考虑同构,可以发现其种数是一类斐波那契函数,只不过fib(1)是1 fib(2)是3。
由于n很大,所以使用矩阵快速幂来求fib。
再者考虑到旋转同构问题,枚举旋转**i (2π/n) **度,其等价类即\\( gcd(i, n) \\)种,那么可以得$$S(n)=\frac{1}{n}\sum_{d|n}^{n}{fib(gcd(d,n))}$$
这样枚举d即可,在此之上公式还可简化成 $$S(n)=\frac{1}{n}\sum_{d|n}^{n}{fib(d)\varphi(\frac{n}{d}) }$$
而枚举因子时,注意优化,得到因子i时可以顺带得到因子n/i,不然TLE...

最后使用EXGCD求1/n的乘法逆元。

还有需要考虑一个问题,当n=1时,答案是2,而fib(1)值为1,所以需要特判一下。

这道题综合的东西还蛮多的,刚好最近都在学这些,不错的题目/.



求欧拉函数时一个地方写错了查了好久T.T

  1.  
  2. /** @Date : 2016-11-12-19.18

  3. * @Author : Lweleth (SoungEarlf@gmail.com)

  4. * @Link : https://github.com/

  5. * @Version :

  6. */

  7. #include <stdio.h>

  8. #include <iostream>

  9. #include <string.h>

  10. #include <algorithm>

  11. #include <utility>

  12. #include <vector>

  13. #include <map>

  14. #include <set>

  15. #include <string>

  16. #include <stack>

  17. #include <queue>

  18. #define LL long long

  19. #define MMF(x) memset((x),0,sizeof(x))

  20. #define MMI(x) memset((x), INF, sizeof(x))

  21. using namespace std;



  22. const int INF = 0x3f3f3f3f;

  23. const int N = 1e5+2000;

  24. const LL mod = 1e9 + 7;







  25. LL gcd(LL a, LL b)

  26. {

  27. return b?gcd(b, a % b):a;

  28. }



  29. LL exgcd(LL a, LL b, LL &x, LL &y)

  30. {

  31. LL d = a;

  32. if(a == 0 && b == 0)

  33. return -1;

  34. if(b == 0)

  35. {

  36. x = 1;

  37. y = 0;

  38. }

  39. else

  40. {

  41. d = exgcd(b, a % b, y, x);

  42. y -= (a / b) * x;

  43. }

  44. return d;

  45. }



  46. LL inv(LL a, LL b)

  47. {

  48. LL x, y;

  49. LL d = exgcd(a, b, x, y);

  50. if(d == 1)

  51. return (x % b + b) % b;

  52. else return -1;

  53. }



  54. struct matrix

  55. {

  56. LL mat[2][2];

  57. void init()

  58. {

  59. mat[0][0] = mat[1][0] = mat[0][1] = mat[1][1] = 0;

  60. }

  61. };



  62. matrix mul(matrix a, matrix b)

  63. {

  64. matrix c;

  65. c.init();

  66. for(int i = 0; i < 2; i++)

  67. for(int j = 0; j < 2; j++)

  68. for(int k = 0; k < 2; k++)

  69. {

  70. c.mat[i][j] += a.mat[i][k] * b.mat[k][j];

  71. c.mat[i][j] %= mod;

  72. }

  73. return c;

  74. }



  75. matrix fpow(matrix x, LL n)

  76. {

  77. matrix r;

  78. r.init();

  79. for(int i = 0; i < 2; i++)

  80. r.mat[i][i] = 1;

  81. while(n > 0)

  82. {

  83. if(n & 1)

  84. r = mul(r, x);

  85. x = mul(x, x);

  86. n >>= 1;

  87. }

  88. return r;

  89. }



  90. LL phi(int x)

  91. {

  92. LL t = x;

  93. LL ans = x;

  94. for(int i = 2; i * i <= t; i++)

  95. {

  96. if(t % i == 0)

  97. {

  98. ans = ans / i * (i - 1);

  99. while(t % i == 0)

  100. {

  101. t /= i;

  102. }

  103. }

  104. }

  105. if(t > 1)

  106. ans = ans/t * (t-1);

  107. return ans;

  108. }



  109. LL fib(int x)

  110. {

  111. matrix t;

  112. t.init();

  113. t.mat[0][0] = 1;

  114. t.mat[0][1] = 1;

  115. t.mat[1][0] = 1;

  116. matrix a;

  117. a = fpow(t, x-1);

  118. LL ans = a.mat[1][0] * 3 + a.mat[1][1];

  119. return ans % mod;

  120. }

  121. int main()

  122. {

  123. LL n;
  124.  
  125. while(~scanf("%lld", &n))

  126. {

  127. LL ans = 0;

  128. for(int i = 1; i * i <= n; i++)//枚举因子优化

  129. {

  130. if(n % i == 0)

  131. {

  132. ans = (ans + phi(n/i)*fib(i)) % mod;

  133. if(n / i != i)

  134. {

  135. ans = (ans + phi(i)*fib(n/i)) % mod;

  136. }

  137. }

  138. }

  139. ans = ans * inv(n, mod) % mod;



  140. if(n == 1)

  141. printf("2\n");

  142. else

  143. printf("%lld\n", ans);

  144. }

  145. return 0;

  146. }


HDU 5868 Different Circle Permutation Burnside引理+矩阵快速幂+逆元的更多相关文章

  1. HDU 5868 Different Circle Permutation(burnside 引理)

    HDU 5868 Different Circle Permutation(burnside 引理) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=586 ...

  2. HDU 5868 Different Circle Permutation

    公式,矩阵快速幂,欧拉函数,乘法逆元. $an{s_n} = \frac{1}{n}\sum\limits_{d|n} {\left[ {phi(\frac{n}{d})×\left( {fib(d ...

  3. (hdu 6030) Happy Necklace 找规律+矩阵快速幂

    题目链接 :http://acm.hdu.edu.cn/showproblem.php?pid=6030 Problem Description Little Q wants to buy a nec ...

  4. HDU 1757 A Simple Math Problem(矩阵快速幂)

    题目链接 题意 :给你m和k, 让你求f(k)%m.如果k<10,f(k) = k,否则 f(k) = a0 * f(k-1) + a1 * f(k-2) + a2 * f(k-3) + …… ...

  5. HDU 5950 Recursive sequence 【递推+矩阵快速幂】 (2016ACM/ICPC亚洲区沈阳站)

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  6. hdu 2604 Queuing dp找规律 然后矩阵快速幂。坑!!

    http://acm.hdu.edu.cn/showproblem.php?pid=2604 这题居然O(9 * L)的dp过不了,TLE,  更重要的是找出规律后,O(n)递推也过不了,TLE,一定 ...

  7. hdu 4291 2012成都赛区网络赛 矩阵快速幂 ***

    分析:假设g(g(g(n)))=g(x),x可能非常大,但是由于mod 10^9+7,所以可以求出x的循环节 求出x的循环节后,假设g(g(g(n)))=g(x)=g(g(y)),即x=g(y),y也 ...

  8. hdu 1757 A Simple Math Problem (矩阵快速幂,简单)

    题目 也是和LightOJ 1096 和LightOJ 1065 差不多的简单题目. #include<stdio.h> #include<string.h> #include ...

  9. 2017ACM暑期多校联合训练 - Team 2 1006 HDU 6050 Funny Function (找规律 矩阵快速幂)

    题目链接 Problem Description Function Fx,ysatisfies: For given integers N and M,calculate Fm,1 modulo 1e ...

随机推荐

  1. 第八章 IO库

    8.1&&8.2 #include <iostream> #include <vector> #include <string> using nam ...

  2. c语言中反转字符串的函数strrev(), reverse()

    1.使用string.h中的strrev函数 #include<stdio.h> #include<string.h> int main() { char s[]=" ...

  3. “Hello world!”团队—团队选题展示(视频展示说明)

    本次博客的主要内容基本分为以下两方面: 一.视频截图展示 二.视频简要说明 博客内容展示: 视频截图1: 简要说明:这是组长在视频前期简要介绍我们这款游戏项目的内容.从可行性和需求市场方面进行了简要阐 ...

  4. 学霸系统UI项目功能说明书 v1.0版本

    发布人员:软件工程实践小队. 发布内容:学霸系统UI项目说明书. 版本:学霸v1.0版本. 学霸系统UI项目说明书 v1.0版本分为以下部分: Part 1:用户须知: Part 2:功能实现: Pa ...

  5. LintCode-212.空格替换

    空格替换 设计一种方法,将一个字符串中的所有空格替换成 %20 .你可以假设该字符串有足够的空间来加入新的字符,且你得到的是"真实的"字符长度. 你的程序还需要返回被替换后的字符串 ...

  6. 转 【.NET平台下使用MongoDB入门教程】

    目录 一.了解MongoDB 二.MongoDB特点 三.安装及常用命令 3.1 下载安装 3.2 启动服务器 3.3 常用操作 3.4 其他命令 3.5 做成windows服务 四.批处理程序开启M ...

  7. 使用Xcode进行调试

    目录 知己知彼 百战不殆抽刀断Bug 普通操作 全局断点(Global BreakPoint) 条件断点(Condational Breakpoints)打印的艺术 NSLog 开启僵尸对象(Enab ...

  8. BZOJ 1227 虔诚的墓主人(离散化+树状数组)

    题目中矩形的尺寸太大,导致墓地的数目太多,如果我们统计每一个墓地的虔诚度,超时是一定的. 而常青树的数目<=1e5.这启发我们从树的方向去思考. 考虑一行没有树的情况,显然这一行的墓地的虔诚度之 ...

  9. 【bzoj1029】[JSOI2007]建筑抢修 贪心+堆

    题目描述 小刚在玩JSOI提供的一个称之为“建筑抢修”的电脑游戏:经过了一场激烈的战斗,T部落消灭了所有z部落的入侵者.但是T部落的基地里已经有N个建筑设施受到了严重的损伤,如果不尽快修复的话,这些建 ...

  10. AOP拦截+权限验证+返回默认接口对象

    接口如:public IList<string> TestAOP(string token); public IMethodReturn Invoke(IMethodInvocation ...