码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 5459 递推

时间:2015-09-19 19:33:14      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:

中等递推题:

ans[i] = ans[i - 2] + ans[i - 1] + ( sum[i - 2] + cnt[i - 2] * len[i - 1] ) * cnt[i - 1] - sum[i - 1] * cnt[i - 2];

其中,ans[i]代表答案,cnt[i]代表ith个message中有多少个cff,sum[i]代表ith个message中cff的c的位置的和(从右向左index依次为1,2...),len[i]代表ith个message的长度。

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 using namespace std;
 5 
 6 typedef long long ll;
 7 const int MOD = 530600414;
 8 const int N = 201315;
 9 int len[N];
10 int sum[N];
11 int ans[N];
12 int cnt[N];
13 
14 void init()
15 {
16     len[1] = 1;
17     len[2] = 2;
18     len[3] = 3;
19     cnt[1] = cnt[2] = 0;
20     cnt[3] = 1;
21     sum[1] = sum[2] = 0;
22     sum[3] = 3;
23     ans[1] = ans[2] = ans[3] = 0;
24     for ( int i = 4; i < N; i++ )
25     {
26         len[i] = ( len[i - 2] + len[i - 1] ) % MOD;
27         cnt[i] = ( cnt[i - 2] + cnt[i - 1] ) % MOD;
28         sum[i] = ( ( sum[i - 2] + ( ( ll ) cnt[i - 2] * len[i - 1] ) % MOD ) % MOD + sum[i - 1] ) % MOD;
29         int tmp1 = ( ans[i - 2] + ans[i - 1] ) % MOD;
30         int tmp2 = ( ( ( ll ) ( ( sum[i - 2] + ( ( ll ) cnt[i - 2] * len[i - 1] ) % MOD ) % MOD ) * cnt[i - 1] ) % MOD - ( ( ll ) sum[i - 1] * cnt[i - 2] ) % MOD + MOD ) % MOD;
31         ans[i] = ( tmp1 + tmp2 ) % MOD;
32     }
33 }
34 
35 int main ()
36 {
37     init();
38     int t;
39     scanf("%d", &t);
40     for ( int _case = 1; _case <= t; _case++ )
41     {
42         int n;
43         scanf("%d", &n);
44         printf("Case #%d: %d\n", _case, ans[n]);
45     }
46     return 0;
47 }

 

hdu 5459 递推

标签:

原文地址:http://www.cnblogs.com/huoxiayu/p/4822007.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!