题目来源
题目来自于AcWing平台:https://www.acwing.com/problem/content/4971/
 以blog的形式记录程序设计算法学习的过程,仅做学习记录之用。
题目描述

输入输出格式与数据范围

样例
第一组:
2 5
 
16
 
第二组:
12 7
 
11943936
 
思路
思路一部分参考自闫总的视频讲解,一部分参考自题解:https://www.acwing.com/solution/content/191150/。
 这道题是标准的模板题。求 
     
      
       
       
         1 
        
       
      
        1 
       
      
    1~ 
     
      
       
       
         N 
        
       
      
        N 
       
      
    N中与 
     
      
       
       
         N 
        
       
      
        N 
       
      
    N互质的数的个数称作欧拉函数,记为 
     
      
       
       
         ϕ 
        
       
         ( 
        
       
         N 
        
       
         ) 
        
       
      
        \phi(N) 
       
      
    ϕ(N)。在算数基本定理中,可以将 
     
      
       
       
         N 
        
       
      
        N 
       
      
    N拆解为: 
     
      
       
       
         N 
        
       
         = 
        
        
        
          p 
         
        
          1 
         
         
         
           a 
          
         
           1 
          
         
        
        
        
          p 
         
        
          2 
         
         
         
           a 
          
         
           2 
          
         
        
       
         ⋅ 
        
       
         ⋅ 
        
       
         ⋅ 
        
        
        
          p 
         
        
          m 
         
         
         
           a 
          
         
           m 
          
         
        
       
      
        N=p_1^{a_1}p_2^{a_2}\cdot\cdot\cdot{p_m^{a_m}} 
       
      
    N=p1a1p2a2⋅⋅⋅pmam,则:
  
     
      
       
       
         ϕ 
        
       
         ( 
        
       
         N 
        
       
         ) 
        
       
         = 
        
       
         N 
        
       
         × 
        
        
         
          
          
            p 
           
          
            1 
           
          
         
           − 
          
         
           1 
          
         
         
         
           p 
          
         
           1 
          
         
        
       
         × 
        
        
         
          
          
            p 
           
          
            2 
           
          
         
           − 
          
         
           1 
          
         
         
         
           p 
          
         
           2 
          
         
        
       
         . 
        
       
         . 
        
       
         . 
        
       
         × 
        
        
         
          
          
            p 
           
          
            m 
           
          
         
           − 
          
         
           1 
          
         
         
         
           p 
          
         
           m 
          
         
        
       
      
        \phi(N)=N\times{{p_1-1}\over{p_1}}\times{{p_2-1}\over{p_2}}...\times{{p_m-1}\over{p_m}} 
       
      
    ϕ(N)=N×p1p1−1×p2p2−1...×pmpm−1。
可以发现,欧拉函数与质因数有关,而 a b a^b ab的质因数与 a a a完全相同,因此只需要求 a a a的质因数即可。
因此,套用求互质数个数的模板,求出 a a a的互质数的个数即可,再使用快速幂对这一结果乘上 a b − 1 a^{b-1} ab−1,即可得到最终的答案。在进程乘法运算的过程中,不要忘记求模。
Code
代码同样参考自闫总的代码:https://www.acwing.com/activity/content/code/content/7589016/。
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std;
typedef long long LL;
const int MOD = 998244353;
LL qmi(LL a, LL b){ // 快速幂算法的模板
    LL res = 1;
    while(b){
        if(b & 1)   res = res * a % MOD;
        a = a * a % MOD;
        b >>= 1;
    }
    return res;
}
int main()
{
    LL a, b;
    cin >> a >> b;
    if(a == 1){
        cout << 0 << endl;
        return 0;
    }
    // 求质因数的个数的模板
    LL res = a, x = a;
    for(int i = 2; i * i <= x; i ++ ){
        if(x % i == 0){
            while(x % i == 0)   x /= i;
            res = res / i * (i - 1);
        }
    }
    if(x > 1)   res = res / x * (x - 1);
    cout << res * qmi(a, b - 1) % MOD << endl;
    return 0;
}
                


















