您的位置:首页 > 其它

hdu 1041 打表:递推+大数 模板

2017-11-09 22:10 429 查看
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1041

每个0换成1 0,每个1换成0 1

#include<cstdio>
#include<cstring>
#include <iostream>
using namespace std;
const int mod=10000;
int a[1010][700];
void fun(){//打表
a[1][1]=0;
a[2][1]=1;
a[3][1]=1;
a[4][1]=3;
int tmp;
for(int i=5; i<1010; i++){
tmp=0;
for(int j=1; j<700; j++){
tmp+=a[i-1][j]+a[i-2][j]+a[i-2][j];//递推公式f(n)=f(n-1)+2*f(n-2)
a[i][j]=tmp%mod;
tmp/=mod;//这样不用处理进位
}
}
return ;
}
int main(){
fun();
int n,i;
while(~scanf("%d",&n)){
if(n==1){
puts("0");
continue;
}
i=699;
while(a
[i]==0) i--; //去前置零,其实这时候数组是倒着存的,0在后面
printf("%d",a
[i--]); //先输出第一个,因为不用保存前置零
for(; i>0; i--)
printf("%04d",a
[i]);//如末一个数组存的是35,但其实是这个大数被分成1356 0035 4565等等
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: