hdu 1576 A/B||hdu 3049 Data Processing 乘法逆元

A/B

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3286    Accepted Submission(s): 2492


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
  
  
2 1000 53 87 123456789
 

Sample Output
  
  
7922 6060

乘法逆元:

定义:

满足a*k≡1 (mod p)的k值就是a关于p的乘法逆元。
用途:
当我们要求(a/b) mod p的值,且a很大,无法直接求得a/b的值时,我们就要用到乘法逆元。
我们可以通过求b关于p的乘法逆元k,将a乘上k再模p,即(a*k) mod p。其结果与(a/b) mod p等价。

code:

#include<stdio.h>
#define MOD 9973
long long extend_gcd(long long a,long long b,long long &x,long long &y)
{
    if(b==0){x=1;y=0;return a;}
    long long d=extend_gcd(b,a%b,y,x);
    y-=a/b*x;
    return d;
}
//*********求逆元*******************
long long mod_reverse(long long a,long long n)
{
    long long x,y;
    long long d=extend_gcd(a,n,x,y);
    if(d==1) return (x%n+n)%n;
    else return -1;
}
//(A/B)%9973->(A*b)%9973->(A%9973*b)%9973->(n*b)%9973//其中b是B的乘法逆元
int main()
{
    int T;
    int n,B;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&B);
        int x=mod_reverse(B,MOD);
        printf("%d\n",n*x%MOD);
    }
    return 0;
}


Data Processing

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


Problem Description
Chinachen is a football fanatic, and his favorite football club is Juventus fc. In order to buy a ticket of Juv, he finds a part-time job in Professor Qu’s lab.
And now, Chinachen have received an arduous task——Data Processing.
The data was made up with N positive integer (n1, n2, n3, … ), he may calculate the number , you can assume mod N =0. Because the number is too big to count, so P mod 1000003 is instead.
Chinachen is puzzled about it, and can’t find a good method to finish the mission, so he asked you to help him.
 

Input
The first line of input is a T, indicating the test cases number.
There are two lines in each case. The first line of the case is an integer N, and N<=40000. The next line include N integer numbers n1,n2,n3… (ni<=N).
 

Output
For each test case, print a line containing the test case number ( beginning with 1) followed by the P mod 1000003.
 

Sample Input
  
  
2 3 1 1 3 4 1 2 1 4
 

Sample Output
  
  
Case 1:4 Case 2:6

思路(1):乘法逆元

code:

#include<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;
const __int64 mod=1000003;
const __int64 ss=40010;
__int64 num[40100];
__int64 x,y;
void init()
{
    num[1]=2;
    for(int i=2;i<=ss;i++)
        num[i]=(num[i-1]*2)%mod;
}

 __int64 ext_gcd(__int64 a,__int64 b)//返回值为最大公约数
{
    __int64 tem;
    if(b==0)
    {
        x=1;y=0;
        return a;
    }
    __int64 r=ext_gcd(b,a%b);
    tem=x;
    x=y;
    y=tem-a/b*y;
    return r;
}
int main()
{
    init();
    __int64 cas,cas1=0,n,t,ans,d,nn;
    scanf("%I64d",&cas);
    while(cas--)
    {
        cas1++;
        ans=0;
        scanf("%I64d",&n);
        nn=n;
        while(nn--)
        {
            scanf("%I64d",&t);
            ans+=num[t];
            ans%=mod;
        }
        ext_gcd(n,mod);
        x=(x%mod+mod)%mod;
        ans=ans*x%mod;
       printf("Case %I64d:%I64d\n",cas1,ans);
    }
    return 0;
}

思路(2):要求sum/n%mod,直接计算数据太大超范围;由于sum%n==0 

所以sum/n%mod == sum/n%(n*mod)这样就能直接计算;

#include<bits/stdc++.h>
using namespace std;
int main()
{
    __int64 ans,n,mod,i,t,num[40010],cas,cas1=0;
    scanf("%I64d",&cas);
    while(cas--)
    {
        mod=1000003;
        cas1++;
        ans=0;
        scanf("%I64d",&n);
        mod*=n;
        num[0]=1;
        for(i=1;i<=40000;i++)
            num[i]=(num[i-1]*2%mod);
        for(i=0;i<n;i++)
        {
            scanf("%I64d",&t);
            ans+=num[t];
            ans%=mod;
        }
        ans/=n;
        printf("Case %I64d:%I64d\n",cas1,ans);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值