HDU-3049-Data Processing(逆元+打表)

Data Processing

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
Hint

Hint: You may use “scanf” to input the data.

解题思路:

还是一个求逆元的题目。但是之前先对2的N次方做一个打表,然后0(n)查询降低复杂度,最后求N的逆元。

AC代码:

#include <iostream>
#include <cstdio>
#include <math.h>
#include <vector>
#include <queue>
#include <algorithm>
#include <map>
#include <cstring>
using namespace std;
typedef long long ll;
const int N = 1e6+10;

ll a[N];
ll pow2[N];

const int mod = 1000003;
long long quickpow(long long a, long long b) {
    if (b < 0) return 0;
    long long ret = 1;
    a %= mod;
    while(b) {
        if (b & 1) ret = (ret * a) % mod;
        b >>= 1;
        a = (a * a) % mod;
    }
    return ret;
}

long long inv(long long a) {
    return quickpow(a, mod - 2);
}
void pow22()
{
    pow2[0] = 1;
    for(int i = 1 ; i <= 400000 ; i++)
        pow2[i] = (pow2[i-1]<<1)%mod;
}

int main()
{
    pow22();
    int t;
    scanf("%d",&t);
    int cas = 1;
    while(t--)
    {
        ll n;
        ll sum = 0;
        scanf("%lld",&n);
        for(int i = 0 ; i < n; i++)
        {
            scanf("%lld",&a[i]);
            sum += pow2[a[i]];
        }
        sum %= mod;
        sum = (sum*inv(n))%mod;
        cout<<"Case "<<cas++<<":"<<sum<<endl;

    }
}

发布了104 篇原创文章 · 获赞 7 · 访问量 4108

猜你喜欢

转载自blog.csdn.net/qq_43461168/article/details/102980072