zoukankan      html  css  js  c++  java
  • Codeforces 1445C. Division(分解质因数)

    Codeforces 1445C. Division

    题目大意

    • 给出 p , q p,q p,q,求最大的 x x x使得 x x x能被 p p p整除但 q q q不能被 x x x整除。
    • p ≤ 1 0 18 pleq 10^{18} p1018 2 ≤ q ≤ 1 0 9 2leq qleq 10^9 2q109

    题解

    • 可以先令 x = p x=p x=p,若不满足第二个条件则不断把 x x x改小,
    • 显然为了满足 x x x仍旧是 p p p的约数,每次要让 x x x除以某个数,
    • 为了让第二个条件成立,需要 q q q分解质因数后某一项 c k c^k ck x x x中只有 c k ′ ( k ′ < k ) c^{k'}(k'<k) ck(k<k)
    • 而且为了 x x x尽可能大,所以只需要某一个质因数满足即可,
    • 那么枚举 q q q的每个质因数,把 x x x一直除以它直到条件成立,在所有这样操作后的 x x x中取最大值。

    代码

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    #define ll long long
    int main() {
    	int tn;
    	scanf("%d", &tn);
    	while(tn--) {
    		ll p, q, i;
    		scanf("%lld%lld", &p, &q);
    		ll Q = q;
    		ll t = 0;
    		for(i = 2; i * i <= q; i++) if(q % i == 0) {
    			while(q % i == 0) q /= i;
    			ll ss = p, l = 1;
    			while(ss % Q == 0 && ss % i == 0) ss /= i, l *= i;
    			t = max(t, ss);
    		}
    		if(q > 1) {
    			ll ss = p, l = 1;
    			while(ss % Q == 0 && ss % q == 0) ss /= q, l *= q;
    			t = max(t, ss);
    		}
    		printf("%lld
    ", t);
    	}
    	return 0;
    }
    
    
    哈哈哈哈哈哈哈哈哈哈
  • 相关阅读:
    Beginning Auto Layout Tutorial in iOS 7: Part 2
    Beginning Auto Layout Tutorial in iOS 7: Part 1
    Autolayout 03
    Autolayout 02
    Autolayout 01
    Start Developing iOS Apps Today
    Spring 7大功能模块的作用
    struts2入门
    myeclipse导入工程 Some projects cannot be imported because they already exist in the workspace
    第十一章 Servlet MVC模式
  • 原文地址:https://www.cnblogs.com/LZA119/p/14279516.html
Copyright © 2011-2022 走看看