Codeforces Round #504 B. Pair of Toys(思维)

题目链接:http://codeforces.com/contest/1023/problem/B

       题意是输入n和m,问从1-n中有多少对相加等于m。

       如果直接暴力的话肯定会超时,毕竟1e14只给了1s,找几个样例手推一下,其实就能发现不需要去遍历,分情况讨论一下就好了。我的代码写的比他们的复杂点...


AC代码:

#include <iostream>
#include <cstring>
#include <cstdio>
#define ll long long
using namespace std;
ll n,m;

int main()
{
	scanf("%I64d%I64d",&n,&m);
	ll temp = m / 2;
	if(m == 1 || m == 2){
		puts("0");
		return 0;
	}
	if(n >= m){
		if(m % 2 == 1){
			printf("%I64d\n",m / 2);
		}
		else{
			printf("%I64d\n",(m - 1) / 2);
		}
		return 0;
	}
	if(temp <= n){
		if(temp > n - temp){
			printf("%I64d\n",n - temp);
		}
		else{
			printf("%I64d\n",temp);
		}
	}
	else puts("0");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Charles_Zaqdt/article/details/81806079
今日推荐