HDU 1576 A/B(拓展歐幾裡得)

來源:互聯網
上載者:User

標籤:des   style   http   color   os   io   資料   ar   

Problem Description要求(A/B)%9973,但由於A很大,我們只給出n(n=A%9973)(我們給定的A必能被B整除,且gcd(B,9973) = 1)。 
Input資料的第一行是一個T,表示有T組資料。
每組資料有兩個數n(0 <= n < 9973)和B(1 <= B <= 10^9)。 
Output對應每組資料輸出(A/B)%9973。 
Sample Input
21000 5387 123456789
 
Sample Output
79226060
 
Authorxhd 
SourceHDU 2007-1 Programming Contest 屬於拓展歐幾裡得演算法:
void gcd(int a,int b,int &x,int &y)//擴充gcd,可以求出gcd(a,b)以及ax+by=gcd(a,b)中x,y的值  {    if(!b)    {        x=1;        y=0;        return ;    }    else    {        gcd(b,a%b,x,y);        int temp=x;        x=y;        y=temp-a/b*y;    }}

有了這個基礎。 由於gcd(b,9973)=bx1+9973y1=1所以可得一組(x1,y1)然後求(a/b)%9973,a/b=x,所以a=bx,有n=a%9973=a-a/9973*9973=bx-9973y;
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<limits.h>typedef long long LL;using namespace std;const int MOD=9973;void gcd(int a,int b,int &x,int &y){    if(!b)    {        x=1;        y=0;        return ;    }    else    {        gcd(b,a%b,x,y);        int temp=x;        x=y;        y=temp-a/b*y;    }}int main(){    int t,n,b,x,y;    scanf("%d",&t);    while(t--)    {        scanf("%d%d",&n,&b);        gcd(b,MOD,x,y);        x*=n;        int tep=(x%MOD+MOD)%MOD;        printf("%d\n",tep);    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.