首页 > 其他分享 >题解:Codeforces Round 962 (Div. 3) D

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

时间:2024-07-30 10:39:17浏览次数:10  
标签:10 le 题解 Codeforces leq 测试用例 test Div ab

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;
}

标签:10,le,题解,Codeforces,leq,测试用例,test,Div,ab
From: https://www.cnblogs.com/jiejiejiang2004/p/18331811

相关文章

  • [USACO1.5] 八皇后 Checker Challenge 题解
    [USACO1.5]八皇后CheckerChallenge题目描述一个如下的\(6\times6\)的跳棋棋盘,有六个棋子被放置在棋盘上,使得每行、每列有且只有一个,每条对角线(包括两条主对角线的所有平行线)上至多有一个棋子。上面的布局可以用序列\(2\4\6\1\3\5\)来描述,第\(i\)个数字表示在......
  • 题解:P10815 【模板】快速读入
    闲着没事儿水篇tj题目大意题目大意极其粗暴,记得\(10^8\times10^8=10^{16}>2^{31}-1\)会爆int,开longlong就好。于是这个题就变成了一个读入输出优化模板题。这不又回去了。另外,输入输出常数优化也很常用,抢最优解和骗分时都可以用上。1cin/cout版本操作ios::......
  • 奇怪的汉诺塔 - 题解
    奇怪的汉诺塔时间限制:C/C++1000MS,其他语言2000MS内存限制:C/C++256MB,其他语言512MB描述汉诺塔问题,条件如下:这里有\(A\)、\(B\)、\(C\)和\(D\)四座塔。这里有\(n\)个圆盘,\(n\)的数量是恒定的。每个圆盘的尺寸都不相同。所有的圆盘在开始时都堆叠在塔\(A\)......
  • 昆虫繁殖 - 题解
    昆虫繁殖时间限制:C/C++1000MS,其他语言2000MS内存限制:C/C++256MB,其他语言512MB描述科学家在热带森林中发现了一种特殊的昆虫,这种昆虫的繁殖能力很强。每对成虫过\(x\)个月产\(y\)对卵,每对卵要过两个月长成成虫。假设每个成虫不死,第一个月只有一对成虫,且卵长成成虫后......
  • 【入门】汉诺塔的移动次数 - 题解
    【入门】汉诺塔的移动次数时间限制:C/C++1000MS,其他语言2000MS内存限制:C/C++128MB,其他语言256MB描述汉诺塔的问题大家都已经很熟悉了,有三个柱子,每个柱子上有一些大小不一的金片,要把金片从\(A\)柱移动到\(C\)柱,可以借助\(B\)柱,请问\(n\)个金片的情况下,需要最少移......
  • 【基础】递归问题—汉诺塔 - 题解
    【基础】递归问题—汉诺塔时间限制:C/C++1000MS,其他语言2000MS内存限制:C/C++127MB,其他语言254MB描述汉诺塔(又称河内塔)问题是印度的一个古老的传说。开天辟地的神勃拉玛在一个庙里留下了三根金刚石的棒,第一根上面套着\(64\)个圆的金片,最大的一个在底下,其余一个比一个小......
  • 放苹果 - 题解
    时间限制:C/C++1000MS,其他语言2000MS内存限制:C/C++256MB,其他语言512MB描述把\(M\)个同样的苹果放在\(N\)个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)\(5,1,1\)和\(1,5,1\)是同一种分法。输入描述第一行是测试数据的数目\(t\)(\(0≤t≤20\))。......
  • 【入门】统计每个月兔子的总数 - 题解
    【入门】统计每个月兔子的总数时间限制:C/C++1000MS,其他语言2000MS内存限制:C/C++16MB,其他语言32MB描述有一对兔子,从出生后第3个月起每个月都生一对兔子,一对小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问第\(n\)个月(\(n<=50\))的兔子总数为多少对?输入描述......
  • 排序 - 题解
    排序时间限制:C/C++1000MS,其他语言2000MS内存限制:C/C++64MB,其他语言128MB描述给定你一个长度为\(n\)的整数数列。请你使用任意排序对这个数列按照从小到大进行排序。并将排好序的数列按顺序输出。数据范围\(1≤n≤100000\)禁止使用sort函数输入描述输入共两......
  • 逆序对的数量 - 题解
    逆序对的数量时间限制:C/C++1000MS,其他语言2000MS内存限制:C/C++64MB,其他语言128MB描述给定一个长度为\(n\)的整数数列,请你计算数列中的逆序对的数量。逆序对的定义如下:对于数列的第\(i\)个和第\(j\)个元素,如果满足\(i<j\)且\(a[i]>a[j]\),则其为一个逆序对;否则......