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. .NET通过字典给类赋值

    /// <summary> /// /// </summary> /// <typeparam name="T"></typeparam& ...

  2. 开源一个Mac漂亮的小工具 PPRows for Mac, 在Mac上优雅的计算你写了多少行代码

    开源一个Mac漂亮的小工具 PPRows for Mac, 在Mac上优雅的计算你写了多少行代码. 开源地址: https://github.com/jkpang/PPRows

  3. 掌握Spark机器学习库-05-spark中矩阵与向量的使用

    1)介绍 矩阵: Matrix,看做二维表,基本运算(+,-,*,T) 向量: Vectors,方向和大小,基本运算,范数 2)spark中向量的使用(主要使用breeze.linalg) 3)spa ...

  4. Asp.Net控件的客户端命名

    我们在用ASP.NET写出来的网页,用浏览器来查看生成的客户端代码的时候经常看到这样的代码:GridView1_ctl101_WebUserControl1_webuserControlButton, ...

  5. linux 后台进程

    1.进程放入后台 ctrl+z 进程放入后台 暂停执行 2.进程放入后台执行 bg % n 或者 bg n 进程放入后台执行 3.进程取出前台执行 fg % n 或者 fg n 进程取出前台执行 4. ...

  6. 最小生成树Prim算法 Kruskal算法

    Prim算法(贪心策略)N^2 选定图中任意定点v0,从v0开始生成最小生成树 树中节点Va,树外节点Vb 最开始选一个点为Va,其余Vb, 之后不断加Vb到Va最短距离的点 1.初始化d[v0]=0 ...

  7. 字符集匹配:\s 匹配一个空格,一边后面加量词表示多个空格,\s*表示0个以上空格,\s+表示1个以上空格,\s相当于[\f\r\n\t ]5种空白字符。

    字符集匹配:\s 匹配一个空格,一边后面加量词表示多个空格,\s*表示0个以上空格,\s+表示1个以上空格,\s相当于[\f\r\n\t ]5种空白字符.

  8. TX-LCN事务控制原理

    TX-LCN由两大模块组成, TxClient.TxManager,TxClient作为模块的依赖框架,提供TX-LCN的标准支持,TxManager作为分布式事务的控制方.事务发起方或者参与方都由T ...

  9. JavaScript exec()方法

    exec() 方法用于检索字符串中的正则表达式的匹配.返回一个数组,其中存放匹配的结果.如果未找到匹配,则返回值为 null. var str = "我今年25岁明年26岁后年27岁千年24 ...

  10. HDOJ 1846 Brave Game - 博弈入门

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1846 经典基础博弈,首先面对(m+1)的人一定会输,依次往后推即可: #include<iost ...