Hex-a-bonacci

给定一个非优化的代码,要求根据输入找到代码的输出。代码实现了一个递归函数fn,计算序列的第n项,且涉及动态规划和取余操作。在主函数中读取测试用例,用动态规划数组方法避免溢出并计算结果,对10000007取余后输出。
摘要由CSDN通过智能技术生成

Given a code (not optimized), and necessary inputs, you have to find the output of the code for the inputs. The code is as follows:
int a, b, c, d, e, f;
int fn( int n ) {
if( n == 0 ) return a;
if( n == 1 ) return b;
if( n == 2 ) return c;
if( n == 3 ) return d;
if( n == 4 ) return e;
if( n == 5 ) return f;
return( fn(n-1) + fn(n-2) + fn(n-3) + fn(n-4) + fn(n-5) + fn(n-6) );
}
int main() {
int n, caseno = 0, cases;
scanf(“%d”, &cases);
while( cases-- ) {
scanf(“%d %d %d %d %d %d %d”, &a, &b, &c, &d, &e, &f, &n);
printf(“Case %d: %d\n”, ++caseno, fn(n) % 10000007);
}
return 0;
}
Input
Input starts with an integer T (≤ 100), denoting the number of test cases.
Each case contains seven integers, a, b, c, d, e, f and n. All integers will be non-negative and 0 ≤ n ≤ 10000 and the each of the others will be fit into a 32-bit integer.
Output
For each case, print the output of the given code. The given code may have integer overflow problem in the compiler, so be careful.
Sample Input
5
0 1 2 3 4 5 20
3 2 1 5 0 1 9
4 12 9 4 5 6 15
9 8 7 6 5 4 3
3 4 3 2 54 5 4
Sample Output
Case 1: 216339
Case 2: 79
Case 3: 16636
Case 4: 6
Case 5: 54

#这道题最优的方案就是用dp。

题意:给你一组数据的前6组,让你求出第N个数;满足关系:fn(n)= fn(n-1) + fn(n-2) + fn(n-3) + fn(n-4) + fn(n-5) + fn(n-6) ,数据对10000007取余。
用数组去记录每一个数据,直接输出N即可,但是切记要取余。
由于数据范围较大,必须用long long
代码如下:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
const int maxn=10000007;//取余数字
long long r[100010];//尽量把数组开大些,范围大些
int main(){
    long long num,N;
    scanf("%lld",&num);
    long long key=1;
    while(num--){
        memset(r,0,sizeof(r));
        scanf("%lld%lld%lld%lld%lld%lld%lld",&r[0],&r[1],&r[2],&r[3],&r[4],&r[5],&N);
        for(int i=6;i<=N;i++){
            r[i]=r[i-1]+r[i-2]+r[i-3]+r[i-4]+r[i-5]+r[i-6];
            r[i]%=maxn;
        }
        printf("Case %lld: %lld\n",key,r[N]%maxn);
        key++;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旺旺的碎冰冰~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值