View Modular Operation from the Perspective of Unique Decomposition Theorem
- Problem background
Given two numbers p and q, if p% q == 0, find the maximum value of x that satisfies the condition (p%x==0 && x% q !=0 ).
- First look at a set of examples
6 % 4 != 0
6 % 2 == 0
6 The prime factor of decomposition is: 2 1 ×3 1
4 Decomposition prime factors are: 2 2
2 Decomposition prime factors are: 2 1
According to the power of each term of the prime factorization formulas of 6, 4, and 2 combined with the modular operation, have you found that the reason for 6% 4! = 0 is 2 1 of 6
2 2 that is less than 4 , and the result of the modulo operation is 0 because the prime factor term of 2 is 2 1 <= 2 1
- get conclusion
If two numbers a and b are modulo 0, then each term in the prime factorization formula of a is completely contained or completely contained in the prime factorization formula of b (where the inclusion refers to each of a The power of the prime term is greater than or equal to the power of each prime term of b)
- Solve the problem
Since p% q == 0 &&p>q, each prime factor term of p completely includes each prime factor term of q. If you want to find a satisfying condition of x, you can make x the initial value of p, and then let some A prime factor term that is less than q, so x %p !=0, and since each prime factor term of p completely contains x, so p% x == 0
Title from: CodeForces 1445C
撸Code:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
using namespace std;
int main() {
int tt;
scanf("%d",&tt);
while(tt--){
long long p,q;
scanf("%lld%lld",&p,&q);
long long Q=q;
long long ans=0;
for(long long i=2;i*i<=q;i++)//将 q 分解质因子 为 i
if(q%i==0){
while(q%i==0) q/=i;
long long ss=p;
while(ss%Q==0) ss/=i;//若 p%q==0 说明 p 的质因子 i 的幂次大于等于 q 的质因子 i 的幂次
ans=max(ans,ss);
}
if(q>1){
long long ss=p;
while(ss%Q==0) ss/=q;
ans=max(ans,ss);
}
printf("%lld\n",ans);
}
return 0;
}