hdoj 1005 number sequence(找规律)
2017-12-04 15:22
357 查看
思路:因为题目给的n的范围很大,所以直接用递归求值是不行的,先找规律,发现f(n)的值由f(n-1),f(n-2)决定,而f(n)的值属于[0,6](因为是对7取模的结果)所以这个函数以49为一个循环,n的值可以对49取模,从而大大减小对存储空间的要求。
代码如下:#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
long long sum=0;
long long fx(long long a,long long b,long n)
{
if(n==1 || n==2)
return 1;
else
return (a*(fx(a,b,n-1))+b*(fx(a,b,n-2)))%7;
}
int main(int argc, char *argv[]) {
int a,b;
long n;
while(scanf("%d%d%d",&a,&b,&n)!=EOF)
{
sum=0;
if(a==0 &&b==0 &&n==0)
break;
else
sum=fx(a,b,n%49);
printf("%lld\n",sum);
}
return 0;
}
代码如下:#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
long long sum=0;
long long fx(long long a,long long b,long n)
{
if(n==1 || n==2)
return 1;
else
return (a*(fx(a,b,n-1))+b*(fx(a,b,n-2)))%7;
}
int main(int argc, char *argv[]) {
int a,b;
long n;
while(scanf("%d%d%d",&a,&b,&n)!=EOF)
{
sum=0;
if(a==0 &&b==0 &&n==0)
break;
else
sum=fx(a,b,n%49);
printf("%lld\n",sum);
}
return 0;
}
相关文章推荐
- hdoj--1005--Number Sequence(规律题)
- HDOJ 1005 Number Sequence(规律)
- HDOJ 1005-Number Sequence【规律】
- hdoj1005 Number Sequence(找规律)
- HDOJ - 1005 - Number Sequence(规律,数学、非常规)
- hdoj--1005--Number Sequence(规律题)
- HDOJ 1005 Number Sequence(规律)
- hdu 1005 Number Sequence【规律题】
- hdoj1005(number sequence
- hdu 1005 Number Sequence(找规律)
- [hdoj_1005]Number Sequence
- HDOJ1005 Number Sequence(数论水题)
- hdu 1005 || zoj 2105 Number Sequence(规律)
- HDOJ_1005_Number Sequence
- HDU 1005 Number Sequence (数学规律)
- HDOJ 1005 Number Sequence
- Number Sequence (HDoj1005)
- HDOJ 1005 Number Sequence 循环数列求值 C语言实现
- HDOJ 1005 Number Sequence
- 【hdoj1005】Number Sequence