跳到正文

个人记录

10.24模拟赛

YYX_2013约 3 分钟
原文
文章信息与版本

文章信息

发布
2026年10月24日
收录
2026年10月28日
更新
2026年11月16日
篇幅
约 3 分钟

版本

  1. v1当前

分数

估分:100+25+25+0=150
得分:75+25+25+0=125
原因:T1常数过大导致TLE

T1

思路:

考虑筛出所有质数,依次判断质因子即可
由于只枚举到 107\sqrt{10^7},所以只枚举 10410^4 个质数即可

但是,若 x,yx,y 本身为质数,则不会被枚举到
所以当枚举结束后且 x≠yx\ne y 时,ans←ans⋅x⋅yans\leftarrow ans\cdot x\cdot y

代码:

C++
#include<bits/stdc++.h>
using namespace std;

int t, x, y, cnt, prime[700000];
bool no_prime[10000100];
long long ans;

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    for(int i = 2; i <= 10000000; i++){
        if(no_prime[i]){
            continue;
        }
        prime[++cnt] = i;
        for(int j = 2; j <= 10000000 / i; j++){
            no_prime[i * j] = true;
        }
    }
    cin >> t;
    while(t--){
        ans = 1;
        cin >> x >> y;
        for(int i = 1; i <= cnt && prime[i] * prime[i] <= max(x, y); i++){
            if(x % prime[i] == 0 && y % prime[i] == 0){
                while(x % prime[i] == 0){
                    x /= prime[i];
                }
                while(y % prime[i] == 0){
                    y /= prime[i];
                }
            }
            if(x % prime[i] == 0){
                ans *= prime[i];
                while(x % prime[i] == 0){
                    x /= prime[i];
                }
            }
            else if(y % prime[i] == 0){
                ans *= prime[i];
                while(y % prime[i] == 0){
                    y /= prime[i];
                }
            }
            if(x == 1 && y == 1){
                break;
            }
        }
        if(x != y){
        	ans *= x;
        	ans *= y;
		}
        cout << ans << '\n';
    }
    return 0;
}
原文