sicily 1047. Super Snooker

#include <iostream> //因为有多个样例N ≤ 10000,所以对 连续数[low,high] 直接套用 0-1背包会TLE
#include<stdio.h>
using namespace std;
int main()
{
int cases,a,b,low,high;
cin
>>cases;
while(cases--)
{
cin
>>a>>b>>low>>high;
if(a>b)
swap(a,b);
//假定a<b
int s=(low+high)*(high-low+1)/2;
if((a+b+s)%2==1||b>a+s||(s+b-a)%2==1) //(s+b-a)%2==0才能保证下面的 x 是整数
{
printf(
"not possible\n");
}
else
{
int x=(s+b-a)/2; //a+x=b+s-x
int tag=0,m,n;
for(int len=1;len<=high-low+1;++len) // x 由 len个在[low,high]内的数组成
{
m
=(low+low+len-1)*len/2;n=(high-len+1+high)*len/2; //m=low+..+low+len-1 n=high-len+1+...+high
if(m<=x&&x<=n) //因为是连续数,所以 len 个在[low,high]内的数的和 必然是在[m,n]内,故只要 x 落在该范围内,则组合成功
{
tag
=1; break;
}
}
if(tag==1)
printf(
"possible\n");
else
printf(
"not possible\n");
}
}
return 0;
}

posted on 2011-07-06 15:30  sysu_mjc  阅读(226)  评论(0编辑  收藏  举报

导航