zoukankan      html  css  js  c++  java
  • Codeforces Gym100812 L. Knights without Fear and Reproach-扩展欧几里得(exgcd)

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

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

    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就直接输出了,并没有考虑取模,但是也水过去了。
    关于扩展欧几里得,以前写过一篇水的博客,传送门:我是智障
     
    代码:
     1 //L - Knights without Fear and Reproach-扩展欧几里得
     2 #include<iostream>
     3 #include<cstring>
     4 #include<cstdio>
     5 #include<algorithm>
     6 using namespace std;
     7 typedef long long ll;
     8 ll exgcd(ll a,ll b,ll &x,ll &y){     //扩展欧几里得
     9     if(b==0){
    10         x=1;y=0;
    11         return a;
    12     }
    13     ll r=exgcd(b,a%b,x,y);
    14     ll t=y;
    15     y=x-(a/b)*y;
    16     x=t;
    17     return r;
    18 }
    19 
    20 int main(){
    21     ll n,m,ans;
    22     scanf("%lld%lld",&n,&m);
    23     ll x,y;
    24     ll c=exgcd(n,m,x,y);
    25     if(n==1&&m==1)ans=1;
    26     else if(n==1||m==1)ans=2;     //特判
    27     else{
    28         if(c!=1)
    29             ans=n*m/c;
    30         else{
    31             if(n*m/c>max(abs(x*n),abs(y*m))){
    32                 ans=max(abs(x*n),abs(y*m));
    33                 if(ans==abs(x*n))ans=abs(((x+m)%m)*n);
    34                 else ans=abs(((y+n)%n)*m);
    35             }
    36             else
    37                 ans=n*m/c;
    38         }
    39     }
    40     printf("%lld
    ",ans);
    41 }

    溜了,去写别的题了。

     
  • 相关阅读:
    Android:TabHost导航栏
    java:StringUtil工具类
    Android进阶篇MediaPlayer
    Android:图片滚轮
    Android:EditText焦点触发布局隐藏以及显示
    Android:Spinner的使用
    ASP.NET编程模型的理解
    ASP.NET页面事件(页面生命周期)
    根据用户喜欢的爱好选择不同风格CSS(ViewState)
    ASP.NET的页面指令
  • 原文地址:https://www.cnblogs.com/ZERO-/p/8604442.html
Copyright © 2011-2022 走看看