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
556 34 13 1556 34 20 240 0 500 5190 0 500 5200 0 500 521
Sample Output
not possiblepossiblepossiblenot possiblenot possible
題目分析:
用窮舉法,2^n的時間複雜度,逾時。
網上一位大神的數學解法(http://blog.sina.com.cn/s/blog_7064e7850100yjg1.html)
假設,兩人已有分數是x1和x2(假設x1>x2)剩餘球的範圍是min到max,現在假設給x1這個人其餘的球的集合是sum1,那麼x2這人的集合是sum2
因為min到max連續 則sum1+sum2=(min+max)(max-min+1)/2
還有sum1+a=sum2+b
通過這兩個方程解出sum1*2=(min+max)(max-min+1)/2+x1-x2
則後面那個式子(min+max)(max-min+1)/2+x1-x2為偶數,且大於等於0(若小於0說明,把所有球給小積分的人都小於大積分的人)
現在解出了sum1,然後窮舉當給sum1這人0個球到max-min+1個球的最大值,最小值情況
例如,給sum1 i個球時,sum1的取值範圍是在(min+0)+(min+1)+……(min+i-1)到(max-i+1)……max
如果sum1滿足其中一個i值,那麼就成立
/**/#include<iostream>#include <iomanip>#include<stdio.h>#include<cmath>#include<iomanip>#include<list>#include <map>#include <vector>#include <string>#include <algorithm>#include <sstream>#include <stack>#include<queue>#include<string.h>using namespace std;int main(){int n;scanf("%d",&n);for(int xx=0;xx<n;xx++){int x1,x2,min,max;bool flag=false;long long sum1=0,sum2=0;//設sum1小於sum2scanf("%d%d%d%d",&x1,&x2,&min,&max);if(x1>x2)swap(x1,x2);long long tmp=(min+max)*(max-min+1)/2+x1-x2;if(tmp%2==1||tmp<0)//tmp小於0時表示,所有的都加到小的上面還沒有上面大{printf("not possible\n");continue;//flag=false;}sum1=tmp/2;for(int i=0;i<=max-min+1;i++){long long l=0,r=0;for(int j=0;j<i;j++){l+=min+j;r+=max-j;}//end for jif(sum1>=l&&sum1<=r)flag=true;}if(flag)printf("possible\n");else printf("not possible\n");}}