jiejiejiang2004

题解:Codeforces Round 962 (Div. 3) D

D. Fun

time limit per test: 2 seconds

memory limit per test: 256 megabytes

input: standard input

output: standard output

Counting is Fun!


— satyam343

Given two integers \(n\) and \(x\), find the number of triplets (\(a,b,c\)) of positive integers such that \(ab + ac + bc \le n\) and \(a + b + c \le x\).

Note that order matters (e.g. (\(1, 1, 2\)) and (\(1, 2, 1\)) are treated as different) and \(a\), \(b\), \(c\) must be strictly greater than \(0\).

给定两个整数 \(n\)\(x\) ,求 \(ab + ac + bc \le n\)\(a + b + c \le x\)个正整数的三联数( \(a,b,c\) )的个数。

注意顺序问题(例如 ( \(1, 1, 2\) ) 和 ( \(1, 2, 1\) ) 被视为不同), \(a\)\(b\)\(c\) 必须严格大于 \(0\)

Input

The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\))  — the number of test cases.

Each test case contains two integers \(n\) and \(x\) (\(1 \leq n,x \leq 10^6\)).

It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^6\) and that the sum of \(x\) over all test cases does not exceed \(10^6\).

输入

第一行包含一个整数 \(t\) ( \(1 \leq t \leq 10^4\) ) - 测试用例数。

每个测试用例包含两个整数 \(n\)\(x\)\(1 \leq n,x \leq 10^6\) )。( \(1 \leq n,x \leq 10^6\) ).

保证所有测试用例的 \(n\) 之和不超过 \(10^6\) ,所有测试用例的 \(x\) 之和不超过 \(10^6\)

Output

Output a single integer — the number of triplets (\(a,b,c\)) of positive integers such that \(ab + ac + bc \le n\) and \(a + b + c \le x\).

输出

输出一个整数 - \(ab + ac + bc \le n\)\(a + b + c \le x\) 的正整数三元组( \(a,b,c\) )的个数。

Example

Input

4
7 4
10 5
7 1000
900000 400000

Output

4
10
7
1768016938

Note

In the first test case, the triplets are (\(1, 1, 1\)), (\(1, 1, 2\)), (\(1, 2, 1\)), and (\(2, 1, 1\)).

In the second test case, the triplets are (\(1, 1, 1\)), (\(1, 1, 2\)), (\(1, 1, 3\)), (\(1, 2, 1\)), (\(1, 2, 2\)), (\(1, 3, 1\)), (\(2, 1, 1\)), (\(2, 1, 2\)), (\(2, 2, 1\)), and (\(3, 1, 1\)).

在第一个测试用例中,三元组是 ( \(1, 1, 1\) )、( \(1, 1, 2\) )、( \(1, 2, 1\) ) 和 ( \(2, 1, 1\) )。

在第二个测试用例中,三元组是 ( \(1, 1, 1\) )、( \(1, 1, 2\) )、( \(1, 1, 3\) )、( \(1, 2, 1\) )、( \(1, 2, 2\) )、( \(1, 3, 1\) )、( \(2, 1, 1\) )、( \(2, 1, 2\) )、( \(2, 2, 1\) ) 和 ( \(3, 1, 1\) )。

题意

题目说得非常直白
给你两个数 \(n,x\) ,让你给出
使 \(a,b,c\) 满足 \(ab + ac + bc \le n\)\(a + b + c \le x\)
给出所有的 \(a,b,c\) 的数量

题解

本题的时间复杂度看似是 \(O(n^3)\) ,实际上是 \(O(n^2)\)
因为你只要确定了 \(a\)\(b\)\(c\) 的范围也就确定了
那我们只要根据式子,已知 \(n,x,a,b\) ,倒推 \(c\) 的表达式
然后对遍历 \(a,b\) 就可以了
\(c\) 的表达式

\[c \le \frac{n-ab}{a+b} \]

\[c \le x - a- b \]

两者取最小值即可

代码

#include <bits/stdc++.h>

#define int long long
#define INF 0x3f3f3f3f
#define all(x) x.begin(),x.end()

using i64 = long long;

int t = 1;

void solve(){
    i64 ans = 0;
    int n,m;
    std::cin >> n >> m;
    for(int i = 1 ; i <= n && i <= m ; i ++) {
        for(int j = 1 ; i+j <= m && i*j <= n ; j ++) {
            ans += std::min(m-i-j,(n-i*j)/(i+j));
        }
    }
    std::cout << ans << "\n";
}

signed main(){
    std::cin >> t;
    while(t--){
        solve();
    }
    return 0;
}

posted on 2024-07-30 10:42  Jiejiejiang  阅读(15)  评论(0编辑  收藏  举报

导航