[枚举]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 << '\n';
			
        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 << '\n';
    }
    
    return 0;
}

 

posted @ 2019-05-14 11:42  张浦  阅读(179)  评论(0编辑  收藏  举报