codeforces C. Division 数学

http://codeforces.com/contest/1445/problem/C

 1 #include<cstdio>
 2 #define rep(i,a,n) for(register ll i = a;i <= n;++i)
 3 #define per(i,n,a) for(register ll i = n;i >= a;--i)
 4 using namespace std;
 5 typedef long long ll;
 6 
 7 const ll Maxn = 100011;
 8 
 9 ll read(){
10     ll a = 0;char c = getchar(),l = c;
11     while(c < '0'||c > '9')l = c,c = getchar();
12     while('0' <= c&&c <= '9')a = a*10+c-'0',c = getchar();
13     if(l == '-')return -a;return a;
14 }
15 
16 ll max(ll a,ll b){if(a > b)return a;return b;}
17 
18 ll prime[Maxn],v[Maxn],b[Maxn];
19 ll cntp;
20 
21 void euler(){
22     cntp = 0;
23     rep(i,2,100010){
24         if(!v[i])v[i] = i,prime[++cntp] = i;
25         rep(j,1,cntp){
26             if(prime[j] > v[i]||prime[j]*i > 100010)break;
27             v[prime[j]*i] = prime[j];
28         }
29     }
30     
31 }
32 
33 int main(){
34     euler();
35     ll t = read();
36     while(t--){
37         ll x = read(),y = read(),ans = 1,z = y,cnt = 0;
38         rep(i,1,cntp)if(z%prime[i] == 0){
39             b[++cnt] = prime[i];//printf("working %d:\n",b[cnt]);
40             while(z%prime[i] == 0)z /= prime[i];
41         }
42         if(z > 1)b[++cnt] = z;
43         rep(i,1,cnt){
44             ll cntx = 0,cnty = 0,xx = x,yy = y;
45             while(yy % b[i] == 0)yy /= b[i],cnty++;
46             while(xx % b[i] == 0)xx /= b[i],cntx++;
47             if(cntx < cnty){ans = x;break;}
48             else{
49                 rep(j,1,cnty-1)xx *= b[i];
50 //                printf("---- %%%d %d\n",b[i],xx);
51                 ans = max(ans,xx);
52             }
53         }
54         if(ans == 1){
55             while(x % y == 0)x /= y;
56             ans = x;
57         }
58         printf("%lld\n",ans);
59     }
60 return 0;
61 }

 

posted @ 2020-11-02 21:13  TIH_HIT  阅读(143)  评论(0编辑  收藏  举报