jzoj 6272. 2019.8.4【NOIP提高组A】整除 (division)

Description

详见OJ

Solution

先想到了50分暴力,又想到了对于每个质数分开求答案,最后相乘,结果打挂。
正解根据中国剩余定理,可以对于每个质数分开求。
\((x^m-x)\)%\(n=0\)
由于\(n\)有多个不同的质数组成,所以我们可以用中国剩余定理来分解成\(c\)个方程。
然后得到\(x^m-x≡0(mod~p)\)\(c\)个方程)
我们对于每个都用50分暴力来求,最后答案乘起来即可。
时间\(O(T*c*t*logm)\),80分。
由于有个\(log\),我们考虑优化。
根据积性筛(欧拉筛),我们可以接近\(O(n)\)求出\(1\)~\(n\)\(m\)次方,在%\(n\)的意义下。
质数直接用\(ksm\)求出\(x^m\),而合数则可以用其质数\(y\)的值和\(x/y\)的值相乘求出。
这样消去\(log\),可以AC了。

Code

#include <cstdio>
#include <cstring>
#define N 51
#define ll long long
#define mo 998244353
#define mem(x, a) memset(x, a, sizeof x)
#define fo(x, a, b) for (int x = a; x <= b; x++)
#define fd(x, a, b) for (int x = a; x >= b; x--)
using namespace std;
int id, T, c, m, n, ans, s1;
int kz[10010], phi[N], pri[10010];

inline int read()
{
    int x = 0; char c = getchar();
    while (c < '0' || c > '9') c = getchar();
    while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
    return x;
}

int ksm(int x, int y)
{
    int s = 1;
    while (y)
    {
        if (y & 1) s = s * x % n;
        x = x * x % n; y >>= 1;
    }
    return s;
}

int main()
{
    freopen("division.in", "r", stdin);
    freopen("division.out", "w", stdout);
    id = read();
    T = read();
    while (T--)
    {
        c = read(), m = read();
        fo(i, 1, c) phi[i] = read();
        ans = 1;
        fo(i, 1, c)
        {
            s1 = 1; n = phi[i];
            fo(ii, 2, n) kz[ii] = 0;
            pri[0] = 0;
            fo(ii, 2, n)
            {
                if (! kz[ii]) kz[ii] = ksm(ii, m), pri[++pri[0]] = ii;
                for (int j = 1; pri[j] * ii <= n; j++)
                {
                    kz[pri[j] * ii] = kz[pri[j]] * kz[ii] % n;
                    if (ii % pri[j] == 0) break;
                }
                if (kz[ii] == ii % n) s1++;
            }
            ans = (ll)ans * s1 % mo;
        }
        printf("%d\n", ans);
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/jz929/p/11299531.html
今日推荐