hdu 2814 Interesting Fibonacci
来源:互联网 发布:纸黄金走势图软件 编辑:程序博客网 时间:2024/05/31 19:05
点击此处即可传送 hdu 2814
题目大意:就是给你两个函数,一个是F(n) = F(n-1) + F(n-2),
F(0) = 0, F(1) = 1;
另一个是 G(n) = G(n-1)^F(a^b);
G(1) = F(a^b);
求G(n) % c;
范围:A, B, N, C (10<=A, B<2^64, 2<=N<2^64, 1<=C<=300)
注意了:c的范围是1<= C <= 300,所以说它一定会有循环 节:
解题思路: 首先算G(1) = F(a^b),设a^b的循环节是len;
F(a^b)%c = F(a^b%len)%c;
一边加一边取余
然后算G(n)%c = F(a^b)^(F(a^b)^(n-1)) % c;
G(n)%c = F(a^b)^(F(a^b)^(n-1)%phi(c)+phi(c))%c;
F(a^b)^(n-1)%phi(c)+phi(c) == (F(a^b)%phi(c)^(n-1))+phi(c)
F(a^b)%phi(c) 有循环节,同上,具体详见代码
上代码:
/*2015 - 8 - 16 下午Author: ITAK今日的我要超越昨日的我,明日的我要胜过今日的我,以创作出更好的代码为目标,不断地超越自己。*/#include <iostream>#include <cstdio>using namespace std;//快速幂取余int quick_mod(int a, unsigned long long b, int c){ int ans = 1; a %= c; while(b) { if(b & 1) ans = (ans*a) % c; b >>= 1; a = (a*a) % c; } return ans;}//欧拉函数int Phi(int m){ int ans = m; for(int i=2; i*i<=m; i++) { if(m%i == 0) ans -= ans/i; while(m%i == 0) m /= i; } if(m > 1) ans -= ans/m; return ans;}//公式:x^y % c == x^(y%phi(c)+phi(c))%c;int data[90005],data1[90005];int main(){ //注意不要用long long,用unsigned long long unsigned long long a, b, n; int c, c1, t, n1, n2, tmp; int g[10], len=0, len_c=0, len_e=0; scanf("%d",&t); for(int k=1; k<=t; k++) { //cin>>a>>b>>n>>c; scanf("%lld%lld%lld%d",&a,&b,&n,&c); if(c == 1) { printf("Case %d: 0\n",k); continue; } data[0]=0, data[1]=1; data1[0]=0, data1[1]=1; for(int i=2; i<90005; i++) { data[i] = (data[i-1]+data[i-2])%c; if(data[i]==1 && data[i-1]==0) { len = i-1;//c的循环节 break; } } c1 = Phi(c); if(c1 > 1) { for(int i=2; i<90005; i++) { data1[i] = (data1[i-1]+data1[i-2])%c1; if(data1[i]==1 && data1[i-1]==0) { len_c = i-1;//Phi(c)的循环节 break; } } } n1 = quick_mod(a%len, b, len); g[1] = data[n1]; if(c1 == 1) g[2] = 0; else { n2 = quick_mod(a%len_c, b, len_c); g[2] = data1[n2]; } int ans1 = quick_mod(g[2], n-1, c1); int ans = quick_mod(g[1], ans1+c1, c); if(n == 1) printf("Case %d: %d\n",k,g[1]); else printf("Case %d: %d\n",k,ans); } return 0;}
0 0
- hdu 2814 Interesting Fibonacci
- hdu 2814 Interesting Fibonacci
- hdu 2814 Interesting Fibonacci
- hdu 2814 Interesting Fibonacci
- HDU 2814 Interesting Fibonacci 循环节
- HDU 2814 - Interesting Fibonacci (Fibonacci性质 + 循环节)
- hdu-2814-Interesting Fibonacci-斐波那契循环节
- HDU 3195 Interesting Numbers
- HDU 5785 Interesting
- HDU-Fibonacci
- hdu Fibonacci
- Fibonacci HDU
- HDU Fibonacci
- HDU 2426 Interesting Housing Problem
- HDU-2426 Interesting Housing Problem
- hdu 2426 Interesting Housing Problem
- HDU 2426 Interesting Housing Problem
- HDU 2426 Interesting Housing Problem
- 黑马程序员——ios基础---C语言:循环结构、函数
- 机房收费系统优化之字符限制
- MyEclipse10 安装pydev插件
- 成都七少优化基础之企业网站优化
- 深度学习(DL)与卷积神经网络(CNN)学习笔记随笔-03-基于Python的LeNet之LR
- hdu 2814 Interesting Fibonacci
- String和Date之间的转换(Java)
- I Hate It
- poj 2186 强连通分量个数
- java正则表达式
- 我的第一篇博客
- web.xml 中 classpath 写法说明
- dijstra(迪杰斯特拉)算法的简单实现
- Python生成验证码