Data Processing

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Wastematerial/article/details/45622497
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 (n1n2n3, … ), 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 n1n2n3 … ( 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

这题只要用下快速幂和乘法逆元便可;

扫描二维码关注公众号,回复: 4653355 查看本文章
#include <stdio.h>
#define mod 1000003
int kuaisumi(int x,int y)
{
    long long sum=1,xx;
    xx=x;//xx可能会超int的范围故用xx代替
    while(y)
    {
        if(y%2!=0)
            sum*=xx;
        y/=2;
        xx=(xx%mod)*(xx%mod)%mod;
        sum=sum%mod;
    }
    return sum;
}

void exgcd(long long a,long long b,long long &x,long long &y)
{
    if(b==0)
    {
        x=1;
        y=0;
        return ;
    }
    exgcd(b,a%b,x,y);
    long long tt;
    tt=x%mod;
    x=y%mod;
    y=(tt-(a/b)*x)%mod;
}
int main()
{
    int t;
    scanf("%d",&t);
    int cas=0;
    while(t--)
    {
        int n;
        scanf("%d",&n);
        long long s=0;
        for(int i=1;i<=n;i++)
        {
            int mm;
            scanf("%d",&mm);
            s=(s+kuaisumi(2,mm))%mod;
        }
        long long x,y;
        exgcd(n,mod,x,y);
        while(x<0)//拓展欧几里得得到的x可能为负数
            x+=mod;
        s=s*x%mod;
        cas++;
        printf("Case %d:%lld\n",cas,s);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Wastematerial/article/details/45622497