L. Knights without Fear and Reproach
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

题意  两个人分别隔 n 和 m 秒砍一刀 主角每闪避一次攻击需要休息一秒  问主角在第几秒被砍死

解析  根据题意我们可以得到 |x*n-m*y|<=1  
一共两组情况
第一种 |x*n-m*y|=0 x*n=m*y 最优解就是n和m的最小公倍数=n*m/gcd(n,m)
第二种
|x*n-m*y|=1 我们令 r = |x*n - m*y| 可以推出 r=gcd(n,m)*|(x*n/gcd(n,m)-(y*m/gcd(n,m)|=gcd(n,m)*k
所以差值r肯定是gcd(n,m)的倍数 当最大公约数不是1的时候第二种情况不存在
当最大公约数为1时 之前的方程|x*n-m*y|=1(x,y为正整数)可以转化为 n*x+m*y=1 (扩展欧几里得)
跑一边 扩展欧几里得算法 可以得出一组解(x0,y0)



我错认为最优解就是max(abs(n*x0),abs(y0*m))
交上去居然过了 可能数据比较水 有人模拟也过了。。。(想不通)


但是后来我又仔细想了想 扩展欧几里得算法 得出来的只是一组解 怎么能确定最优解是
max(abs(n*x0),abs(y0*m))?
难道就没有比他们更小的解吗?然后就想到最优解应该是|x*n|或者|m*y|越小越好 n>0,m>0所以|x|或|y|越小越好
因为 他们相差1 所以单独考虑一个x就好了 查阅资料得到了x的通项公式x=x0+k*m/gcd(n,m)
当x>0是 最小正整数解为 x%m 但并不能保证是绝对值最小的解 如果x%m<=m/2 x*n是最优解 反之(x-m)*n+1是最优解
当x<0时 最小负整数解为(x+m)%m 同理

我们再换个角度想 n*x+m*y=1
说明 n*x>0,m*y<0 或者 n*x<0,m*y>0
所以我们只需要求x的最小正整数解x' 和y的最小正整数解y'
相关博客
http://blog.csdn.net/zhjchengfeng5/article/details/7786595
最优解就是min(x'*n,y'*m)
而不用像上面所说的 去找x绝对值最小的解

思维一点一点的进步。。这应该是标准答案了吧。。。
AC代码1
 #include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <vector>
using namespace std;
const int maxn = +;
const int maxm = 1e4+;
const int inf = 0x3f3f3f3f;
const int mod = ;
const double epx = 1e-;
typedef long long ll;
const ll INF = 1e18;
ll n,m,t;
ll exgcd(ll a,ll b,ll &x,ll &y) //&引用符号,修改x,y,函数返回的是a,b的最大公约数
{
if(b==)
{
x=;
y=;
return a;
}
ll r=exgcd(b,a%b,x,y); //递归下去
ll temp=x;
x=y;
y=temp-a/b*y;
return r;
}
int main()
{
cin>>n>>m;
ll x,y;
ll g=exgcd(n,m,x,y);
ll ans=n*m/g;
if(n==||m==)
{
if(n==m)
cout<<""<<endl;
else
cout<<""<<endl;
return ;
}
if(g==)
ans=min(ans,max(abs(x*n),abs(y*m)));
cout<<ans<<endl;
}

AC代码2

 #include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <vector>
using namespace std;
const int maxn = +;
const int maxm = 1e4+;
const int inf = 0x3f3f3f3f;
const int mod = ;
const double epx = 1e-;
typedef long long ll;
const ll INF = 1e18;
ll n,m,t;
ll exgcd(ll a,ll b,ll &x,ll &y) //&引用符号,修改x,y,函数返回的是a,b的最大公约数
{
if(b==)
{
x=;
y=;
return a;
}
ll r=exgcd(b,a%b,x,y); //递归下去
ll temp=x;
x=y;
y=temp-a/b*y;
return r;
}
ll abss(ll a)
{
if(a<)
return -a;
else
return a;
}
int main()
{
cin>>n>>m;
ll x,y;
ll g=exgcd(n,m,x,y);
ll ans=n*m/g;
if(n==||m==)
{
if(n==m)
cout<<""<<endl;
else
cout<<""<<endl;
return ;
}
if(g==)
{
if(x>)
{
x=x%m;
if(x>m/)
{
x-=m;
ans=min(ans,abss(x*n)+);
}
else
ans=min(ans,x*n);
}
else if(x<)
{
x=x%m;
if(abss(x)>m/)
{
x+=m;
ans=min(ans,x*n);
}
else
ans=min(ans,abss(x*n)+);
}
}
cout<<ans<<endl;
}

AC代码3(最完美)

 #include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <vector>
using namespace std;
const int maxn = +;
const int maxm = 1e4+;
const int inf = 0x3f3f3f3f;
const int mod = ;
const double epx = 1e-;
typedef long long ll;
const ll INF = 1e18;
ll n,m,t;
ll exgcd(ll a,ll b,ll &x,ll &y) //&引用符号,修改x,y,函数返回的是a,b的最大公约数
{
if(b==)
{
x=;
y=;
return a;
}
ll r=exgcd(b,a%b,x,y); //递归下去
ll temp=x;
x=y;
y=temp-a/b*y;
return r;
}
int main()
{
cin>>n>>m;
ll x,y;
ll g=exgcd(n,m,x,y);
ll ans=n*m/g;
if(n==||m==)
{
if(n==m)
cout<<""<<endl;
else
cout<<""<<endl;
return ;
}
if(g==)
{
ll ans1=((x%m+m)%m)*n;
ll ans2=((y%n+n)%n)*m;
ans=min(ans,min(ans1,ans2));
}
cout<<ans<<endl;
}

一开始没看题意看样例认为只是单纯的输出最大值的二倍,读一遍题暴力模拟了一发,超时了。

为什么人家的模拟就过了。。应该超时的啊 只能说数据水了

引发了这么长的血案。。

 

Gym100812 L 扩展欧几里得的更多相关文章

  1. POJ 1061 青蛙的约会 扩展欧几里得

    扩展欧几里得模板套一下就A了,不过要注意刚好整除的时候,代码中有注释 #include <iostream> #include <cstdio> #include <cs ...

  2. poj 1061 扩展欧几里得解同余方程(求最小非负整数解)

    题目可以转化成求关于t的同余方程的最小非负数解: x+m*t≡y+n*t (mod L) 该方程又可以转化成: k*L+(n-m)*t=x-y 利用扩展欧几里得可以解决这个问题: eg:对于方程ax+ ...

  3. hdu 5512 Pagodas 扩展欧几里得推导+GCD

    题目链接 题意:开始有a,b两点,之后可以按照a-b,a+b的方法生成[1,n]中没有的点,Yuwgna 为先手, Iaka后手.最后不能再生成点的一方输: (1 <= n <= 2000 ...

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

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

  5. 扩展欧几里得 POJ 1061

    感觉这道题目的数据好水啊...我的代码我都觉得姿势特别奇怪...竟然还过了... 好吧,原来不是姿势奇怪,而是逆元需要用的时候是余数也需要的时候,这里的余数是不需要的,所以就AC了 就说一下碰到的问题 ...

  6. pku 1061 青蛙的约会 扩展欧几里得

    青蛙的约会Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 120482 Accepted: 25449Description 两只青 ...

  7. POJ1061 青蛙的约会(扩展欧几里得)

    题目链接:http://poj.org/problem?id=1061 青蛙的约会 Time Limit: 1000MS   Memory Limit: 10000K Total Submission ...

  8. [P1516]青蛙的约会 (扩展欧几里得/中国剩余定理?)

    每日做智推~ 一看就是一道数学题. 再看是一道公约数的题目. 标签是中国孙子定理. 题解是扩展欧几里得 (笑) 一开始没看数据范围 只有50分 开一个longlong就可以了 #include< ...

  9. J - 青蛙的约会(扩展欧几里得)

    https://vjudge.net/contest/218366#problem/J 第一步追及公式要写对:y+nk-(x+mk)=pL => (n-m)k+lp=x-y 可以看出扩展欧几里得 ...

随机推荐

  1. sdut2784&cf 126b Good Luck!(next数组)

    链接 next数组的巧妙应用  学弟出给学弟的学弟的题.. 求最长的 是前缀也是后缀同时也是中缀的串  next的数组求的就是最长的前后缀 但是却不能求得中缀 所以这里 就把尾部去掉之后再求 这样就可 ...

  2. 【深入.NET平台】浅谈.NET Framework基元类型

    什么是基元类型? 初学者可能很少听说过这个名词,但是平时用得最多的肯定是基元类型.先看下面两行代码: System.Int32 a = ; ;  上面两行代码都表示声明一个int类型的变量,但在平时写 ...

  3. [BZOJ1192][HNOI2006]鬼谷子的钱袋 数学

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1192 大水题,把m分成二的幂次方和. #include<cstdio> #in ...

  4. Vue 拦截器的使用

    拦截器 可以全局进行拦截器设置.拦截器在发送请求前或响应返回时做一些特殊的处理. 拦截器的注册 Vue.http.interceptors.push({ request: function ( req ...

  5. vue2.0 vue.extend()的拓展

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. 前端入门22-讲讲模块化 包括webstrom建立简单ES6

    https://www.cnblogs.com/dasusu/p/10105433.html

  7. MySQL系列(一)--数据类型

    如何选择优化的数据类型: 1.通常更小的更好 相同级别的数据类型,选择占据空间更小的数据类型.更小的数据类型通常更快,因为占用更少的磁盘.内存和CPU缓存,处理时需要的 CPU周期也更少,但是要确保需 ...

  8. idea文件全部变红, 文件全部红色

    idea如果当前project用了版本控制器,其下面新建的所有的项目默认都是加入到版本控制里面,所以项目名称和文件都是红色的,如图: 看起来非常不爽, 那么如何解决呢? File–>Settin ...

  9. Analysis Of The Causes Of Internal Symmetry Of Hydraulic Motor

    The main reasons why hydraulic motors have this symmetrical internal structure are as follows: The   ...

  10. weblogic启动 web应用ssh关闭 nohup命令

    平时我们操作linux服务器的时候,都是通过ssh远程连接,然后启动服务器上的服务的,所以有时候启动weblogic,我们关闭ssh,weblogic 服务也相应的关闭了,那么我们就只能用nohup这 ...