HDU 2814 斐波那契数列的循环节问题
2014-07-26 19:54
309 查看
http://acm.hdu.edu.cn/showproblem.php?pid=2814
Problem Description
In mathematics, the Fibonacci numbers are a sequence of numbers named after Leonardo of Pisa, known as Fibonacci (a contraction of filius Bonaccio, "son of Bonaccio"). Fibonacci's 1202 book Liber Abaci introduced the sequence to Western European mathematics,
although the sequence had been previously described in Indian mathematics.
The first number of the sequence is 0, the second number is 1, and each subsequent number is equal to the sum of the previous two numbers of the sequence itself, yielding the sequence 0, 1, 1, 2, 3, 5, 8, etc. In mathematical terms, it is defined by the following
recurrence relation:
That is, after two starting values, each number is the sum of the two preceding numbers. The first Fibonacci numbers (sequence A000045 in OEIS), also denoted as F
;
F
can be calculate exactly by the following two expressions:
A Fibonacci spiral created by drawing arcs connecting the opposite corners of squares in the Fibonacci tiling; this one uses squares of sizes 1, 1, 2, 3, 5, 8, 13, 21, and 34;
So you can see how interesting the Fibonacci number is.
Now AekdyCoin denote a function G(n)
Now your task is quite easy, just help AekdyCoin to calculate the value of G (n) mod C
Input
The input consists of T test cases. The number of test cases (T is given in the first line of the input. Each test case begins with a line containing A, B, N, C (10<=A, B<2^64, 2<=N<2^64, 1<=C<=300)
Output
For each test case, print a line containing the test case number( beginning with 1) followed by a integer which is the value of G(N) mod C
Sample Input
1
17 18446744073709551615 1998 139
Sample Output
Case 1: 120
解题思路:
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
#define max 400
int quick_mod(int a,unsigned long long b, int c)//快速幂
{
int ans=1;
a=a%c;
while(b>0)
{
if (b%2==1)
ans=(ans*a)%c;
b=b/2;
a=(a*a)%c;
}
return ans;
}
int eular(int n) //欧拉函数
{
int ret=1,i;
for (i=2; i*i<=n; i++)
{
if (n%i==0)
{
n/=i,ret*=i-1;
while (n%i==0)
n/=i,ret*=i;
}
}
if (n>1)
ret*=n-1;
return ret;
}
unsigned long long a,b,n;
int c;
int data1[9003];
int data2[9003];
int g[9003];
int len,len_c,len_e;
int main()
{
int T,tt=0;
scanf("%d",&T);
while(T--)
{
scanf("%I64d%I64d%I64d%d",&a,&b,&n,&c);
if(c==1)
{
printf("Case %d: 0\n",++tt);
continue;
}
data1[0]=0,data1[1]=1;
for(int i=2;; i++)
{
data1[i]=(data1[i-1]+data1[i-2])%c;
if(data1[i]==1&&data1[i-1]==0)
{
len=i-1;
break;
}
}
int o=eular(c);
if(o==1)
len_c=0;
else
{
data2[0]=0,data2[1]=1;
for(int i=2;;i++)
{
data2[i]=(data2[i-1]+data2[i-2])%o;
if(data2[i]==1&&data2[i-1]==0)
{
len_c=i-1;
break;
}
}
}
int n1=quick_mod(a%len,b,len);
g[1]=data1[n1];
int mi_zhi_shu;
int n2;
if(len_c==0)
mi_zhi_shu=0;
else
{
n2=quick_mod(a%len_c,b,len_c);
mi_zhi_shu=data2[n2];
}
int x=quick_mod(mi_zhi_shu,n-1,o);
x+=o;
x=quick_mod(g[1],x,c);
if(n==1)
printf("Case %d: %d\n",++tt,g[1]);
else
printf("Case %d: %d\n",++tt,x);
}
return 0;
}
解法二: 在将n从1~max列举出来,在找循环节,陈老师是这么做的。
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
#define max 400
int quick_mod(int a,unsigned long long b, int c)//快速幂
{
int ans=1;
a=a%c;
while(b>0)
{
if (b%2==1)
ans=(ans*a)%c;
b=b/2;
a=(a*a)%c;
}
return ans;
}
int eular(int n) //欧拉函数
{
int ret=1,i;
for (i=2; i*i<=n; i++)
{
if (n%i==0)
{
n/=i,ret*=i-1;
while (n%i==0)
n/=i,ret*=i;
}
}
if (n>1)
ret*=n-1;
return ret;
}
unsigned long long a,b,n;
int c;
int data1[9003];
int data2[9003];
int g[9003];
int len,len_c,len_e;
int main()
{
int T,tt=0;
scanf("%d",&T);
while(T--)
{
scanf("%I64d%I64d%I64d%d",&a,&b,&n,&c);
if(c==1)
{
printf("Case %d: 0\n",++tt);
continue;
}
data1[0]=0,data1[1]=1;
for(int i=2;; i++)
{
data1[i]=(data1[i-1]+data1[i-2])%c;
if(data1[i]==1&&data1[i-1]==0)
{
len=i-1;
break;
}
}
int o=eular(c);
if(o==1)
len_c=0;
else
{
data2[0]=0,data2[1]=1;
for(int i=2;;i++)
{
data2[i]=(data2[i-1]+data2[i-2])%o;
if(data2[i]==1&&data2[i-1]==0)
{
len_c=i-1;
break;
}
}
}
int n1=quick_mod(a%len,b,len);
g[1]=data1[n1];
int mi_zhi_shu;
int n2;
if(len_c==0)
mi_zhi_shu=0;
else
{
n2=quick_mod(a%len_c,b,len_c);
mi_zhi_shu=data2[n2];
}
mi_zhi_shu+=o;
for(int i=2;i<=max;i++)
g[i]=quick_mod(g[i-1]%c,mi_zhi_shu,c);
for(int i=max-1;i>=1;i--)
{
if(g[i]==g[max])
{
len_e=max-i;
break;
}
}
int tmp=n%len_e;
if(n>=len_e)
{
while(tmp+len_e<max)
tmp=tmp+len_e;
}
if(n<len_e)
tmp=n;
if(n==1)
printf("Case %d: %d\n",++tt,g[1]);
else
printf("Case %d: %d\n",++tt,g[tmp]);
}
return 0;
}
Problem Description
In mathematics, the Fibonacci numbers are a sequence of numbers named after Leonardo of Pisa, known as Fibonacci (a contraction of filius Bonaccio, "son of Bonaccio"). Fibonacci's 1202 book Liber Abaci introduced the sequence to Western European mathematics,
although the sequence had been previously described in Indian mathematics.
The first number of the sequence is 0, the second number is 1, and each subsequent number is equal to the sum of the previous two numbers of the sequence itself, yielding the sequence 0, 1, 1, 2, 3, 5, 8, etc. In mathematical terms, it is defined by the following
recurrence relation:
That is, after two starting values, each number is the sum of the two preceding numbers. The first Fibonacci numbers (sequence A000045 in OEIS), also denoted as F
;
F
can be calculate exactly by the following two expressions:
A Fibonacci spiral created by drawing arcs connecting the opposite corners of squares in the Fibonacci tiling; this one uses squares of sizes 1, 1, 2, 3, 5, 8, 13, 21, and 34;
So you can see how interesting the Fibonacci number is.
Now AekdyCoin denote a function G(n)
Now your task is quite easy, just help AekdyCoin to calculate the value of G (n) mod C
Input
The input consists of T test cases. The number of test cases (T is given in the first line of the input. Each test case begins with a line containing A, B, N, C (10<=A, B<2^64, 2<=N<2^64, 1<=C<=300)
Output
For each test case, print a line containing the test case number( beginning with 1) followed by a integer which is the value of G(N) mod C
Sample Input
1
17 18446744073709551615 1998 139
Sample Output
Case 1: 120
解题思路:
对于A^B%C 有一个公式 即
A^x = A^(x % Phi(C) + Phi(C)) (mod C)
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
#define max 400
int quick_mod(int a,unsigned long long b, int c)//快速幂
{
int ans=1;
a=a%c;
while(b>0)
{
if (b%2==1)
ans=(ans*a)%c;
b=b/2;
a=(a*a)%c;
}
return ans;
}
int eular(int n) //欧拉函数
{
int ret=1,i;
for (i=2; i*i<=n; i++)
{
if (n%i==0)
{
n/=i,ret*=i-1;
while (n%i==0)
n/=i,ret*=i;
}
}
if (n>1)
ret*=n-1;
return ret;
}
unsigned long long a,b,n;
int c;
int data1[9003];
int data2[9003];
int g[9003];
int len,len_c,len_e;
int main()
{
int T,tt=0;
scanf("%d",&T);
while(T--)
{
scanf("%I64d%I64d%I64d%d",&a,&b,&n,&c);
if(c==1)
{
printf("Case %d: 0\n",++tt);
continue;
}
data1[0]=0,data1[1]=1;
for(int i=2;; i++)
{
data1[i]=(data1[i-1]+data1[i-2])%c;
if(data1[i]==1&&data1[i-1]==0)
{
len=i-1;
break;
}
}
int o=eular(c);
if(o==1)
len_c=0;
else
{
data2[0]=0,data2[1]=1;
for(int i=2;;i++)
{
data2[i]=(data2[i-1]+data2[i-2])%o;
if(data2[i]==1&&data2[i-1]==0)
{
len_c=i-1;
break;
}
}
}
int n1=quick_mod(a%len,b,len);
g[1]=data1[n1];
int mi_zhi_shu;
int n2;
if(len_c==0)
mi_zhi_shu=0;
else
{
n2=quick_mod(a%len_c,b,len_c);
mi_zhi_shu=data2[n2];
}
int x=quick_mod(mi_zhi_shu,n-1,o);
x+=o;
x=quick_mod(g[1],x,c);
if(n==1)
printf("Case %d: %d\n",++tt,g[1]);
else
printf("Case %d: %d\n",++tt,x);
}
return 0;
}
解法二: 在将n从1~max列举出来,在找循环节,陈老师是这么做的。
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
#define max 400
int quick_mod(int a,unsigned long long b, int c)//快速幂
{
int ans=1;
a=a%c;
while(b>0)
{
if (b%2==1)
ans=(ans*a)%c;
b=b/2;
a=(a*a)%c;
}
return ans;
}
int eular(int n) //欧拉函数
{
int ret=1,i;
for (i=2; i*i<=n; i++)
{
if (n%i==0)
{
n/=i,ret*=i-1;
while (n%i==0)
n/=i,ret*=i;
}
}
if (n>1)
ret*=n-1;
return ret;
}
unsigned long long a,b,n;
int c;
int data1[9003];
int data2[9003];
int g[9003];
int len,len_c,len_e;
int main()
{
int T,tt=0;
scanf("%d",&T);
while(T--)
{
scanf("%I64d%I64d%I64d%d",&a,&b,&n,&c);
if(c==1)
{
printf("Case %d: 0\n",++tt);
continue;
}
data1[0]=0,data1[1]=1;
for(int i=2;; i++)
{
data1[i]=(data1[i-1]+data1[i-2])%c;
if(data1[i]==1&&data1[i-1]==0)
{
len=i-1;
break;
}
}
int o=eular(c);
if(o==1)
len_c=0;
else
{
data2[0]=0,data2[1]=1;
for(int i=2;;i++)
{
data2[i]=(data2[i-1]+data2[i-2])%o;
if(data2[i]==1&&data2[i-1]==0)
{
len_c=i-1;
break;
}
}
}
int n1=quick_mod(a%len,b,len);
g[1]=data1[n1];
int mi_zhi_shu;
int n2;
if(len_c==0)
mi_zhi_shu=0;
else
{
n2=quick_mod(a%len_c,b,len_c);
mi_zhi_shu=data2[n2];
}
mi_zhi_shu+=o;
for(int i=2;i<=max;i++)
g[i]=quick_mod(g[i-1]%c,mi_zhi_shu,c);
for(int i=max-1;i>=1;i--)
{
if(g[i]==g[max])
{
len_e=max-i;
break;
}
}
int tmp=n%len_e;
if(n>=len_e)
{
while(tmp+len_e<max)
tmp=tmp+len_e;
}
if(n<len_e)
tmp=n;
if(n==1)
printf("Case %d: %d\n",++tt,g[1]);
else
printf("Case %d: %d\n",++tt,g[tmp]);
}
return 0;
}
相关文章推荐
- HDU 1358字符串循环节问题 ,next数组
- HDU 2814 Interesting Fibonacci 循环节
- [KMP-循环节问题]HDU 1358 period
- hdu-2814-Interesting Fibonacci-斐波那契循环节
- HDU 3746 字符串匹配(字符串的最小循环节问题)
- HDU 3977 Evil teacher(斐波那契数列循环节)
- HDU 3746 KMP循环节问题
- HDU 1358字符串循环节问题 ,next数组
- hdu 3746 Cyclic Nacklace KMP循环节问题
- HDU 2814 斐波那契循环节+欧拉函数降幂
- HDU 2814 - Interesting Fibonacci (Fibonacci性质 + 循环节)
- hdu 1715 大菲波数 (斐波那契数列 大数问题)
- hdu 3746(KMP的循环节问题)
- HDU 4794 Arnold 斐波那契数列循环节
- HDU-1358 Period 字符串问题 KMP算法 求最小循环节
- HDU 4794 斐波那契数列循环节
- HDU 1231 (简单DP问题)
- [dp问题] Girl Love Value(dp问题)hdu2670
- “斐波那契数列”问题的递推算法
- hdu 2602 Bone Collector (01 背包问题)