Hedwig's Ladder

仓鼠Hedwig在一个由主人每天重新布置的管道网络中探险,每个交叉点都藏有一块胡萝卜。Hedwig的目标是在不重复经过同一交叉点的情况下,找出特定长度的所有路径。本篇将介绍如何通过编程解决这一问题,找出所有可能的路径数量。
摘要由CSDN通过智能技术生成

Hedwig the hamster enjoys exploring networks of hamster tubes. Each day, Hedwig’s ownerrearranges the tubes to form a new network of tubes.
在这里插入图片描述
One day, Hedwig awoke to find that the network of tubes had been arranged in a ladder pattern as shown below, where each junction point (An and Bn) contained a small piece of carrot (Hedwig loves carrots!). Hedwig had not yet explored the new tube network and had no idea how long it was (it may go on forever!)
在这里插入图片描述
Hedwig decided to make a game where given a length, figure out how many paths from the start, A0, of the given length there are using these rules: Each time a junction is reached, Hedwig eats the piece of carrot that is there. A junction can only be used if there is a piece of carrot there when Hedwig arrives.To help Hedwig out, you will write a program which finds the number of paths from A0 of a specified length which do not hit any junction more than once. For example, all the paths of length 3 are shown below:
在这里插入图片描述
InputThe first line of input contains a single decimal integer P, (1≤P≤800), which is the number of data sets that follow. Each data set should be processed identically and independently.Each data set consists of one line of input. The line contains the data set number, K, followed by the length, N, (1≤N≤1000) of the paths to be counted.OutputFor each data set there is one line of output.The output line consists of the data set number, K, followed by the number of paths of length N modulo 10007.

输入
4
1 3
2 9
3 18
4 111

输出
1 6
2 110
3 8361
4 237

#include<cstdio>
using namespace std;
typedef long long ll;
const int mod=10007;
ll dp[10005];
ll f(int n){
 if(dp[n]) return dp[n];
 if(n==1) return 2;
 if(n==2) return 3;
 else 
 return dp[n]=(f(n-1)%mod+f(n-2)%mod+(n&1))%mod;
}
int main(){
 int P,K,N;
 scanf("%d",&P);
 while(P--){
  scanf("%d%d",&K,&N);
  printf("%d %lld\n",K,f(N));
 }
 return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值