HDU 3977 斐波那契循环节
这类型的题目其实没什么意思..知道怎么做后,就有固定套路了..而且感觉这东西要出的很难的话,有这种方法解常数会比较大吧..所以一般最多套一些比较简单的直接可以暴力求循环节的题目了..
/** @Date : 2017-09-26 16:37:05
* @FileName: HDU 3977 斐波那契循环节.cpp
* @Platform: Windows
* @Author : Lweleth (SoungEarlf@gmail.com)
* @Link : https://github.com/
* @Version : $Id$
*/
#include <bits/stdc++.h>
#define LL long long
#define PII pair<int ,int>
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std; const int INF = 0x3f3f3f3f;
const double eps = 1e-8; /////////
LL mul(LL x, LL y, LL mod)
{
return (x * y - (LL)(x / (long double)mod * y + 1e-3) * mod + mod) % mod;
} struct Matrix
{
LL m[2][2];
}; Matrix A;
Matrix I = {1, 0, 0, 1}; Matrix multi(Matrix a, Matrix b, LL MOD)
{
Matrix c;
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < 2; j++)
{
c.m[i][j] = 0;
for(int k = 0; k < 2; k++)
c.m[i][j] = (c.m[i][j] % MOD + (a.m[i][k] % MOD) * (b.m[k][j] % MOD) % MOD) % MOD;
c.m[i][j] %= MOD;
}
}
return c;
} Matrix power(Matrix a, LL k, LL MOD)
{
Matrix ans = I, p = a;
while(k)
{
if(k & 1)
{
ans = multi(ans, p, MOD);
k--;
}
k >>= 1;
p = multi(p, p, MOD);
}
return ans;
} LL gcd(LL a, LL b)
{
return b ? gcd(b, a % b) : a;
} const int N = 400005;
const int NN = 5005; LL num[NN], pri[NN];
LL fac[NN];
int cnt, c; bool prime[N];
int p[N];
int k; void isprime()
{
k = 0;
memset(prime, true, sizeof(prime));
for(int i = 2; i < N; i++)
{
if(prime[i])
{
p[k++] = i;
for(int j = i + i; j < N; j += i)
prime[j] = false;
}
}
} LL fpow(LL a, LL b, LL m)
{
LL ans = 1;
a %= m;
while(b)
{
if(b & 1)
{
ans = mul(ans, a , m);
b--;
}
b >>= 1;
a = mul(a , a , m);
}
return ans;
} LL legendre(LL a, LL p)
{
if(fpow(a, (p - 1) >> 1, p) == 1)
return 1;
else return -1;
} void Solve(LL n, LL pri[], LL num[])
{
cnt = 0;
LL t = (LL)sqrt(1.0 * n);
for(int i = 0; p[i] <= t; i++)
{
if(n % p[i] == 0)
{
int a = 0;
pri[cnt] = p[i];
while(n % p[i] == 0)
{
a++;
n /= p[i];
}
num[cnt] = a;
cnt++;
}
}
if(n > 1)
{
pri[cnt] = n;
num[cnt] = 1;
cnt++;
}
} void Work(LL n)
{
c = 0;
LL t = (LL)sqrt(1.0 * n);
for(int i = 1; i <= t; i++)
{
if(n % i == 0)
{
if(i * i == n) fac[c++] = i;
else
{
fac[c++] = i;
fac[c++] = n / i;
}
}
}
} LL get_loop(LL n)
{
Solve(n, pri, num);
LL ans = 1;
for(int i = 0; i < cnt; i++)
{
LL record = 1;
if(pri[i] == 2)
record = 3;
else if(pri[i] == 3)
record = 8;
else if(pri[i] == 5)
record = 20;
else
{
if(legendre(5, pri[i]) == 1)
Work(pri[i] - 1);
else
Work(2 * (pri[i] + 1));
sort(fac, fac + c);
for(int k = 0; k < c; k++)
{
Matrix a = power(A, fac[k] - 1, pri[i]);
LL x = (a.m[0][0] % pri[i] + a.m[0][1] % pri[i]) % pri[i];
LL y = (a.m[1][0] % pri[i] + a.m[1][1] % pri[i]) % pri[i];
if(x == 1 && y == 0)
{
record = fac[k];
break;
}
}
}
for(int k = 1; k < num[i]; k++)
record *= pri[i];
ans = ans / gcd(ans, record) * record;
}
return ans;
} LL fib[5005]; void Init()
{
A.m[0][0] = 1;
A.m[0][1] = 1;
A.m[1][0] = 1;
A.m[1][1] = 0;
fib[0] = 0;
fib[1] = 1;
for(int i = 2; i < 5005; i++)
fib[i] = fib[i - 1] + fib[i - 2];
}
//////////
int main()
{
Init();
isprime();
int T;
cin >> T;
int icas = 0;
while(T--)
{
LL n;
cin >> n;
LL ans = get_loop(n);
printf("Case #%d: %lld\n", ++icas, ans);
}
return 0;
}
HDU 3977 斐波那契循环节的更多相关文章
- HDu4794 斐波那契循环节
题意:Arnold变换把矩阵(x,y)变成((x+y)%n,(x+2*y)%n),问最小循环节 题解:仔细算前几项能看出是斐波那契数论modn,然后套个斐波那契循环节板子即可 //#pragma GC ...
- HDU 2814 斐波那契循环节 欧拉降幂
一看就是欧拉降幂,问题是怎么求$fib(a^b)$,C给的那么小显然还是要找循环节.数据范围出的很那啥..unsigned long long注意用防爆的乘法 /** @Date : 2017-09- ...
- HDU 2855 斐波那契+矩阵快速幂
http://acm.hdu.edu.cn/showproblem.php?pid=2855 化简这个公式,多写出几组就会发现规律 d[n]=F[2*n] 后面的任务就是矩阵快速幂拍一个斐波那契模板出 ...
- HDU 2516 斐波那契博弈
点这里去看题 n为斐波那契数时,先手败,推断方法见算法讲堂 #include<bits/stdc++.h> using namespace std; int main() { ],i,n, ...
- HDU 1021(斐波那契数与因子3 **)
题意是说在给定的一种满足每一项等于前两项之和的数列中,判断第 n 项的数字是否为 3 的倍数. 斐波那契数在到第四十多位的时候就会超出 int 存储范围,但是题目问的是是否为 3 的倍数,也就是模 3 ...
- hdu 5914(斐波拉契数列)
Triangle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- HDU——4549M斐波那契数列(矩阵快速幂+快速幂+费马小定理)
M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Total Su ...
- HDU 5686 斐波那契数列、Java求大数
原题:http://acm.hdu.edu.cn/showproblem.php?pid=5686 当我们要求f[n]时,可以考虑为前n-1个1的情况有加了一个1. 此时有两种情况:当不适用第n个1进 ...
- HDU 1021 斐波那契
参考自:https://www.cnblogs.com/ECJTUACM-873284962/p/6404504.html Fibonacci Again Time Limit: 2000/1000 ...
随机推荐
- ASP.NET MVC 4 内容映射
文章:ASP.NET MVC 4 内容映射 地址:https://msdn.microsoft.com/zh-cn/library/gg416514(v=vs.108).aspx 模型-视图-控制器 ...
- lintcode-408-二进制求和
408-二进制求和 给定两个二进制字符串,返回他们的和(用二进制表示). 样例 a = 11 b = 1 返回 100 标签 二进制 字符串处理 脸书 思路 先相加,在处理进位,为了方便操作,将选字符 ...
- Spring管理过滤器:org.springframework.web.filter.DelegatingFilterProxy
配置web.xml <filter> <filter-name>springSecurityFilterChain</filter-name> ...
- TCP系列42—拥塞控制—5、Linux中的慢启动和拥塞避免(二)
在本篇中我们继续上一篇文章wireshark的示例讲解,上一篇介绍了一个综合示例后,本篇介绍一些简单的示例,在读本篇前建议先把上一篇读完,为了节省篇幅,本篇只针对一些特殊的场景点报文进行讲解,不会像上 ...
- 【Leetcode】 328. Odd Even Linked List
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note her ...
- nodejs 调试
什么语言入门的准备功能就是写helloworld, 调试. 用惯了chrome的话,推荐用chrome自带的调试器来调试.很方便. 在地址栏中输入 chrome://inspect 并按回车,会打开如 ...
- 九度-题目1195:最长&最短文本
http://ac.jobdu.com/problem.php?pid=1195 题目描述: 输入多行字符串,请按照原文本中的顺序输出其中最短和最长的字符串,如果最短和最长的字符串不止一个,请全部输出 ...
- 【C++】深度探索C++对象模型读书笔记--关于对象(Object Lessons)
前言中的内容: 1.什么是C++对象模型? 1.语言中直接支持面向对象程序设计的部分 2. 对于各种支持的底层实现机制 2. C++ class的完整virtual functions在编译时期就固定 ...
- Struts按着配置文件的加载的顺序,后面文件和前面文件相同的配置,后面的会把前面的文件的值覆盖
Struts按着配置文件的加载的顺序,后面文件和前面文件相同的配置,后面的会把前面的文件的值覆盖
- 【JavaScript&jQuery】购物车自动结算
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...