3332. 无限剑制的终焉

L1ngYu

题意:求 $\sum_{i = 1} ^ n 10 ^ {i - 1} \bmod p$
做法: 不难得出 $\operatorname{ans} = \frac{10 ^ n - 1}{9} \bmod p$
用快速幂求即可。注意爆long long

#include <iostream>
#define int long long
using namespace std;
inline int ksc(int a, int b, const int &p) { return (a * b - (int)((long double)(a) / p * b) * p + p) % p; }
inline int ksm(int a, int b, const int &p)
{
    int c = 1;
    for (a %= p;b;a = ksc(a, a, p), b >>= 1) if (b & 1) c = ksc(c, a, p);
    return c;
}
void solve()
{
    int n, p; cin >> n >> p;
    cout << (ksm(10, n, 9 * p) - 1 + 9 * p) / 9 % p << '\n';
}
signed main()
{
    cin.tie(0)->sync_with_stdio(0);
    int _;for (cin >> _; _--; ) solve();
    return 0;
}
你当前正在回复 博客/题目
存在问题!