您的位置:首页 > 产品设计 > UI/UE

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: