【codeforces 787A】The Monster
【题目链接】:http://codeforces.com/contest/787/problem/A
【题意】
把b一直加a->得到x
把d一直加c->得到y
然后问你x和y可不可能有相同的值.
有的话,输出那个最小的;
【题解】
等价于
令t=(b+u*a-d)%c==0
u为整数
这里如果b< d就swap(a,c),swap(b,d)就好;
然后如果t遇到了重复的值,就结束,往后都不可能了;
如果t中途变成0了,就输出那个u;
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define ref(x) scanf("%lf",&x)
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 110;
map <int, int> dic;
int a, b, c, d;
int ans = -1;
void in()
{
rei(a), rei(b);
rei(c), rei(d);
}
int get_ans()
{
int x = 0;
if (b < d)
{
swap(a, c);
swap(b, d);
}
//b>=d
int t = (b + x*a - d) % c;
while (t != 0)
{
if (dic[t])
return -1;
dic[t] = 1;
x++;
t = (b + x*a - d) % c;
}
return x;
}
int main()
{
//freopen("F:\\rush.txt", "r", stdin);
in();
ans = get_ans();
if (ans==-1)
puts("-1");
else
printf("%d\n",b+ans*a);
//printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}
【codeforces 787A】The Monster的更多相关文章
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【27.66%】【codeforces 592D】Super M
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
- 【Codeforces 670C】 Cinema
[题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...
随机推荐
- CSDN博客的文章分类和战略规划
CSDN原创文章已经有300多篇了,现在已经整理了好多个分类目录了. 今天,特别向大家介绍下,每个分类的含义和规划. CSDN博客是我的一个重要的自媒体,也是我的一个战略实践. 我会精心维护这个博客, ...
- 洛谷 P2025 脑力大人之监听电话
P2025 脑力大人之监听电话 题目背景 画外音: (声明:不要管前面那个,纯属意外,现已经重新编题,绝对原创) 上次海选,我们选出了参赛者中的20%参加本次比赛,现在我们将进行第二轮的筛选,这次的比 ...
- dinic算法学习(以poj1273为例)
Dinic 算法模板 Dinic算法是一种比較easy实现的.相对照较快的最大流算法. 求最大流的本质,就是不停的寻找增广路径.直到找不到增广路径为止. 对于这个一般性的过程,Dinic算法的优化例 ...
- php课程 10-34 目录遍历中的注意事项是什么
php课程 10-34 目录遍历中的注意事项是什么 一.总结 一句话总结:用scandir,会把目录和文件放到一个数组中. 1.移动文件怎么实现,php里面没有移动文件这个函数? 先复制,再删除 2 ...
- 【UIL框架】Universal-Image-Loader全然解析(一)之介绍与使用具体解释
转载请标明出处: http://blog.csdn.net/developer_jiangqq/article/details/50439814 本文出自:[江清清的博客] (一).前言: [好消息] ...
- mysql快速入门 分类: B6_MYSQL 2015-04-28 14:31 284人阅读 评论(0) 收藏
debian方式: apt-get install mysql-server-5.5 mysql -u root -p redhat安装方式 一.下载并解压 $ wget http://cdn ...
- 影响stm32仿真的因素
可能是因为电池电量不足??? 电量不足可能会妨碍SD卡的挂载
- UICollectionView使用方法补充(照片轮播墙)
一 整体功能图和实现思路 1 完整的功能图: 2 实现功思路: 1> 流水布局(实现UICollectionView必需要的条件) 2> 自己定义cell(实现UICollectionVi ...
- php面试题四
php面试题四 一.总结 二.php面试题四 01. 输出为 Mozilla/4.0(compatible;MSIE5.01;Window NT 5.0)时,可能的输出语句是: A.$_S ...
- [AngularFire 2] Protect Write Access Using Security Rules
We cannot allow un-auth user to change the database data as they want, for Firebase, it is easy just ...