• [枚举]LCM


    题意:给a b 求 a 范围内最大的 x * y / gcd(x, y) = b;

     解题思路:一开始想的是枚举lcm的倍数到a * a, 然后在倍数里枚举因子判断是否合法

    直接枚举因子就可以了, 因子范围小 只用n方 枚举lcm的每个倍数都是n方

    以后做题尽量枚举小范围易得的部分


    代码:

    /*
        Zeolim - An AC a day keeps the bug away
    */
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);     cout.tie(0);
        //freopen("D://test.in", "r", stdin);
        //freopen("D://test.out", "w", stdout);
        
        ll n;
    
        cin >> n;
    
        while(n--)
        {
            ll a, b;
            cin >> a >> b;
            std::vector<ll> inv;
            for(ll i = 1; i * i <= b; ++i)  //筛b因子
                if(b % i == 0) inv.push_back(i), inv.push_back(b / i);
    
            sort(inv.begin(), inv.end());
            inv.erase(upper_bound(inv.begin(), inv.end(), a), inv.end()); //筛合法因子
    
    //		for(auto x : inv)
    //			cout << x << '
    ';
    			
            ll ans = -1, rx;
            for(int i = 0; i < inv.size(); ++i) //暴力枚举
            {
                for(int j = i; j < inv.size(); ++j)
                {
                    rx = inv[i] * inv[j];
                    if(rx > ans && rx / __gcd(inv[i], inv[j]) == b)  //判断合法
                        ans = rx;
                }
            }
            cout << ans << '
    ';
        }
        
        return 0;
    }
  • 相关阅读:
    Nginx URL后面不加斜杠301重定向
    Jenkins 配置 Node.js 项目
    在 Linux 上搭建IntelliJ IDEA license server服务器
    Vue 使用细节收集
    sinopia 搭建记录
    cli 开发记录
    Cgroup(一)简介
    Kubernetes (一)POD驱逐
    RabbitMQ(五)镜像队列
    RabbitMQ(四)队列结构
  • 原文地址:https://www.cnblogs.com/zeolim/p/12270363.html
Copyright © 2020-2023  润新知