**题意:**有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


/** @Date    : 2016-11-12-19.18

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

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

* @Version :

*/

#include <stdio.h>

#include <iostream>

#include <string.h>

#include <algorithm>

#include <utility>

#include <vector>

#include <map>

#include <set>

#include <string>

#include <stack>

#include <queue>

#define LL long long

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

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

using namespace std;



const int INF = 0x3f3f3f3f;

const int N = 1e5+2000;

const LL mod = 1e9 + 7;







LL gcd(LL a, LL b)

{

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

}



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

{

LL d = a;

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

return -1;

if(b == 0)

{

x = 1;

y = 0;

}

else

{

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

y -= (a / b) * x;

}

return d;

}



LL inv(LL a, LL b)

{

LL x, y;

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

if(d == 1)

return (x % b + b) % b;

else return -1;

}



struct matrix

{

LL mat[2][2];

void init()

{

mat[0][0] = mat[1][0] = mat[0][1] = mat[1][1] = 0;

}

};



matrix mul(matrix a, matrix b)

{

matrix c;

c.init();

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

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

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

{

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

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

}

return c;

}



matrix fpow(matrix x, LL n)

{

matrix r;

r.init();

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

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

while(n > 0)

{

if(n & 1)

r = mul(r, x);

x = mul(x, x);

n >>= 1;

}

return r;

}



LL phi(int x)

{

LL t = x;

LL ans = x;

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

{

if(t % i == 0)

{

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

while(t % i == 0)

{

t /= i;

}

}

}

if(t > 1)

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

return ans;

}



LL fib(int x)

{

matrix t;

t.init();

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

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

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

matrix a;

a = fpow(t, x-1);

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

return ans % mod;

}

int main()

{

LL n;
while(~scanf("%lld", &n))

{

LL ans = 0;

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

{

if(n % i == 0)

{

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

if(n / i != i)

{

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

}

}

}

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



if(n == 1)

printf("2\n");

else

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

}

return 0;

}


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. 自测之Lesson10:管道

    题目:建立双向管道,实现:父进程向子进程传送一个字符串,子进程对该字符串进行处理(小写字母转为大写字母)后再传回父进程. 实现代码: #include <stdio.h> #include ...

  2. java知乎爬虫

    好久没写博客了,前阵子项目忙着上线,现在有点空闲,就把最近写的一个爬虫和大家分享下,统计结果放在了自己买的阿里云服务器上(点此查看效果),效果如下: 程序是在工作之余写的,用了java 的webmgi ...

  3. (一)Model的产生及处理

    MVC的概念其实最早可以追溯到很久很久以前,并不是WEB开发过程中所首创, 但是,MVC也适合WEB上的开发,并真正的在WEB开发领域广泛应用.MVC的第一个字母M是Model,承载着View层和Co ...

  4. MFC消息处理

    1.MFC窗口如何与AfxWndProc建立联系. 当一个新的CWnd派生类创建时,在调用CWnd::CreateEx()过程中,MFC都会安装AfxCbtFilterHook().这个Hook将拦截 ...

  5. Unity3d学习日记(二)

      跟着教程做让背景可以滚动起来并添加了背景的粒子特效,加入了敌机.   ctrl攻击,↑↓←→移动,Game Over后按R重新开始游戏.   Space Shooter游戏地址:http://ya ...

  6. JDK版本Java SE、Java EE、Java ME的区别

    想在win7 X64上搭建JAVA开发环境来着(只是尝试下),打开JAVA 官网下载JDK,发现好多版本懵了,百度了下找到这些版本的区别,故有了下文 1.JAVA SE Java2平台标准版(Java ...

  7. Codeforces Gym 101142 G Gangsters in Central City (lca+dfs序+树状数组+set)

    题意: 树的根节点为水源,编号为 1 .给定编号为 2, 3, 4, …, n 的点的父节点.已知只有叶子节点都是房子. 有 q 个操作,每个操作可以是下列两者之一: + v ,表示编号为 v 的房子 ...

  8. 【题解】HNOI2004敲砖块

    题目传送门:洛谷1437 决定要养成随手记录做过的题目的好习惯呀- 这道题目乍看起来和数字三角形有一点像,但是仔细分析就会发现,因为选定一个数所需要的条件和另一个数所需要的条件会有重复的部分,所以状态 ...

  9. [BZOJ5303] [HAOI2018] 反色游戏

    题目链接 LOJ:https://loj.ac/problem/2524 BZOJ:https://lydsy.com/JudgeOnline/problem.php?id=5303 洛谷:https ...

  10. 2018牛客多校第六场 G.Pikachu

    题意: 给出一棵n个点的树,每条边有边权.对这个树加边变成一个完全图.新加的边的权值为边上两点在树上的距离.求完全图上任意两点的最大流之和. 题解: 一共有C(n,2)个点对.假设当前求s到t之间的最 ...