**题意:**有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. Python-列表练习

    1.使用列表生成式生成如下列表:[1,9,25,49,81] s = [i**2 for i in range(1,10)if i%2==1] print(s) 2.输入一个由英文单词组成的字符串(分 ...

  2. tomcat端口号修改

    修改Tomcat的端口号: 在默认情况下,tomcat的端口是8080,如果出现8080端口号冲突,用如下方法可以修改Tomcat的端口号: 首先: 在Tomcat的根(安装)目录下,有一个conf文 ...

  3. Swift-枚举enum理解

    //定义一个枚举 //枚举的语法,enum开头,每一行成员的定义使用case关键字开头,一行可以定义多个关键字 enum CompassPoint { case North case South ca ...

  4. sql sever误删数据库

    在sql sever 2008 r2中,我想把一个数据库添加到DATA中,结果发现被占用,我就打算解除占用后再进行复制,本来应该先是让数据库脱离,再复制,结果,我自作聪明地右键数据库,选择了删除,结果 ...

  5. dedecms给原模型添加新字段

    1.进入dedecms后台 2.点击核心=>频道模型=>内容模型管理(在这里可以看到dedecms预设的模型设置) 3.选中我们需要的模型,点击更改,跳入以下页面 4.点击字段管理(可以看 ...

  6. MYsql 数据库密码忘记(Window)-2(mysql 5.7)

    很久没用Mysql了,再次打开,发现用不了了,密码忘了,服务也无法打开,在cmd中输入mysql之后,显示不是内部指令. 看来问题是mysql服务打不开了 (1)在cmd中 输入net start m ...

  7. intelliJ IDEA最常用的快捷键

    一.使用相关快捷键 1.重写接口实现类:Ctrl+I 2.搜索:Shift+Shift   3.生成get或set方法快捷键:Alt+insert: 4.导入未实现的方法,强制类型转换:Alt+Ent ...

  8. BZOJ 1787 紧急集合(LCA)

    转换成抽象模型,就是要求一棵树(N个点,有N-1条边表示这个图是棵树)中某一点满足给定三点a,b,c到某一点的距离和最小.那么我们想到最近公共祖先的定义,推出只有集合点在LCA(a,b).LCA(a, ...

  9. Python Collections详解

    Python Collections详解 collections模块在内置数据结构(list.tuple.dict.set)的基础上,提供了几个额外的数据结构:ChainMap.Counter.deq ...

  10. vue-cli项目打包出现空白页和路径错误问题

    vue-cli项目打包: 1. 命令行输入:npm  run  build 打包出来后项目中就会多了一个文件夹dist,这就是我们打包过后的项目. 第一个问题,文件引用路径.我们直接运行打包后的文件夹 ...