洛谷1630:求和

洛谷1630:求和

题意描述

  • \(1^b+2^b+...+a^b\)取余\(10000\)的结果。

数据范围

  • \(a,b\leq 10^9\).

思路

  • 模数很小,对于\(a^xmod10000=(amod10000)^x\)
  • 所以预处理出来模数的前缀和就可。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e4;
int T;
ll a, b, sum[mod + 10];

ll qmi(ll a, ll b, ll p)
{
    ll res = 1; res %= p;
    while(b)
    {
        if(b & 1) res = res * a % p;
        a = a * a % p;
        b >>= 1;
    }
    return res % p;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> T;
    while(T--)
    {
        cin >> a >> b;
        for(int i = 1; i <= mod; i++)
            sum[i] = (sum[i-1] + qmi(i, b, mod)) % mod;
        cout << (a/mod * sum[mod] + sum[a%mod]) % mod << endl;
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/zxytxdy/p/12098753.html