[COCI2020-2021#4] Vepar 题解

思路

由于输入比较大,直接乘肯定会炸掉,所以我们把每个数拆分成质因数相乘的形式,然后指数相加减,但是还是会炸

有一个定理:\(n!\) 中质因子\(p\) 有:
\(⌊\frac{n}{p}⌋+⌊\frac{n}{p ^ 2}⌋+⌊\frac{n}{p ^ 3}⌋⋅⋅⋅⋅⋅⋅\) 个(简要证明: \(p\) 的倍数, \(p\) 平方的倍数……)

Code:

// 写了普通拆分依然TLE :( 所以只能动用数学的力量了
#include <bits/stdc++.h>
using namespace std;
namespace IO
{
    inline long long read()
    {
        long long res = 0;
        bool f = 0;
        char ch = getchar();
        while (ch < '0' || ch > '9')
            f |= (ch == '-'), ch = getchar();
        while (ch >= '0' && ch <= '9')
            res = (res << 3) + (res << 1) + ch - '0', ch = getchar();
        return f ? -res : res;
    }
}
using IO::read;
const int MAXN = 1e7 + 5;
bitset<MAXN> not_prime;
vector<int> prime;
int low[MAXN];
int val[MAXN];
void init()
{
    for (int i = 2; i < MAXN; ++i)
    {
        if (!not_prime[i])
            prime.push_back(i), low[i] = i;
        for (auto pri : prime)
        {
            if (pri * i >= MAXN)
                break;
            not_prime[i * pri] = 1;
            low[i * pri] = pri;
            if (i % pri == 0)
                break;
        }
    }
}
int num[MAXN];
int T, n, a, b, c, d;
int part(int num, int pri)
{
    int cnt = 0;
    while (num)
    {
        cnt += num / pri; // 能被pri次幂整除的个数
        num /= pri;
    }
    return cnt;
}
void solve()
{
    a = read(), b = read(), c = read(), d = read();
    memset(val, 0, sizeof(val));
    // int maxx = max({a, b, c, d}); 其实跟取b同效
    for (auto pri : prime)
    {
        if (pri > b)
            break;
        int a_jian_b = part(b, pri) - part(a - 1, pri);
        int c_jian_d = part(d, pri) - part(c - 1, pri);
        if (a_jian_b > c_jian_d)
            return cout << "NE\n", void();
    }
    return cout << "DA\n", void();
}
int main()
{
    init();
    T = read();
    while (T--)
        solve();
    return 0;
}
posted @ 2025-10-15 22:27  孤独的Bochi  阅读(13)  评论(0)    收藏  举报