hdu 1297 数学

来源:互联网 发布:squid 端口 编辑:程序博客网 时间:2024/03/29 22:37

大数+找规律

f[n] = f[n-1] + f[n-2] + f[n-4]

AC代码如下:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int f[1001][1001];bool add( int a[], int b[] ){int pos = 1, mod = 0;for( ; pos <= b[0]; pos++ ){mod += a[pos] + b[pos];a[pos] = mod % 10;mod /= 10;}while( mod ){mod += a[pos];a[pos++] = mod % 10;mod /= 10;}pos--;a[0] = pos > a[0] ? pos : a[0];return true;}int main(){int N;memset( f, 0, sizeof( f ) );f[1][0] = f[2][0] = f[3][0] = f[4][0] = 1;f[1][1] = 1;f[2][1] = 2;f[3][1] = 4;f[4][1] = 7;for( int i = 5; i <= 1000; i++ ){add( f[i], f[i-1] );add( f[i], f[i-2] );add( f[i], f[i-4] );}while( scanf( "%d", &N ) != EOF ){for( int i = f[N][0]; i >= 1; i-- ){cout << f[N][i];}cout << endl;}return 0;}


 

原创粉丝点击