2020牛客暑期多校训练营(第九场) E.Groundhog Chasing Death(数论,质因数分解,费马小定理)

题目传送
借鉴大佬的博客

思路:
真的是搞人,一个bug能找俩小时
把题意转化求这个等式,其中aa表示的是x中数值为p的质因子的个数,bb表示的是y中数值为p的质因子个数
在这里插入图片描述

因为求他们的gcd就相当于求其质因数分解后的每个质因子的最小数目的乘积。然后再算每个质因子对答案的贡献。

详细看大佬的博客叭,太详细了 Orz

这里需要用到:
a ^ b % p == a ^ (b%(p-1)) % p

AC代码

#include <bits/stdc++.h>
inline long long read(){char c = getchar();long long x = 0,s = 1;
while(c < '0' || c > '9') {if(c == '-') s = -1;c = getchar();}
while(c >= '0' && c <= '9') {x = x*10 + c -'0';c = getchar();}
return x*s;}
using namespace std;
#define NewNode (TreeNode *)malloc(sizeof(TreeNode))
#define Mem(a,b) memset(a,b,sizeof(a))
#define lowbit(x) (x)&(-x)
const int N = 2e3 + 5;
const long long INFINF = 0x7f7f7f7f7f7f7f;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-7;
const int mod = 998244353;
const double II = acos(-1);
const double PP = (II*1.0)/(180.00);
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> piil;
ll a,b,c,d,x,y,sum = 1;
ll quick_pow(ll a,ll b,ll p)
{
    ll ans = 1;
    while(b)
    {
        if(b&1)
            ans = a*ans%p;
        b>>=1;
        a = a*a%p;
    }
    return ans;
}
void solve(ll num)
{
    ll numa = 0,numb = 0,ans = 0;
    while(x % num == 0) numa++,x/=num;
    while(y % num == 0) numb++,y/=num;
    if(numa == 0 || numb == 0) return ;
    for(ll i = a;i <= b;i++)
    {
        ll j = numa*i/numb;
        if(j < c) ans = ans + (d-c+1)*numa%(mod-1)*i%(mod-1);
        else if(j >= d) ans = ans + (c+d)*(d-c+1)/2%(mod-1)*numb%(mod-1);
        else ans = (ans + (d-j)*numa%(mod-1)*i%(mod-1) + (j+c)*(j-c+1)/2%(mod-1)*numb%(mod-1))%(mod-1);
        ans %= (mod-1);
    }
    sum = sum*quick_pow(num,ans,mod)%mod;
}
signed main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    //    freopen("input.txt","r",stdin);
    //    freopen("output.txt","w",stdout);
    cin >> a >> b >> c >> d >> x >> y;
    for(ll i = 2;i * i <= x;i++)
        if(x % i == 0)
            solve(i);
    if(x > 1) solve(x);
    cout << sum << endl;
}

©️2020 CSDN 皮肤主题: 黑客帝国 设计师:上身试试 返回首页