标签:
Jesus Is Here
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 65535/102400 K (Java/Others)
Total Submission(s): 346 Accepted Submission(s): 243
Problem Description
I‘ve sent Fang Fang around 201314 text messages in almost 5 years. Why can‘t she make sense of what I mean?
``But Jesus is here!" the priest intoned. ``Show me your messages."
Fine, the first message is s1=‘‘c" and the second one is s2=‘‘ff".
The i-th message is si=si−2+si−1 afterwards. Let me give you some examples.
s3=‘‘cff", s4=‘‘ffcff" and s5=‘‘cffffcff".
``I found the i-th message‘s utterly charming," Jesus said.
``Look at the fifth message". s5=‘‘cffffcff" and two ‘‘cff" appear in it.
The distance between the first ‘‘cff" and the second one we said, is 5.
``You are right, my friend," Jesus said. ``Love is patient, love is kind.
It does not envy, it does not boast, it is not proud. It does not dishonor others, it is not self-seeking, it is not easily angered, it keeps no record of wrongs.
Love does not delight in evil but rejoices with the truth.
It always protects, always trusts, always hopes, always perseveres."
Listen - look at him in the eye. I will find you, and count the sum of distance between each two different ‘‘cff" as substrings of the message.
Input
An integer T (1≤T≤100), indicating there are T test cases.
Following T lines, each line contain an integer n (3≤n≤201314), as the identifier of message.
Output
The output contains exactly
T lines.
Each line contains an integer equaling to:
∑i<j:sn[i..i+2]=sn[j..j+2]=‘‘cff"(j−i) mod 530600414,
where sn as a string corresponding to the n-th message.
Sample Input
9
5
6
7
8
113
1205
199312
199401
201314
Sample Output
Case #1: 5
Case #2: 16
Case #3: 88
Case #4: 352
Case #5: 318505405
Case #6: 391786781
Case #7: 133875314
Case #8: 83347132
Case #9: 16520782
Source
题意:s1 = c,s2 = ff,递推,当i>=3,i等于i-2加上i-1构成,即s3 = cff,问第n个字符串中所有c字符的下标差值
第i个字符串的所有c字符的下标差值肯定由i-1的串还有i-2的串的计算过来的,然而怎么计算……需要什么定义什么,让计算机干什么
你想计算下标差值,这么多数,i-1,i-2,定义-数组把所有的下标和存起来,当算i-1和i-2串的差值计算的时候分两步,算i-2串到串末的距离加上i-1串的距离就等于i-1和i-2串差值
*****************************************************************************************************************************
ans[i] = ans[i-1]+ans[i-2] + (len[i-2]*num[i-2]-sum[i-2])*num[i-1] + sum[i-1]*num[i-2];
i 字符串中所有c字符差值 = i-1和i-2差值之和,再加上i-1中的c和i-2中的c的差值, (= 所有 i-2 中的c的下标到i-2串末尾的距离 + i-1串的和*i-2中c的数量
i-2中的c下标到i-2末尾的距离,就等于i-2中c字符 个 总长度减去c字符的下标之和
1 #include <iostream>
2 #include<cstdio>
3 #include<cstring>
4 #include<math.h>
5 #include<algorithm>
6
7 using namespace std;
8
9 const int maxn = 201315;
10
11 #define mod 530600414
12 #define INF 0x3f3f3f3f
13
14 long long num[maxn], sum[maxn], len[maxn], ans[maxn];
15 // num是该下标表示的字符串中c的个数,sum是所有c下标之和,len是字符串长度,ans是所有下标差值之和
16 int main()
17 {
18 int t, x;
19 num[1] = len[1] = sum[1] = 1;
20 len[2] = 2;
21 sum[2] = num[2] = 0;
22 for(int i = 3; i < maxn; i++)
23 {
24 num[i] = (num[i-1] + num[i-2])%mod;
25 len[i] = (len[i-1] + len[i-2])%mod;
26 sum[i] = ((sum[i-1] + sum[i-2])%mod + (len[i-2]*num[i-1])%mod)%mod; //sum表示c下标和,因为i-1串放在了i-2串的后边,所以sum要加上i-1串中的数量*i-2串的长度,就是i串中所有c的下标和
27 ans[i] = (ans[i-1]%mod + ans[i-2]%mod + (num[i-2]%mod*len[i-2]%mod - sum[i-2]%mod)%mod*num[i-1]%mod+num[i-2]%mod*sum[i-1]%mod)%mod;
28 }
29 scanf("%d", &t);
30 for(int i = 1; i <= t; i++)
31 {
32 scanf("%d", &x);
33 printf("Case #%d: %I64d\n", i, ans[x]);
34 }
35 return 0;
36 }
Jesus Is Here
标签:
原文地址:http://www.cnblogs.com/Tinamei/p/4831872.html