Data Processing
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
Chinachen is puzzled about it, and can't find a good method to finish the mission, so he asked you to help him.
InputThe first line of input is a T
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 ).
OutputFor each test case, print a line containing the test case number ( beginning with 1) followed by the P
2
3
1 1 3
4
1 2 1 4
Case 1:4
Case 2:6
這題只要用下快速冪和乘法逆元便可;
#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;
}