codechef 营养题 第一弹

第一弾が始まる!

定期更新しない!

来源: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=0;RG int c=Blue();
    for(;c<48||c>57;c=Blue())
        ;
    for(;c>47&&c<58;c=Blue())
        x=(x<<1)+(x<<3)+c-48;
}
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[30][MaxL],ans;
#define FO(x) {freopen(#x".in","r",stdin);}
int main(){
    FO(cc authen failed);
    Rin(kase);
    while(kase--){
        memset(f,0,sizeof f);
        memset(g,0,sizeof g);
        memset(h,0,sizeof h);
        ans=0;
        Rin(n),Rin(k),geTc(ch);
        f[0][0]=g[0]=1;
        for(RG int i=1;i<=n+k;i++){
            RG int s=ch[i-1]-‘a‘;
            for(RG int j=0;j<=dmin(i-1,k);j++){
                f[i][j]=(g[i-j-1]-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-1+mo)%mo);
    }
    return 0;
}
时间: 2024-10-13 23:57:35

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

codechef营养题 第三弹

第三弾が始まる! codechef problems 第三弹 一.Motorbike Racing 题面 It's time for the annual exciting Motorbike Race in Byteland. There are N motorcyclists taking part in the competition. Johnny is watching the race. At the present moment (time 0), Johnny has taken

「软考备考,福利第一弹」全场视频课程6折/套餐折上9折+答疑指导

备考福利第一弹来啦!这次第一弹的福利给你众多惊喜~本次福利是由软考专家.课课家金牌讲师@多位业内软考大咖的专场活动,不但有视频课程/套餐限时折扣,还有赠送:辅导教材.历年真题.考前冲剌资料.在线模拟测试题库等配套资料,还可以与@面授名师进行互动答疑,让疑难问题不再困扰你~ 好了,闲话不多说,赶快来看看软考暑假充电,福利第一弹的具体内容吧~ 软考阅卷专家,拥有10年以上软考培训经验,培训学员过万人.擅长培训课程有:网络工程师.网络规划设计师.系统集成项目管理师.信息系统项目管理师.软件设计师.信息

javascript之【贪吃蛇系列】第一弹:简单的贪吃蛇实现

参考博客:http://blog.csdn.net/sunxing007/article/details/4187038 以上博客是参考,毕竟第一次做,真让自己盲人摸象做不出来. 不过我在其上做了一些改进,界面等效果看起来更好一些. 下图是在Chrome上运行的效果,但是火狐和IE会不兼容,onkeydown事件不能正确调用 这里用了一张图把贪吃蛇制作过程的思想画了出来,画的有点简陋: 下面就是把代码发上来,上边有详细的解释: <html> <head> <title>

FluentData 学习 第一弹

地址: http://fluentdata.codeplex.com/ 前世: FluentData 我们公司用的一个增删改查的里面的持久层.之前还不知道 这个持久层叫FluentData.  某天看见群里 说 某视频网站里面 居然在讲这个开发框架,还收费.我搜了一下.fluentdata有源代码. 这个13年有过记载.不过我是新手.什么也需要 学习一下.  和 室友说了一下微型orm ,他们 呢  用的  微型orm是 Dapper .可以去了解一下. 废话真多,完毕. FluentData

C#Light 和 uLua的对比第一弹

初始化 做一样的初始化,其实是没办法对等的 C#Light的Env 不等于Lua的LuaState C#Light的执行有完全的执行堆栈,完整的作用域,lua都没有 C#Light有完整的class 函数,继承特征,lua都没有 C#Light可以用VS做编辑器,lua? 为了保持平等对抗,三个测试先做一遍,再依次执行 测试1 ,简单的一次创建给个名字并销毁,测试300次 测试2,调用Debug.Log打印,执行300次 测试3,一次性在脚本里多调用一些方法,创建10个物体再销毁 测试一结果:

The Internet Communications Engine (Ice) 跨平台异构通讯方案 第一弹-ICE简介

.net中的通讯方案很多,从.net Remoting,MSMQ,Webservice,WSE,WCF等等,他们都有一个特点,易用,但是都不能跨语种异构,如果你服务端要用java开发,客户端用C#开发,或者其它语言譬如C++, Python,PHP, Ruby, Objective-C,等等,那么.net提供的解决方案将不再有效.现在,隆重推出跨平台异构方案ICE.ICE官网:http://www.zeroc.com/ice.html 简介:The Internet Communications

MongoDB第一弹

下载: MongoDB下载地址: http://www.mongodb.org/ 我下载的是windows32的MSI. 安装: next就可以. 新建: 新建data文件夹,在data里面再建db和log两个子目录. 启动: 进入cmd,到当前MongoDB的bin目录下面,启动 "mongd"!(不是mongo),修改db路径. 然后打开浏览器输入localhost:27017,会出现: 最后在cmd中启动mongo.exe,出现: 基本操作: insert: find: upda

Ubuntu闪电入门第一弹

目录: 一.设置主机名 1.临时修改主机名 2.永久修改主机名 二.设置网络 1.配置静态IP地址 2.配置DNS服务器解析 3.修改DNS本地解析 三.软件包管理 1.dpkg管理软件包 2.apt管理软件包 四.运行级别 五.文件传输工具lrszs 1.发送文件sz 2.接收文件rz 六.磁盘管理 1.磁盘分区 2.磁盘分区格式化 3.挂载磁盘分区 一.设置主机名 1.临时修改主机名(重启系统后失效) $ sudo hostname ikki $ hostname ikki 2.永久修改主机

C# 二进制替换第一弹 byte 数组替换

Unity 3D本地发布WebPlayer版时遇到提示:"Failed to download data file"(如上图),遇到这个问题就是指Web服务器并没有支持这种*.unity3d文件类型.需要做的是在Web服务器中添加MIME类型: 1.IIS 7 及以上版本: 在功能视图的IIS选项卡中: 双击打开MIME,选择"添加": .unity3d application/octet-stream 2.IIS6版本: 右击计算机名,选择属性,找到MIME类型,