其他分享
首页 > 其他分享> > Master of Phi(数论,dfs)

Master of Phi(数论,dfs)

作者:互联网

题目描述

You are given an integer n. Please output the answer of 在这里插入图片描述modulo 998244353. n is represented in the form of factorization.

φ(n) is Euler’s totient function, and it is defi ned more formally as the number of integers k in the interval 1≤k≤n for which the greatest common divisor gcd(n,k) is equal to 1.
For example, the totatives of n = 9 are the six numbers 1, 2, 4, 5, 7 and 8. They are all co-prime to 9, but the other three numbers in this interval, 3, 6, and 9 are not, because gcd(9,3) = gcd(9,6) = 3 and gcd(9,9) = 9. Therefore, φ(9) = 6. As another example, φ(1) = 1 since for n = 1 the only integer in the interval from 1 to n is 1 itself, and gcd(1,1) = 1.
And there are several formulas for computing φ(n), for example, Euler’s product formula states like:
在这里插入图片描述
where the product is all the distinct prime numbers (p in the formula) dividing n.

输入

The fi rst line contains an integer T (1≤T≤20) representing the number of test cases.
For each test case, the fi rst line contains an integer m￿(1≤m≤20) is the number of prime factors.
The following m lines each contains two integers pi and qi (2≤pi≤108 , 1≤qi≤108 ) describing that n contains the factor piqi , in other words,在这里插入图片描述 . It is guaranteed that all pi are prime numbers and diff erent from each other.

输出

For each test case, print the the answer modulo 998244353 in one line.

样例输入
2
2
2 1
3 1
2
2 2
3 2

样例输出
15
168

提示

For first test case, n = 21*31= 6, and the answer is (φ(1)*n/1+φ(2)*n/2+φ(3)*n/3+φ(6)*n/6) mod 998244353 = (6 + 3 + 4 + 2) mod 998244353 = 15.

思路
根据题目可知,其乘积可化为在这里插入图片描述,因此可以通过公式,用搜索找到各个状态的解,答案就是各个解的和

代码实现

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
 
using namespace std;
typedef long long ll;
const int N=25;
const ll mod=998244353;
int T,m;
int p[N],q[N],inv[N];
ll ans,x;
 
ll quickpow(ll a,ll b)
{
    ll sum=1;
    while(b)
    {
        if(b&1) sum=sum*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return sum;
}
void dfs(ll pos,ll n)
{
    if(pos==m)
    {
        ans=(ans+n)%mod;
        return ;
    }
    dfs(pos+1,n);
    dfs(pos+1,n*(p[pos]-1)%mod*q[pos]%mod*inv[pos]%mod);
}
 
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        ans=0;
        x=1;
        scanf("%d",&m);
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&p[i],&q[i]);
            inv[i]=quickpow(p[i],mod-2);
            x=x*quickpow(p[i],q[i])%mod;
        }
        dfs(0,x);
        printf("%lld\n",ans);
    }
    return 0;
}

标签:Phi,gcd,int,ll,pos,dfs,Master,998244353,mod
来源: https://blog.csdn.net/weixin_43935894/article/details/89047898