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+bcn and a+b+cx.

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.

给定两个整数 nx ,求 ab+ac+bcna+b+cx个正整数的三联数( a,b,c )的个数。

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

Input

The first line contains a single integer t (1t104)  — the number of test cases.

Each test case contains two integers n and x (1n,x106).

It is guaranteed that the sum of n over all test cases does not exceed 106 and that the sum of x over all test cases does not exceed 106.

输入

第一行包含一个整数 t ( 1t104 ) - 测试用例数。

每个测试用例包含两个整数 nx1n,x106 )。( 1n,x106 ).

保证所有测试用例的 n 之和不超过 106 ,所有测试用例的 x 之和不超过 106

Output

Output a single integer — the number of triplets (a,b,c) of positive integers such that ab+ac+bcn and a+b+cx.

输出

输出一个整数 - ab+ac+bcna+b+cx 的正整数三元组( 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+bcna+b+cx
给出所有的 a,b,c 的数量

题解

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

cnaba+b

cxab

两者取最小值即可

代码

#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   Jiejiejiang  阅读(34)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】

导航

统计信息

点击右上角即可分享
微信分享提示