Master of Phi HDU - 6265(数论)

Problem B. Master of Phi

You are given an integer n. Please output the answer of ∑ d|n φ(d) × n d modulo 998244353. n is represented in the form of factorization.

φ(n) is Euler’s totient function, and it is defined 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:

                      φ(n) = n ∏ p|n ( 1 − 1 p ) ,

where the product is all the distinct prime numbers (p in the formula) dividing n.

Input

The first line contains an integer T (1 ≤ T ≤ 20) representing the number of test cases.

For each test case, the first 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 pi qi , in other words, n = ∏m i=1 pi qi . It is guaranteed that all pi are prime numbers and different from each other.

Output

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

Example standard input           standard output

2                       15

2                       168

2 1

3 1

2

2 2

3 2

Explanation

For first test case, n = 21 ∗3 1 = 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.

 

题意

计算∑ d|n φ(d) × n/d


 

#include<bits/stdc++.h>
using namespace std;
const long long mod = 998244353;

long long poww(long long a,long long b)
{
    long long ans = 1;
    while(b)
    {
        if(b&1)
        {
            ans = ans*a%mod;
        }
        a = a*a%mod;
        b>>=1;

    }
    return ans;
}
long long mul(long long a, long long b)
{
    long long ans = 0;
    while(b)
    {
        if(b&1)
        {
            ans = ans+a%mod;
        }
        a = 2*a%mod;
        b>>=1;
    }
    return ans;
}
int main()
{
    long long i,j,m,n,t,p,q,ans;
    scanf("%lld",&t);
    while(t --)
    {
        scanf("%lld",&n);
        ans = 1;
        for(i = 0; i < n; i++)
        {
            scanf("%lld %lld",&p, &q);
            ans = ans*poww(p,q-1ll)%mod;
            ans = ((p+mul(p,q)-q)%mod)*ans%mod;
        }
        printf("%lld\n",ans);
    }
    return 0;
}

 

首先,我们需要将目标函数 f 进行整理,将表示为 w、t、A、phi1、phi2、mm、M、f、g、k、x0、beta、F0、rho、g 和 S 的函数。然后,使用 MATLAB 的优化工具箱中的函数 fmincon 来求解最大值问题。 下面是使用 MATLAB 的代码示例: ```matlab % 定义目标函数 f = @(x) -(-x(3)^2*x(1)*(x(7)-x(6))*cos(x(1)*x(2)+x(5))-x(4)*cos(x(1)*x(2))+x(7)*x(8)+x(10)*x(11)-2*x(3)*x(10)*sin(x(1)*x(2)+(x(4)+x(5))/2)*sin(x(5)-x(4))-x(12)*x(3)*x(1)*sin(x(1)*x(2)+x(5))-2*x(13)*x(1)*x(3)*cos(x(1)*x(2)+(x(4)+x(5))/2)*sin((x(5)-x(4))/2)-x(14)*x(7)*x(6)*x(15)+x(3)*x(14)*x(7)*x(3)*cos(x(1)*x(2)+x(5))); % 定义约束条件 nonlcon = @(x) deal([], [x(2)-100; x(13)-10000; x(13)]); % 定义初始点 x0 = [1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1; 1]; % 定义变量的上下界 lb = [-Inf; -Inf; -Inf; -Inf; -Inf; -Inf; -Inf; -Inf; -Inf; -Inf; -Inf; -Inf; -Inf; -Inf; -Inf]; ub = [Inf; Inf; Inf; Inf; Inf; Inf; Inf; Inf; Inf; Inf; Inf; Inf; Inf; Inf; Inf]; % 使用 fmincon 函数求解最大值问题 options = optimoptions('fmincon', 'Display', 'iter'); [x_opt, f_opt] = fmincon(f, x0, [], [], [], [], lb, ub, nonlcon, options); % 输出结果 fprintf('Optimal solution:\n'); fprintf('w = %.4f\n', x_opt(1)); fprintf('t = %.4f\n', x_opt(2)); fprintf('A = %.4f\n', x_opt(3)); fprintf('phi1 = %.4f\n', x_opt(4)); fprintf('phi2 = %.4f\n', x_opt(5)); fprintf('mm = %.4f\n', x_opt(6)); fprintf('M = %.4f\n', x_opt(7)); fprintf('f = %.4f\n', x_opt(8)); fprintf('g = %.4f\n', x_opt(9)); fprintf('k = %.4f\n', x_opt(10)); fprintf('x0 = %.4f\n', x_opt(11)); fprintf('beta = %.4f\n', x_opt(12)); fprintf('F0 = %.4f\n', x_opt(13)); fprintf('rho = %.4f\n', x_opt(14)); fprintf('S = %.4f\n', x_opt(15)); fprintf('Maximum value: %.4f\n', -f_opt); ``` 请注意,上述代码中的目标函数 f 和约束条件 nonlcon 需要根据具体的问题进行修改。初始点 x0、变量的上下界 lb 和 ub 也需要根据实际情况进行设置。 希望这可以帮助到你!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值