补一篇以前的扩展欧几里得的题,发现以前写错了竟然也过了,可能数据水???

这个题还是很有意思的,和队友吵了两天,一边吵一边发现问题???

L. Knights without Fear and Reproach

http://codeforces.com/gym/100812/problem/L

time limit per test

2.0 s

memory limit per test

256 MB

input

standard input

output

standard output

They were all dead. The final lunge was an exclamation mark to everything that had led to this point. I wiped my sword from the blood of Dragon and sheathed it. And then it was all over. Devastated, I came out of the empty castle and wandered somewhere along a dirt road. But before I could think about what I would do now, I heard a piercing scream from behind: "Stop right now! Drop a sword and raise your hands up!". They were knights. Only knights scream like that before making a hit. If they had been bandits I would be already dead.

I turned back and saw two figures in heavy armor rushing towards me. They were Lancelot and Percival — two knights of the Round Table, known for their fast reprisal over renegades like me. In the Kingdom they were called the Cleaners. As for me, not the most suitable name: they usually left a lot of dirt.

I almost instantly read their technique. Each of them was preparing for some time, then hit instantly, then was preparing again for the same time, then hit again, and so on, while their victim was not fallen. Lancelot spent n seconds to prepare, and Percival — m seconds. I was too tired and could parry a hit only if the previous one was done more than a second ago, and there were no powers to counter-attack at all. It was the sense that Lady Luck was really a hooker, and you were fresh out of cash. The knights knew their job and the first hit I wouldn't be able to parry would finish me off. My story wouldn't have a happy end.

Input

The only line contains two integers separated by a space: n and m (1 ≤ n, m ≤ 2·109) — the intervals of time in seconds between hits of Lancelot and Percival correspondingly.

Output

Output a single integer — the number of seconds from the beginning of the fight when the protagonist will be killed.

Examples
input

Copy
9 6
output
18
input

Copy
7 11
output
22
 
 
这个题的意思就是问什么时候两个数的距离最小为1或者0,然后输出数相比较而言大的那个。
这个题一定是有解的,就是a的倍数是b,b的倍数是a。最小距离是0,但是要找最优解。
所以扩展欧几里得就很OK。
如果a和b的最大公约数不是1,那么答案就是他们的最小公倍数。那么a和b的最小距离就是0的时候,直接就是ans=a*b/gcd(a,b);
如果a和b的最大公约数是1,那么利用扩展欧几里得解方程a*x+b*y=1;
求出来的x和y就是a和b对应的系数。但是这里求出来的并不是最优解,因为公式满足a*(x+k*b/gcd(a,b))+b*(y-k*a/gcd(a,b))=1;
这个肯定是成立的,因为gcd(a,b)=1,所以直接将求出来的x和y进行对b和a的取模就可以。
为了避免求出来负值,所以先加上一个b或者a然后取模就可以了。
我一开始写的时候,求出来x和y就直接输出了,并没有考虑取模,但是也水过去了。
关于扩展欧几里得,以前写过一篇水的博客,传送门:我是智障
 
代码:
 //L - Knights without Fear and Reproach-扩展欧几里得
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
ll exgcd(ll a,ll b,ll &x,ll &y){ //扩展欧几里得
if(b==){
x=;y=;
return a;
}
ll r=exgcd(b,a%b,x,y);
ll t=y;
y=x-(a/b)*y;
x=t;
return r;
} int main(){
ll n,m,ans;
scanf("%lld%lld",&n,&m);
ll x,y;
ll c=exgcd(n,m,x,y);
if(n==&&m==)ans=;
else if(n==||m==)ans=; //特判
else{
if(c!=)
ans=n*m/c;
else{
if(n*m/c>max(abs(x*n),abs(y*m))){
ans=max(abs(x*n),abs(y*m));
if(ans==abs(x*n))ans=abs(((x+m)%m)*n);
else ans=abs(((y+n)%n)*m);
}
else
ans=n*m/c;
}
}
printf("%lld\n",ans);
}

溜了,去写别的题了。

 

Codeforces Gym100812 L. Knights without Fear and Reproach-扩展欧几里得(exgcd)的更多相关文章

  1. Gym100812 L 扩展欧几里得

    L. Knights without Fear and Reproach time limit per test 2.0 s memory limit per test 256 MB input st ...

  2. 【数论】【扩展欧几里得】Codeforces 710D Two Arithmetic Progressions

    题目链接: http://codeforces.com/problemset/problem/710/D 题目大意: 两个等差数列a1x+b1和a2x+b2,求L到R区间内重叠的点有几个. 0 < ...

  3. [codeforces 200 E Tractor College]枚举,扩展欧几里得,三分

    题目出自 Codeforces Round #126 (Div. 2) 的E. 题意大致如下:给定a,b,c,s,求三个非负整数x,y,z,满足0<=x<=y<=z,ax+by+cz ...

  4. 【扩展欧几里得】BAPC2014 I Interesting Integers (Codeforces GYM 100526)

    题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...

  5. 青蛙的约会 扩展欧几里得 方程ax+by=c的整数解 一个跑道长为周长为L米,两只青蛙初始位置为x,y;(x!=y,同时逆时针运动,每一次运动分别为m,n米;问第几次运动后相遇,即在同一位置。

    /** 题目:青蛙的约会 链接:https://vjudge.net/contest/154246#problem/R 题意:一个跑道长为周长为L米,两只青蛙初始位置为x,y:(x!=y,同时逆时针运 ...

  6. 【数论】【扩展欧几里得】Codeforces Round #484 (Div. 2) E. Billiard

    题意:给你一个台球桌面,一个台球的初始位置和初始速度方向(只可能平行坐标轴或者与坐标轴成45度角),问你能否滚进桌子四个角落的洞里,如果能,滚进的是哪个洞. 如果速度方向平行坐标轴,只需分类讨论,看它 ...

  7. 【扩展欧几里得】Codeforces Round #406 (Div. 2) A. The Monster

    扩欧,a+bx=c+dx,输出x>=0且y>=0,且a+bx最小的解. 要注意不能只保证x非负,还得看看能否保证y也非负. #include<cstdio> #include& ...

  8. Codeforces 7C 扩展欧几里得

    扩展欧几里得是计算 ax + by = gcd(a,b) 的 x,y的整数解. 现在是ax + by + c = 0; 只要 -c 是 gcd(a,b) 的整数倍时有整数解,整数解是 x = x*(- ...

  9. Codeforces Round #451 (Div. 2) B. Proper Nutrition【枚举/扩展欧几里得/给你n问有没有两个非负整数x,y满足x·a + y·b = n】

    B. Proper Nutrition time limit per test 1 second memory limit per test 256 megabytes input standard ...

随机推荐

  1. c++ vector实例

    #include <iostream> #include <string> #include <vector> #include <iostream> ...

  2. memset和memcpy

    void memset(void s, int ch, size_t n); 函数解释:将s中当前位置后面的n个字节 (typedef unsigned int size_t )用 ch 替换并返回 ...

  3. linux centos6 系统优化脚本-经典

    转载一篇Ricky的系统优化脚本,这个脚本只能针对centos6x 其他还没有测试,但centos7肯定不行的 #!/bin/bash # ID 201510192126 # Author Ricky ...

  4. Android拨打电话不弹出系统拨号界面总结

    我在网上搜了一下,解决这个问题,有两种方式: 1.反射调用系统底层方法,并获取系统权限 反射调用的代码如下: Class phoneFactoryClass = Class.forName(" ...

  5. UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 19: ordinal not in range(128)

    解决方案: 1: 在网上找到的解决方案是: 在调用import matplotlib.pyplot as plt前 import sys sys.setdefaultencoding(“gbk”) 让 ...

  6. Nodejs-模块化结构

    1.模块(一个文件就是一个模块) 获取当前脚本所在的路径 _ _dirname 文件路径 _ _filename (1)创建模块(module1.js) const fs=require('fs'); ...

  7. 分布式存储系统可靠性系列五:副本放置算法 & CopySet Replication

    本文来自网易云社区 作者:孙建良 在分布式存储系统 中说明了,在一定情况下,copyset的数量不是越多越好,在恢复时间确定的情况下,找到合适的copyset的数量可以降低数据丢失的概率. 在分布式存 ...

  8. Python数据结构之列表、元组及字典

    一位大牛Niklaus Wirth曾有一本书,名为<Algorithms+Data Structures=Programs>,翻译过来也就是算法+数据结构=程序.而本文就是介绍一下Pyth ...

  9. CentOS6.4编译Hadoop-2.4.0

      因为搭建Hadoop环境的时候,所用的系统镜像是emi-centos-6.4-x86_64,是64位的,而hadoop是默认是32的安装包.这导致我们很多操作都会遇到这个问题(Java HotSp ...

  10. c++ primer 6 练习题 (非复习题)

    第7章 7.13-1调和平均数 //7.13-1 excise.cpp 调和平均数 #include <iostream> double calculate(double a,double ...