1040 Be Patient , Read Carefully !【子串和】
来源:互联网 发布:java中文文档 编辑:程序博客网 时间:2024/06/08 02:33
Be Patient , Read Carefully !
时间限制:1000 ms | 内存限制:65535 KB
难度:0
- 描述
- (原文讲的是白雪公主的故事,和题目没有关系,这里已经省略......)
BlaBlaBla...
反正你是读不懂题目在说什么的...但是你偷偷看到了Fongliqian 的代码:
#include<stdio.h>
int main()
{
int i,j,cnt,k,N,K,a[5555];
scanf("%d%d",&N,&K);
int ans=0;
for(i=1;i<=N;i++) scanf("%d",&a[i]);
for( i=1;i<=N;i++)
for(j=i+1;j<=N;j++)
{
cnt=0;
for( k=i;k<=j;k++) cnt+=a[k];
if(cnt%K==0) ans++;
}
printf("%d\n",ans);
}
然后你又偷偷问了Yomeam,他跟你说这样会超时(不管你信不信,反正我是信了,不信你复制粘贴上去看看?)
- 输入
- (只有一组数据)
第一行2个整数 N K N<= 5000 K>0
第二行N个数a[i]: 第i个数为a[i] 0<= a[i]<= 10000 - 输出
- 结果,换行。
- 样例输入
5 11 2 3 4 5
- 样例输出
10
这个题是目前唯一一个做过的给你代码,让你理解运行的功能,然后自己个更优化的程序来解决这个问题!
不难看出,这个程序要求的是给出的序列的子串(多于一个元素)能被 给出的数整除的个数,输出个数...
简化方法是,在输入的时候预处理一下,把时间复杂度降到n的平方,这样就不会超时了......
#include<stdio.h>int x[5005],n,k;void slove(){int cnt=0;for(int i=2;i<=n;++i)//注意两层循环的控制{for(int j=0;j<i-1;++j){int sum=x[i]-x[j];//子串和if(sum%k==0){++cnt;//统计}}}printf("%d\n",cnt);}int main(){while(~scanf("%d%d",&n,&k)){for(int i=1;i<=n;++i){scanf("%d",x+i);if(i>1){x[i]+=x[i-1];//预处理...} }slove();}return 0;}
0 0
- 1040 Be Patient , Read Carefully !【子串和】
- be patient~~~
- Should Read the Message Prompted from the Computer Carefully
- build path errors和Arichive cannot be read问题
- Be patient to grow -- Rome was not built in a day
- YOU SHOULD CAREFULLY
- A book need to be read
- Patient Orientation(0020.0020)的理解,DR和乳腺(MG)的挂片协议
- documents needed to be read!!!(to be updated)
- API IsValidCodePage Patient!d1
- HDU4960:Another OCD Patient
- hdu4960-Another OCD Patient
- HDU4960 Another OCD Patient
- Another OCD Patient HDU
- NYOJ44 子串和
- 子串和
- 子串和
- 最大子串和
- linux手动安装screen
- JavaBean
- 服务器文件自动备份工具
- Android Spinner
- permgen space这个异常解决方案
- 1040 Be Patient , Read Carefully !【子串和】
- 开发流程简易图
- sql_优化
- MemCache
- 欢迎使用CSDN-markdown编辑器
- http
- android SDK manager 无法获取更新版本列表
- 3D编程的四个境界
- openssl unable报错