HDU 1576 扩展欧几里得&&暴力

来源:互联网 发布:tiny core linux 中文 编辑:程序博客网 时间:2024/06/07 03:12

HDU 1576

A - A
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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

这个题,题目很机智的给了一个很小的MOD,所以我们可以暴力

姿势如下:

#include <iostream>#include <cstdio>#include <algorithm>#include <string>#include <cstring>#include <cmath>using namespace std;const int maxn=1e5+10;const int INF=0x3f3f3f3f;int main(){    int t;    long long n,b;    cin>>t;    while(t--){        cin>>n>>b;        for(int i=1;i<9973;i++)            if((i*b-n)%9973==0){                cout<<i<<endl;                break;            }    }}

换一种正常的姿势,即扩展欧几里得:

#include <iostream>#include <cstdio>#include <cmath>#include <cstring>#include <string>#include <cstdlib>using namespace std;const long long mod=9973;void exgcd(long long a,long long &x,long long b,long long  &y){    if(!b){        x=1;        y=0;    }    else{        exgcd(b,y,a%b,x);        y-=x*(a/b);    }}int main(){    long long t,N,B,x,y;    cin>>t;    while(t--){        cin>>N>>B;        exgcd(B,x,mod,y);        long long ans=((x%mod)*(N%mod))%mod;        while(ans<0) ans+=mod;        cout<<ans<<endl;    }    return 0;}



0 0
原创粉丝点击