第一弾が始まる!

定期更新しない!

来源:http://wenku.baidu.com/link?url=XOJLwfgMsZp_9nhAK15591XFRgZl7f7_x7wtZ5_3T2peHh5XXoERDanUcdxw08SmRj1a5VY1o7jpW1xYv_V1kuYao1Pg4yKdfG4MfNsNAEa

codechef problems 第一弹

一.Authentication Failed
原题题面
Several days ago Chef decided to register at one of the

programming sites. For registering he was asked to choose a

nickname and a password. There was no problem with choosing a

nickname ("Chef" is his favorite nickname), but choosing a

password in a secure way seemed to be a real problem for Chef.

Therefore, he decided to write a program which would generate

the password of length N consisting of small Latin letters a..z.

Then Chef successfully registered at the site and saved the

password in a file (as it was too hard to remember).

Today Chef decided to visit the site once again. He entered his

nickname, copied the password from the file... "Authentication

failed!" was the answer. Trying to understand the reason of

this, he noticed that the password in his file had length N+K

instead of N! Sure enough of the source of the problem, Chef

went straight to his young brother.

And Chef was right, it was his brother who had inserted K random

small Latin letters at some random positions (possibly at the

beginning or at the end) of the password. Chef's brother didn't

remember what exactly changes he had made at all, but he

promised that he had done nothing besides inserting letters.

As there is no other way to recover the password, Chef is now

starting to remove every possible combination of K letters from

the password trying to enter (when Chef obtains the same

password as one of the previously entered passwords, he doesn't

try to enter using this password again). Now the question is:

what is the number of times Chef will receive "Authentication

failed!" as the answer before successful entering in the worst

case? As the answer might be quite large, output its remainder

of division by 1009419529.

Input

The first line of the input file contains one integer T -- the

number of test cases (no more than 10). Each test case is

described by a line containing two integers N and K (1 ≤ N ≤

10000, 1 ≤ K ≤ 100) separated by a single space, followed by a

line containing a string of length N+K consisting of small Latin

letters a..z.

Output

For each test case output just one line containing the required

number modulo 1009419529.

Example

Input:
3
2 1
aaa
3 1
abcd
4 2
ababab

Output:
0
3
10

Explanation:
In the first test case, the password is definitely "aa". In the

second test case, it can be "abc", "abd", "acd" or "bcd", so in

the worst case Chef will guess the correct option from the

fourth attempt, thus making 3 unsuccessful attempts.

Description
你把一个长为N的小写字母组成的密码保存在一个txt文件里;一个熊孩子

在密码的某些位置插入了共计K个字母,注意这里的K个字母不存在重复;

你决定把密码重新试出来;求最坏的情况下需要试多少次?
规定:N<=1W,K<=100

Solution
我们选择用递推的方式计数
先设计计数状态吧
设f[i][j]为前i位中去掉了j位且第i个字符未删除的方案数
那么我们的递推式是f[i][j]=f[i-1][j]+f[i-2][j-1]+f[i-3][j-2]+...
直接枚举每次累加的时间复杂度为O(N*K*K),超时
由于除了f[i-1][j]以外,其他项可以看作是递推矩阵中一个对角线上的

数字和,即∑f[i-x][j-x+1]
那么考虑前缀和优化,多加几个数组就行了
优化后的时间复杂度为O(N*K),O(100W)->AC
Code

#include <stdio.h>
#include <memory.h>
#define MaxL 10110
#define MaxK 110
#define MaxBuf 1<<22
#define mo 1009419529
#define RG register
#define Blue() ((S==T&&(T=(S=B)+fread(B,1,MaxBuf,stdin),S==T))?0:*S++)
#define dmin(a,b) ((a)<(b)?(a):(b))
#define dmax(a,b) ((a)>(b)?(a):(b))
char B[MaxBuf],*S=B,*T=B;
template<class Type>inline void Rin(RG Type &x){
x=;RG int c=Blue();
for(;c<||c>;c=Blue())
;
for(;c>&&c<;c=Blue())
x=(x<<)+(x<<)+c-;
}
inline void geTc(char *C){
char c=Blue();
for(;c<'a'||c>'z';c=Blue())
;
for(;c>='a'&&c<='z';c=Blue())
*C++=c;
}
char ch[MaxL];
int kase,n,k,f[MaxL][MaxK],g[MaxL],h[][MaxL],ans;
#define FO(x) {freopen(#x".in","r",stdin);}
int main(){
FO(cc authen failed);
Rin(kase);
while(kase--){
memset(f,,sizeof f);
memset(g,,sizeof g);
memset(h,,sizeof h);
ans=;
Rin(n),Rin(k),geTc(ch);
f[][]=g[]=;
for(RG int i=;i<=n+k;i++){
RG int s=ch[i-]-'a';
for(RG int j=;j<=dmin(i-,k);j++){
f[i][j]=(g[i-j-]-h[s][i-j])%mo;
if(i-j==n)(ans+=f[i][j])%=mo;
(g[i-j]+=f[i][j])%=mo;
(h[s][i-j]+=f[i][j])%=mo;
}
}
printf("%d\n",(ans-+mo)%mo);
}
return ;
}

codechef 营养题 第一弹的更多相关文章

  1. codechef营养题 第二弹

    第二弾が始まる! codechef problems 第二弹 一.Backup Functions 题面 One unavoidable problem with running a restaura ...

  2. codechef营养题 第三弹

    第三弾が始まる! codechef problems 第三弹 一.Motorbike Racing 题面 It's time for the annual exciting Motorbike Rac ...

  3. espcms代码审计第一弹

    以前的代码审计都是在CTF比赛题里面进行对于某一段代码的审计,对于后端php整体代码和后端整体架构了解的却很少,所以有空我都会学习php的代码审计,以提高自己 环境就直接用的是phpstudy,学习的 ...

  4. 关于『HTML』:第一弹

    关于『HTML』:第一弹 建议缩放90%食用 根据C2024XSC212童鞋的提问, 我准备写一稿关于『HTML』基础的帖 But! 当我看到了C2024XSC130的 "关于『HTML5』 ...

  5. typecho流程原理和插件机制浅析(第一弹)

    typecho流程原理和插件机制浅析(第一弹) 兜兜 393 2014年03月28日 发布 推荐 5 推荐 收藏 24 收藏,3.5k 浏览 虽然新版本0.9在多次跳票后终于发布了,在漫长的等待里始终 ...

  6. 我的长大app开发教程第一弹:Fragment布局

    在接下来的一段时间里我会发布一个相对连续的Android教程,这个教程会讲述我是如何从零开始开发“我的长大”这个Android应用. 在开始之前,我先来介绍一下“我的长大”:这是一个校园社交app,准 ...

  7. Hadoop基础-MapReduce的工作原理第一弹

    Hadoop基础-MapReduce的工作原理第一弹 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 在本篇博客中,我们将深入学习Hadoop中的MapReduce工作机制,这些知识 ...

  8. Java基础-程序流程控制第一弹(分支结构/选择结构)

    Java基础-程序流程控制第一弹(分支结构/选择结构) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.if语句 1>.if语句的第一种格式 if(条件表达式){ 语句体: ...

  9. RMQ_第一弹_Sparse Table

    title: RMQ_第一弹_Sparse Table date: 2018-09-21 21:33:45 tags: acm RMQ ST dp 数据结构 算法 categories: ACM 概述 ...

随机推荐

  1. bzoj2287

    背包+fft 既然要不选一个东西,那么我们求出前缀背包和后缀背包,每次答案就是f[i-1][w]*g[i+1][j-w] 但是这样复杂度还是n^3,跑不过,但是我们发现上面那个东西不就是个裸卷积吗,直 ...

  2. bzoj3270

    3270: 博物馆 Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 474  Solved: 261[Submit][Status][Discuss] ...

  3. Could not find modernizr-2.6.2 in any of the sources

  4. 【152】C# 操作 Excel 杂记

    前面写了一篇博文是关于 C# 操作 Excel 的文章,但是里面有些中规中矩,搞的我不知道怎么写了,所以另开一帖.. 注意:基本应用如下所示! //首先是引用 using Excel = Micros ...

  5. Linux中的LVM

    逻辑卷管理器,通过将另外一个硬盘上的分区加到已有文件系统,来动态地向已有文件系统添加空间的方法. 逻辑卷管理的核心是处理安装在系统上的硬盘分区.在逻辑卷管理的世界里,硬盘称作物理卷(Physical ...

  6. E20180127-hm

    retain  vt. 保持; 留在心中,记住; 雇用; 付定金保留;

  7. JavaScript编程艺术-第8章-8.6.1-显示“缩略词语表”

    8.6.1-显示“缩略词语表” ***代码亲测可用*** HTML: JS: ***end***

  8. 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛-K-Matrix Multiplication(矩阵乘法)

    题目描述 In mathematics, matrix multiplication or matrix product is a binary operation that produces a m ...

  9. python程序展现图片

    突然想写一个python程序能够显示图片的 ,展示文字的已经实现了 现在就搞一搞这个吧 相信也是很简单 首先是放一张图片在e盘下面 等会程序打包的时候将会用到 就决定是你啦 皮卡丘: 然后就写代码吧:

  10. [转]微信开发.Net 接入示例

    本文转自:http://my.oschina.net/lcak/blog/219618 微信公众平台接口开发官方仅提供了 PHP 接入示例代码, 网上找到的.Net代码多半需要积分下载, 所以自己写了 ...