Jesus Is Here HDU - 5459(思维)

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

题意

求解任意两个字符c距离之和。

分析

每一个字符串都是由前面两个字符串组成 ,即s[i]=s[i-2]+s[i-1];所以易想到
ans[i]=(ans[i-1]+ans[i-2]+s[i-2]中的每个c到s[i-1]中每个c的距离);

ll loca[maxn];//字符c的位置 之和
ll dis[maxn]; // 字符c距离末尾之和
ll sum_c[maxn];//字符c个数之和
ll sum_s[maxn];//字符串长度
ll ans[maxn]; //答案

因为每一个字符串都是由前面两个字符串组成 ,所以我们可以分成两部分来思考,例( c f f f f c f f | f f c f f c f f f f c f f)
前面字符串中每个c到字符末尾的距离加后一个字符串中c到开头的距离即( loca[maxn];//字符c的位置之和
dis[maxn]; // 字符c距离末尾之和 )
1 3 | 3 6 11
第一个c:7+3+7+6+7+11
第二个c:2+3+2+6+2+11
所以(7+2)*3+(3+6+11)*2
仔细观察既可以得到3为后面字符串中c的个数;2为前面字符串中c的个数。
所以ans的递推公式:
ans[i]=(ans[i-1]+ans[i-2]+dis[i-2]*sum_c[i-1]+loca[i-1]*sum_c[i-2]);
然后在思考loca与dis的递推公式:
每个c的位置前面一个字符串不变后面字符串中每个c的位置加上前一个字符串长度即可
loca[i]=(loca[i-1]+loca[i-2]+sum_c[i-1]*sum_s[i-2]);
同理c距离末尾之和即后面字符串不变前面的加后面的长度
dis[i]=(dis[i-1]+dis[i-2]+sum_c[i-2]*sum_s[i-1]);

有了这几个递推公式就可以打表得出答案了。

注意:每一次递推都要取模哦!!!

#include<cstdio>
#define maxn 201314+10
#define mod 530600414
using namespace std; 
typedef long long ll;

ll loca[maxn];//字符c的位置 之和 
ll dis[maxn]; // 字符c距离末尾之和 
ll sum_c[maxn];//字符c个数之和 
ll sum_s[maxn];//字符串长度 
ll ans[maxn];
int main()
{
	int t,cnt=0,n;
	scanf("%d",&t);
	loca[3]=1,loca[4]=3;
	dis[3]=2,dis[4]=2;
	sum_c[3]=sum_c[4]=1,sum_c[5]=2;
	sum_s[3]=3,sum_s[4]=5,sum_s[5]=8;
	ans[3]=0,ans[4]=0;
	for(int i=5;i<=maxn;i++){
		loca[i]=(loca[i-1]+loca[i-2]+sum_c[i-1]*sum_s[i-2])%mod;
		dis[i]=(dis[i-1]+dis[i-2]+sum_c[i-2]*sum_s[i-1])%mod;
		sum_c[i]=(sum_c[i-1]+sum_c[i-2])%mod;
		sum_s[i]=(sum_s[i-1]+sum_s[i-2])%mod;
		ans[i]=(ans[i-1]+ans[i-2]+dis[i-2]*sum_c[i-1]+loca[i-1]*sum_c[i-2])%mod;
		ans[i]%=mod;
	}
	while(t--)
	{
		scanf("%d",&n);
		printf("Case #%d: %lld\n",++cnt,ans[n]);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据引用中的描述,"Process finished with exit code 0" 表示程序正常结束,没有出现任何错误。 而引用提到,产生编译后的target文件,我们需要检查其中的文件是否编译成功,以确保程序能正常执行。 然而,引用中的描述并没有直接与 "Process finished with exit code 3" 相关。所以我们无法根据提供的引用内容得出关于 "Process finished with exit code 3" 的具体解释。 请提供更多关于 "Process finished with exit code 3" 的上下文或引用内容,以便我能够为您提供准确的答案。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [运行显示“process finished with exit code 0”的解决办法(pycharm)](https://blog.csdn.net/m0_65485230/article/details/129307303)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [Process finished with exit code 0 报错解决方法](https://blog.csdn.net/Jesus_apostle/article/details/120315059)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [C++代码展示使用try-catch块来捕获和处理异常方法](https://download.csdn.net/download/weixin_44609920/88222487)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值