前置知识
欧拉函数
在数论中,对正整数n,欧拉函数φ(n)是小于或等于n的正整数中与n互质的数的数目。
参考链接:https://blog.csdn.net/Qiuker_jl/article/details/109587507
快速幂
参考链接:https://blog.csdn.net/m0_52072919/article/details/116400820
题目描述
给定 a, b,求 1 ≤ x < ab 中有多少个 x 与 ab 互质。由于答案可能很大,你只需要输出答案对 998244353 取模的结果。
输入格式
输入一行包含两个整数分别表示 a, b,用一个空格分隔。
代码
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.*;
public class E{
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StreamTokenizer st = new StreamTokenizer(br);
static int nextInt() throws Exception{st.nextToken();return (int) st.nval;}
static long nextLong() throws Exception {st.nextToken();return (long) st.nval;}
static PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
static int mod = 998244353;
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
long a = scanner.nextLong();
long b = scanner.nextLong();
long res = qsm(a,b-1);
long x = a;
//欧拉函数
for(long i=2;i<=Math.sqrt(a);i++) {
if(a%i==0) {
x = x/i*(i-1);
while(a%i==0)
a/=i;
}
}
x = x/a*(a-1);
System.out.println(res*x%mod);
}
//快速幂
public static long qsm(long a,long b) {
long res = 1;
while(b!=0) {
if(b%2==1) {
res = res*a%mod;
}
a = a*a%mod;
b = b/2;
}
return res;
}
}
此文章用于个人学习