codeforces 1872D

题目链接

题目大意

给定三个正整数n,x,y,定义一个合法的含有n个元素的排列仅由1~n之间的数字组成且且每个数字有且只使用一次,顺序不要求,问元素下标为x的倍数的和,减去为y的倍数的和,的最大值

ACcode

#include

using namespace std;

using ll = long long;

void solve()
{
    ll n, x, y;cin >> n >> x >> y;
    ll a = n / x;
    ll b = n / y;
    ll c = n / (lcm(x, y));
    a -= c;
    b -= c;
    ll na = (2 * n - a + 1) * a / 2;
    ll nb = (1 + b) * b / 2;
    cout << na - nb << '\n';
}

int main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int t;cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

你可能感兴趣的:(算法,c++,数据结构)