codeforce Gym 100500F Door Lock (二分)
根据题意略推一下,其实就是问你满足(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; }