码迷,mamicode.com
首页 > 其他好文 > 详细

CF Gym 100500F Door Lock

时间:2015-07-27 20:34:41      阅读:95      评论:0      收藏:0      [点我收藏+]

标签:

根据题意略推一下,其实就是问你满足(a*(a+1))/2 < m <= ((a+1)*a(a+2))/2的a和m-(a*(a+1))/2 -1是多少。

二分求解就行了

#include<cstdio>

using namespace std;
typedef long long ll;

int main()
{
    int T;
    scanf("%d",&T);
    for(int k = 1; k <= T; k++){
        ll n,m;
        scanf("%I64d%I64d",&n,&m);
        ll L = 0, R = n;
        ll mid , b1, b2;
        while(L < R){
            mid = (L + R) >> 1;
            b1 = (mid*(mid+1))>>1;
            if(m <= b1) {
                R = mid;
                continue;
            }
            b2 = ((mid+2)*(mid+1))>>1;
            if(b2 < m) {
                L = mid;
                continue;
            }
            break;
        }
        if(L == R) mid = L;
        printf("Case %d: %I64d %I64d\n",k,mid,m-b1-1);
    }
    return 0;
}

 

CF Gym 100500F Door Lock

标签:

原文地址:http://www.cnblogs.com/jerryRey/p/4680870.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!