Sicily 1047 Super Snooker

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

On one of my many interplanetary travels I landed on a beautiful little planet called Crucible. It was inhabited by an extremely peace-loving people who called themselves Snooks. They received me very gracefully and told me I had arrived at precisely the right time, as the biggest event of the year was just then taking place: the Super Snooker Championship. Of course I couldn’t decline an invitation to go and watch.
That year the Super Snooker Championship was contested by two experienced Snooks universally ac-claimed as the best players on the planet: Stephanie McHendry and Joanna McHiggins. The game involved an immense rectangular table covered with green cloth and lined by edges two inches high, except in the four corners and in the middle of the longer sides where there were holes. On it were put a number of balls (from 6 up to as many as 25), each representing a value or certain number of points (anywhere from 2 to 1000, but numbered consecutively). Each player in turn tried to nudge the lowest valued ball left on the table into one of the holes on the edges of the table using a strange limb called a “kew”. If one succeeded, she was said to have “podded” the ball and the value of the podded ball was added to her score.
But here is the strange thing: the object of the game was not to finish with more points than the opponent. No, being a people who loved peace above all else, the object for both players was to end up with an equal number of points. This presented a bit of a problem. It was very important to them to know if it was possible to finish equal given the score of both players and the values of the balls left on the table. For instance, with a score-line of 56–34 and three balls left with values 13, 14 and 15, it is impossible to reach equal end-scores. If there are five balls left with values 20–24, it is possible: 56 + 20 + 24 = 34 + 21 + 22 + 23 = 100. You are asked to write a program that helps the Snooks by calculating whether it is possible for two Super Snooker players to win their game by finishing equal, given a score-line and the range of values of the range of the remaining balls.

Input

The input consists of a line containing the number of configurations N (0 ≤ N ≤ 10000) to be calculated. It is followed by N lines each containing two scores and the lowest and highest values of the remaining balls.

Output

The output consists of one line for each configuration with the string ‘possible’ or the string ‘not possible’, depending on whether it is possible or not to finish equal from the given configuration.

Sample Input

5
56 34 13 15
56 34 20 24
0 0 500 519
0 0 500 520
0 0 500 521

Sample Output

not possible
possible
possible
not possible
not possible

Solution

题目大意就是给出两个数a,b 然后给出一个区间[st, end]。问,能否找到一种方案,将区间的数划分为两组,其和为S1,S2,使得a+S1=b+S2。一开始读懂题意之后,受到之前一道分石头的题目的影响,就感觉直接暴力搜索吧,感觉2^26应该剪枝就ok了的样子。结果就悲剧了,10^5的一次输入,大概合起来就算剪枝也大概10^10以上的样子。总以为自己剪枝的姿势不对,感觉剪枝就是预计节点能否再进行拓展,我应该都估计到了啊......于是请教大神,大神说这是一道数论题,直接解方程就可以了。因为st到end是连续的。设已经找到了一个方案满足要求,其和分别为s1,s2(s1<s2),那么就有a-b=s2-s1(a>b)和s1+s2=(st+end)*(end-st+1)/2,对于s1是有要求的,其元素的个数必须大于等于0,于是联立方程解s1,有2*s1=(st+end)*(end-st+1)/2-a+b,注意这里要判断s1为偶数,不然就没有解了。


接着按照s1的数值分布列出与元素个数i之间的不等式关系:st+(st+1)+(st+2)+...+(st+i-1) <=s1<=end+(end-1)+(end-2)+...+(end-i+1)


如果不等式有合法解即可以找到方案,按方程左边求出i值代入右边验证即可。

#include <stdio.h>
#include <math.h>
#include <algorithm>

using namespace std;

int main()
{
  int n;

  scanf("%d", &n);
  for (int i = 0; i < n; ++i)
  {
    int a, b, s, e;

    scanf("%d%d%d%d", &a, &b, &s, &e);
    if (a < b) swap(a, b);
    int s1 = ((s + e) * (e - s + 1) / 2 + b - a); //计算较小的和
    int i1 = (sqrt((2*s-1)*(2*s-1) + 4*s1)-2*s+1) / 2; // 按不等式解出数字个数

    if (s1 < 0 || s1 % 2 || i1 < 0) printf("not possible\n");
    else
    {
      if ((2*e-i1+1)*i1 >= s1) printf("possible\n");
      else printf("not possible\n");
    }
  }

  return 0;
}
数学题要多练多思考,不要忘了数学是本源。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值