Codeforces 842A. Kirill And The Game (暴力)
来源:互联网 发布:淘宝和阿里巴巴老板 编辑:程序博客网 时间:2024/06/06 17:34
给定5个int : l,r,x,y,k;
求是否存在两个整数a,b满足l<=a<=r, x<=b<=y使得k*b=a;
这道题刚开始判断范围,结果被hack了.
发现乘出来的范围不是连续的,据说很多人疯狂hack,不是暴力做法的都被hack了.
纯暴力会t,可以先判断范围剪枝一下.
代码用时:5分钟
#include <bits/stdc++.h>using namespace std;int main(void){ long long l,r,x,y,k; cin >>l>>r>>x>>y>>k; long long pl=k*x,pr=k*y; if(pl>r||pr<l) { printf("no\n"); } else { for(long long i=x;i<=y;i++) { long long p=i*k; for(long long j=l;j<=r;j++) { if(j==p) { printf("yes\n"); return 0; } } } printf("no\n"); } return 0;}
有两个点需要注意:一个是变量的运算结果会存在变量的类型里,比如
int a=1e7,b=1e7;long long c=a*b;
经测试,此时c的值并不是1e14,而是相当于溢出了
另一个是一个优化技巧.
for(int i=l;i<=r;i++)for(int j=x;j<=y;j++)if(j*k==i) /* do something */;
for(int i=x;i<=y;i++){ int p=i*k; for(int j=l;j<=r;j++) { if(j==p) /* do something */; } }
两份代码效率天差地别,原因在于第一个中,循环顺序实际上是可交换的,交换后就会发现,做了大量的重复操作 j*k.
以后多加注意.
阅读全文
0 0
- Codeforces 842A. Kirill And The Game (暴力)
- Codeforces 842 A Kirill And The Game
- codeforces 842 A. Kirill And The Game(阅读题)
- A. Kirill And The Game
- Codeforces Round #430 (Div. 2) A. Kirill And The Game
- Codeforces Round #430 (Div. 2) A. Kirill And The Game
- Codeforces #430(div2) A. Kirill And The Game
- (数学水题)Codeforces A. Kirill And The Game
- Kirill And The Game
- A. Kirill And The Game #430 (Div. 2)
- codeforces:A. The Great Game
- Codeforces 443A Borya and Hanabi(暴力)
- CodeForces 321A Ciel and Robot[暴力]
- Codeforces 425A Sereja and Swaps(暴力)
- Codeforces 487A Fight the Monster(暴力)
- Codeforces 612A The Text Splitting 【暴力】
- Codeforces 467B Fedor and New Game(暴力)
- Codeforces 451C Predict Outcome of the Game(暴力)
- ListView源码解析(一) 绘制流程
- Java工具类之Apache的Commons Lang和BeanUtils
- stringbuilder截取最后一个字符
- 使用Java随机划分数据集为训练集和测试集
- java的timer方法改造,改造成注解使用
- Codeforces 842A. Kirill And The Game (暴力)
- Unity3d多人网络
- hsdis插件查看汇编码
- 欢迎使用CSDN-markdown编辑器
- fpga non-project模式下使用
- Oauth2授权协议
- 试不过三
- MongoDB 导入 CSV 格式数据详细过程
- 数组作为函数参数、scanf初始化指针