# poj3685 二分答案+找规律

Matrix
 Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 7915 Accepted: 2388

Description

Given a N × N matrix A, whose element in the i-th row and j-th column Aij is an number that equals i2 + 100000 × i + j2 - 100000 × j + i × j, you are to find the M-th smallest element in the matrix.

Input

The first line of input is the number of test case.
For each test case there is only one line contains two integers, N(1 ≤ N ≤ 50,000) and M(1 ≤ M ≤ N × N). There is a blank line before each test case.

Output

For each test case output the answer on a single line.

A[i+1][j] = A[i][j] + (2*i + j + 1 + 100000);（同一列递推式）

A[i][j+1] = A[i][j] + (2*j + i + 1 - 100000); （同一行递推式）

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<set>
#define sd(x) scanf("%d",&x)
#define ss(x) scanf("%s",x)
#define sc(x) scanf("%c",&x)
#define sf(x) scanf("%f",&x)
#define slf(x) scanf("%lf",&x)
#define slld(x) scanf("%lld",&x)
#define me(x,b) memset(x,b,sizeof(x))
#define pd(d) printf("%d\n",d);
#define plld(d) printf("%lld\n",d);
#define eps 1.0E-8
// #define Reast1nPeace

typedef long long ll;

using namespace std;

const ll INF = 0x3f3f3f3f3f3f3f;

ll x,y;

ll get(ll i,ll j){
return i*i + 100000*i + j*j -100000*j + i*j;
}

bool judge(ll num){
ll sum = 0;
for(int j = 1 ; j<=x ; j++){
ll l = 1 ; ll r = x;
ll ans = 0;
while(l<=r){
ll mid = (l+r)/2;
if(get(mid,j) <= num){
ans = mid;
l = mid+1;
}
else{
r = mid-1;
}
}
sum += ans;
}
return sum >= y;
}

int main(){
#ifdef Reast1nPeace
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
ios::sync_with_stdio(false);
int T;
cin>>T;
while(T--){
cin>>x>>y;

ll l = -INF; ll r = INF;
ll ans;
while(l<=r){
ll mid = (l+r)/2;
if(judge(mid)){
ans = mid;
r = mid-1;
}
else{
l = mid+1;
}
}
cout<<ans<<endl;
}
return 0;
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120