Codeforces Round #589 (Div. 2)

A - Distinct Digits

水题。

B - Filling the Grid

好像也是水题。

C - Primes and Multiplication

题意:给一个数字x,一个数字n,求x的每种质因数在[1,n]中贡献的积。

题解:分解质因数x,然后用快速幂乘起来。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

const int mod = 1e9 + 7;

ll qpow(ll x, ll n) {
    ll res = 1;
    while(n) {
        if(n & 1)
            res = res * x % mod;
        x = x * x % mod;
        n >>= 1;
    }
    return res;
}

int fac[55][2];

void test_case() {
    int x;
    ll n;
    scanf("%d%lld", &x, &n);
    int ftop = 0;
    for(int i = 2; i * i <= x; ++i) {
        if(x % i == 0) {
            fac[++ftop][0] = i;
            fac[ftop][1] = 0;
            while(x % i == 0) {
                x /= i;
                ++fac[ftop][1];
            }
        }
    }
    if(x != 1) {
        fac[++ftop][0] = x;
        fac[ftop][1] = 1;
    }
    ll ans = 1;
    for(int i = 1; i <= ftop; ++i) {
        ll p = fac[i][0], pk = fac[i][0];
        while(pk <= n) {
            ans = ans * qpow(p, n / pk) % mod;
            if(n / pk >= p)
                pk *= p;
            else
                break;
        }
    }
    printf("%lld
", ans);
}

int main() {
#ifdef KisekiPurin
    freopen("KisekiPurin.in", "r", stdin);
#endif // KisekiPurin
    int t = 1;
    //scanf("%d", &t);
    for(int ti = 1; ti <= t; ++ti) {
        //printf("Case #%d: ", ti);
        test_case();
    }
}
原文地址:https://www.cnblogs.com/KisekiPurin2019/p/11891591.html