Codeforces Gym 100500F Problem F. Door Lock 二分

时间:2022-11-18 17:54:44

Problem F. Door Lock
Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/gym/100500/attachments

Description

It was a beautiful night at Yekaterinburg, the sky was crystal clear with no clouds, and the view of the moon and the stars was magnificent. Coach Fegla was enjoying this view at the Novotel hotel with Hanaa El-Jazzar (a member of the systems operation team of the ACPC). It was the first time for Hanaa to attend the ICPC world finals event, and she was so happy about it and brought coach Fegla a gift from her hometown Lebanon as a sign of gratitude. The gift was a round shaped plate with engraved images of different sightseeing in Lebanon. While Hanaa was showing the present to the coach, two drunk fellows entered the room by mistake. They were so high they could not distinguish their room, from the coach’s room. Coach Fegla thought what if the ones who entered the room were complete strangers, what should he do? He decided to install another lock to his door, but it was not like any other lock. To obtain the key of the lock a certain puzzle has to be solved, this way coach Fegla might be able to stop most of the intruders. The puzzle was as follows, you will be given 2 numbers n and m. You should build up a domino set. Each piece of the set will contain 2 integer numbers in the range from 0 to n-1, and all the pieces are pairwise distinct and no two pieces of domino should look the same if rotated. As you know a domino piece is divided into 2 halves where the top half contains one integer and other half contains the other integer. You should lay down the pieces in a straight line where the top half of each piece should contain the greatest value of the 2 halves. Then you should sort the domino pieces in increasing order based on the top half, in case of equality sort them in increasing order based on the other half. The lock to the key will be the mth piece in this sequence. You will be given n, m can you get lock key? (check the image below for a domino set where n = 3)

Input

The first line will be the number of test cases T. Each of the following lines will contain 2 numbers n, m. 1 ≤ T ≤ 100,000 1 ≤ n ≤ 1,000,000,000 1 ≤ m ≤ n×(n+1) 2

Output

For each test case print a single line containing: Case_x:_a_b x is the case number starting from 1. a is the greatest value on the domino piece, and b is the other value. Replace underscores with spaces.

Sample Input

2 2 1 3 5

Sample Output

Case 1: 0 0 Case 2: 2 1

HINT

题意

让你用两个数来记录这个数的大小,大小记录规则由题目给出

题解

简单分析之后,发现这其实是一个前缀和的东西,所以我们直接二分就好了

代码

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
const int maxn=;
#define mod 1000000007
#define eps 1e-9
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//************************************************************************************* int main()
{
int t=read();
for(int cas=;cas<=t;cas++)
{
ll n,m;
n=read(),m=read();
if(m==)
{
printf("Case %d: 0 0\n",cas);
continue;
}
ll l=,r=n;
while(l<r)
{
ll mid=(l+r)/;
if((mid*mid+mid)/>m)
r=mid;
else if((mid*mid+mid)/==m)
l=r=mid;
else
l=mid+;
}
l--,m--;
printf("Case %d: %lld %lld\n",cas,l,m-(l*l+l)/);
}
}