【codeforces 787A】The Monster
2024-08-31 21:02:18
【题目链接】: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;
}
最新文章
- HTML5新增的属性
- (02)javascript属性
- [原创] 关于quartz (spring 中的任务调度器)时间配置
- HDU - 1693 Eat the Trees(多回路插头DP)
- SignalR2.0开发实例之——私聊
- angular中的jsonp记录
- IE的CSS滤镜不过只支持IE可以创建幻灯片等一些炫酷的效果
- LeetCode算法题-Array Partition I(Java实现)
- Ubuntu学习之路
- MySQL Connector/ODBC 2.50 (MyODBC 2.50)连接方式
- Java多线程常用面试题(含答案,精心总结整理)
- 一些Android手机的平台信息
- java.util.base64报错解决
- 使用__future__实现从python2.7到python3.x的过渡
- iframe设置高度为100%
- Oracle EBS打补丁
- JS基础(三)
- Grails开发环境的高速搭建
- 【CSS】关于flex
- C++中去掉string字符串中的\r\n等
热门文章
- Windows下合并tar分卷
- vmware之linux不重启添加虚拟硬盘
- 从Set里面取出有序的记录
- hprof教程 分类: B1_JAVA 2015-03-02 12:18 444人阅读 评论(0) 收藏
- 魔兽争霸war3心得体会(三):UD内战
- js javascript正则
- 【37.21%】【codeforces 721B】Passwords
- android 之修改图片的某一颜色值
- [React] Modify file structure
- JBoss AS 7之初步了解(The Return Of The King)