zoukankan      html  css  js  c++  java
  • 欧几里得模板题目

    Romantic

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 4147    Accepted Submission(s): 1727


    Problem Description
    The Sky is Sprite.
    The Birds is Fly in the Sky.
    The Wind is Wonderful.
    Blew Throw the Trees
    Trees are Shaking, Leaves are Falling.
    Lovers Walk passing, and so are You. 
    ................................Write in English class by yifenfei

     

    Girls are clever and bright. In HDU every girl like math. Every girl like to solve math problem!
    Now tell you two nonnegative integer a and b. Find the nonnegative integer X and integer Y to satisfy X*a + Y*b = 1. If no such answer print "sorry" instead.
     
    Input
    The input contains multiple test cases.
    Each case two nonnegative integer a,b (0<a, b<=2^31)
     
    Output
    output nonnegative integer X and integer Y, if there are more answers than the X smaller one will be choosed. If no answer put "sorry" instead. 
     
    Sample Input
    77 51
    10 44
    34 79
     
    Sample Output
    2 -3
    sorry
    7 -3
    题意:给出a,b; 求满足 ax + by = 1的 x 和 y值,其中x非负,多解时输出x最小的那一个
     
    直接按照扩展欧几里得求解就ok了,x的通解: x = x0 + b / d * t,其中b / d 都正数,x 和 t成正比,当x0为正数的时候 t == 0,有最小值;当x0为负数的时候,x最小就是0,与横轴相交,所以可以求出 x0 + b / t * t = 0时候的 t,因为此时 t是整数,不一定是准确相交的那一个点,所以当前的x可能是负数,如果为负数,t++,取下一个整数点
     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cstring>
     4 #include <cstdio>
     5 using namespace std;
     6 int a,b,x,y,d;
     7 int gcd(int a, int b)
     8 {
     9     if(b == 0)
    10     {
    11         x = 1;
    12         y = 0;
    13         return a;
    14     }
    15     d = gcd(b, a % b);
    16     int temp = x;
    17     x = y;
    18     y = temp - a / b * y;
    19     return d;
    20 }
    21 int main()
    22 {
    23     while(scanf("%d%d", &a, &b) != EOF)
    24     {
    25         d = gcd(a, b);
    26         if(1 % d)   // 如果c % gcd == 1就无解
    27         {
    28             printf("sorry
    ");
    29             continue;
    30         }
    31         x = x / d;  // 特解
    32         y = y / d;
    33         if(x > 0)  // x 大于0直接输出
    34             printf("%d %d
    ",x, y);
    35         else
    36         {
    37             int t = (-1) * x / b * d;
    38             //cout<<x<<" "<<b<<" "<<d<<endl;
    39             int k = x + b / d * t;  //当前的t下的x值
    40             if(k < 0) 
    41             {
    42                 t++; //当前的t所求的x为负,取下一个点
    43 
    44             }
    45             printf("%d %d
    ", x + b / d * t, y - a / d * t);
    46 
    47         }
    48     }
    49     return 0;
    50 }
    View Code
      http://acm.hdu.edu.cn/showproblem.php?pid=1576
    题意:要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973) = 1)。
    分析:假设(A / B) % 9973 = k,那么A / B = 9973 * t + k,A能整除B,所以 A = ( 9973 * t + k) * B;又n = A % 9973所以 A = 9973 * y + n
    所以 ( 9973 * t + k) * B = 9973 * y + n,(9973 * t + k) / n * B + 9973 *( -y ) / n = gcd( B,9973) = 1;直接由扩展欧几里得定理解出 ( 9973 * t + k ) / n,要求的就是乘上n之后对9973取余,用long long 类型
     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cstring>
     4 #include <cstdio>
     5 using namespace std;
     6 typedef long long LL;
     7 LL n,B,a,b,c,d;
     8 LL x,y;
     9 LL gcd(LL a, LL b)
    10 {
    11     if(b == 0)
    12     {
    13         x = 1;
    14         y = 0;
    15         return a;
    16     }
    17     d = gcd(b, a % b);
    18     LL temp = x;
    19     x = y;
    20     y = temp - a / b * y;
    21     return d;
    22 }
    23 int main()
    24 {
    25     int test;
    26     scanf("%d", &test);
    27     while(test--)
    28     {
    29         scanf("%I64d%I64d", &n, &B);
    30         a = B;
    31         b = 9973;
    32         c = 1;
    33         d = gcd(a, b);
    34         x = x * (c / d);
    35         y = y * (c / d);
    36         if(x > 0)
    37         {
    38             printf("%I64d
    ", n % 9973 * (x % 9973) % 9973);
    39         }
    40         else
    41         {
    42             LL t = d / b * (-1) * x;
    43             LL k = x + b / d * t;
    44             if(k < 0)
    45             {
    46                 t++;
    47             }
    48             x = x + b / d * t;
    49             printf("%I64d
    ", n % 9973 * (x % 9973) % 9973);
    50         }
    51     }
    52     return 0;
    53 }
    View Code

     http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4712求最小整数逆元

    则称x是a关于m的乘法逆元

    该写法可以写成 ax  + my = 1,所以就是一个扩展的欧几里得而已,其中c = 1;

     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 #include <algorithm>
     5 using namespace std;
     6 int a,b,d;
     7 int x,y;
     8 int gcd(int a, int b)
     9 {
    10     if(b == 0)
    11     {
    12         x = 1;
    13         y = 0;
    14         return a;
    15     }
    16     d = gcd(b, a % b);
    17     int temp = x;
    18     x = y;
    19     y = temp - a / b * y;
    20     return  d;
    21 }
    22 int main()
    23 {
    24     int test;
    25     scanf("%d", &test);
    26     while(test--)
    27     {
    28         scanf("%d%d", &a, &b);
    29         d = gcd(a, b);
    30         if(1 % d)
    31         {
    32             printf("Not Exist
    ");
    33         }
    34         else
    35         {
    36             int t = - d / b * x;
    37             int k = x + b * t;
    38             if(k <= 0)
    39             {
    40                 k += b;;
    41             }
    42             printf("%d
    ", k);
    43         }
    44     }
    45     return 0;
    46 }
    View Code
  • 相关阅读:
    python内建eval, exec 和exec函数的用法
    wxPython控件学习之wx.grid.Grid (包括对GridCellEditor和GridCelRender的扩展,以支持更多的grid cell 样式, 以GridCellColorEditor为例)
    wxPython控件学习之wx.ComboBox
    tar 解压缩命令
    ios是否安装了某应用
    iPhone IOS中DEB如何安装
    一种简便获取iPhone IMEI 的方法
    iOSOpenDevtroubleshoot
    Mac&iOS Socket编程
    Debian Binary Package Building HOWTO
  • 原文地址:https://www.cnblogs.com/zhaopAC/p/5206891.html
Copyright © 2011-2022 走看看