题目链接:Pair of Toys
题意
有 n n 个玩具,第 个玩具的价格为 i i ,问有多少对玩具 ,它们的价值和等于 k k ,其中 和 (b,a) ( b , a ) 被认为是相同的玩具对。
输入
输入只包含两个整数 n,k (1≤n,k≤1014) n , k ( 1 ≤ n , k ≤ 10 14 ) 。
输出
输出满足条件的玩具对数。
样例
输入 |
---|
8 5 |
输出 |
2 |
提示 |
(1,4) ( 1 , 4 ) 和 (2,3) ( 2 , 3 ) 都是合法的选择。 |
输入 |
---|
8 15 |
输出 |
1 |
提示 |
只有 (7,8) ( 7 , 8 ) 是合法的选择。 |
输入 |
---|
7 20 |
输出 |
0 |
提示 |
1 1 到 中所有数字相加都不能达到 20 20 ,因此答案为 0 0 。 |
输入 |
---|
1000000000000 1000000000001 |
输出 |
500000000000 |
提示 |
我们可以选择 ,总共有 500000000000 500000000000 对整数。 |
题解
先确定所有可选数字的右边界 R=min(k−1,n) R = min ( k − 1 , n ) ,再确定其左边界 L=max(1,k−R) L = max ( 1 , k − R ) ,其中满足条件的整数对数为 ⌊R−L+12⌋ ⌊ R − L + 1 2 ⌋
过题代码
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <algorithm>
using namespace std;
#define LL long long
LL n, k;
int main() {
#ifdef Dmaxiya
freopen("test.txt", "r", stdin);
// freopen("10.out", "w", stdout);
#endif // Dmaxiya
while(scanf("%I64d%I64d", &n, &k) != EOF) {
LL R = min(k - 1, n);
LL L = max(1LL, k - R);
if(L >= R) {
printf("0\n");
continue;
}
printf("%I64d\n", (R - L + 1) / 2);
}
return 0;
}