Gym100812 L 扩展欧几里得
2.0 s
256 MB
standard input
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.
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 a single integer — the number of seconds from the beginning of the fight when the protagonist will be killed.
9 6
18
7 11
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 扩展欧几里得的更多相关文章
- POJ 1061 青蛙的约会 扩展欧几里得
扩展欧几里得模板套一下就A了,不过要注意刚好整除的时候,代码中有注释 #include <iostream> #include <cstdio> #include <cs ...
- poj 1061 扩展欧几里得解同余方程(求最小非负整数解)
题目可以转化成求关于t的同余方程的最小非负数解: x+m*t≡y+n*t (mod L) 该方程又可以转化成: k*L+(n-m)*t=x-y 利用扩展欧几里得可以解决这个问题: eg:对于方程ax+ ...
- hdu 5512 Pagodas 扩展欧几里得推导+GCD
题目链接 题意:开始有a,b两点,之后可以按照a-b,a+b的方法生成[1,n]中没有的点,Yuwgna 为先手, Iaka后手.最后不能再生成点的一方输: (1 <= n <= 2000 ...
- 【数论】【扩展欧几里得】Codeforces 710D Two Arithmetic Progressions
题目链接: http://codeforces.com/problemset/problem/710/D 题目大意: 两个等差数列a1x+b1和a2x+b2,求L到R区间内重叠的点有几个. 0 < ...
- 扩展欧几里得 POJ 1061
感觉这道题目的数据好水啊...我的代码我都觉得姿势特别奇怪...竟然还过了... 好吧,原来不是姿势奇怪,而是逆元需要用的时候是余数也需要的时候,这里的余数是不需要的,所以就AC了 就说一下碰到的问题 ...
- pku 1061 青蛙的约会 扩展欧几里得
青蛙的约会Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 120482 Accepted: 25449Description 两只青 ...
- POJ1061 青蛙的约会(扩展欧几里得)
题目链接:http://poj.org/problem?id=1061 青蛙的约会 Time Limit: 1000MS Memory Limit: 10000K Total Submission ...
- [P1516]青蛙的约会 (扩展欧几里得/中国剩余定理?)
每日做智推~ 一看就是一道数学题. 再看是一道公约数的题目. 标签是中国孙子定理. 题解是扩展欧几里得 (笑) 一开始没看数据范围 只有50分 开一个longlong就可以了 #include< ...
- J - 青蛙的约会(扩展欧几里得)
https://vjudge.net/contest/218366#problem/J 第一步追及公式要写对:y+nk-(x+mk)=pL => (n-m)k+lp=x-y 可以看出扩展欧几里得 ...
随机推荐
- 增大PHP允许上传的文件大小;解决POST Content-Length exceeds the limit
在php.ini中: upload_max_filesize = 1000M ;1GB post_max_size = 1000M 然后重启apache 参考链接
- IIS中不让下级应用程序继承主域名的web.config配置
<location path="." allowOverride="true" inheritInChildApplications="fals ...
- $.extend(x,y); 函数用法介绍。
第一篇资料: 转自: https://www.cnblogs.com/yuqingfamily/p/5813650.html 语法:jQuery.extend( [deep ], target, o ...
- git创建分支及日常使用
克隆代码 git clone https://github.com/master-dev.git 查看所有分支 git branch --all # 默认只有master分支,所以会看到如下两个分支 ...
- InChatter系统之客户端实现原理与阶段小结
InChatter客户端的开发可以说是目前系统的阶段性结尾了.很抱歉的是,这篇文章来的这么晚,迟到了这么久. 在客户端的开发主要针对两个方面: 消息的传输与处理 消息的UI交互处理 一.消息的传输与处 ...
- ButterKnife 在父类 点击事件没反应的解决方案
在用继承的方式实现butterKnife的封装的时候遇到问题, butterKnife就在baseActivity中绑定的,但是父类中公共控件点击事件无效.找了半天原因,原来是子类和父类定义的点击方法 ...
- emil 的使用
摘抄自别人 RFC882文档规定了如何编写一封简单的邮件(纯文本邮件),一封简单的邮件包含邮件头和邮件体两个部分,邮件头和邮件体之间使用空行分隔. 邮件头包含的内容有: from字段 --用于指明发 ...
- gifsicle for linux ----------gif 图像处理
1.gifsicle 在linux 中的使用下载gifsicle yum install gifsicle 若发现没有此包 ,更新epel第三方软件库 sudo yum install epel-re ...
- Delphi最简化异步选择TCP服务器
网上Delphi的Socket服务器优良代码,实在少见,索性写个简化的异步Socket服务器,虽然代码较少,但却该有的都有了,使用的是异步选择WSAAsyncSelect,减少了编写线程的繁琐.可能会 ...
- Java 调用存储过程 返回结果集
这里使用Oracle数据库的thin连接. 下面是存储过程SQL 1 createorreplaceprocedure proc3(stid in student.stuid%type, stname ...