POJ3685 Matrix(嵌套二分)

同行元素递减,同列元素递增,采用嵌套二分的方法

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
using namespace std;
typedef __int64 LL;

LL n, m;
LL F(LL l, LL r){
    return l * l + 100000 * l + r * r - 100000 * r  + l *r;
}

LL solve(LL x){
    LL sum = 0;
    for(int i = 1; i <= n; i++){
        int l = 1, r = n + 1;
        while(l < r){
            LL mid = (l + r)>>1;
            if(F(mid, i) > x){
                r = mid;
            }else{
                l = mid + 1;
            }
        }
        sum += l - 1;

    }
    return sum;
}

int main(){
    int t;
    cin>>t;
    while(t--){
        cin>>n>>m;
        LL l = -100000*n, r = n*n+100000*n+n*n+n*n;
        while(l < r){
            LL mid = (l + r) >> 1;
            if(solve(mid) < m){
                l = mid + 1;

            }else{

                r = mid;
            }
        }
        cout<<l<<‘\n‘;
    }

    return 0;
}

  

时间: 2024-10-23 03:05:10

POJ3685 Matrix(嵌套二分)的相关文章

poj3685(嵌套二分)

Matrix Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 4658   Accepted: 1189 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 ar

HDU 2119 Matrix 简单二分匹配

行做x集,列做y集,1就给该行该列连一条边,输出最大匹配边即可 #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<vector> #include<set> using namespace std; #define N 105 int lef[N], pn;//lef[v]表示Y集的点v 当前连接的点 , pn为x点集的

poj3685 Matrix 二分 函数单调性

  Memory Limit: 65536K Total Submissions: 4637   Accepted: 1180 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 smalles

fafu 1568 Matrix(二分匹配+二分)

Description: You are given a matrix which is n rows m columns (1 <= n <= m <= 100). You are supposed to choose n elements, there is no more than 1 element in the same row and no more than 1 element in the same column. What is the minimum value of

poj3685 Matrix

思路: 二分套二分. 矩阵在每一列上是严格递增的,可以利用这一点进行二分. 实现: 1 #include <cstdio> 2 #include <cmath> 3 #include <algorithm> 4 using namespace std; 5 typedef long long ll; 6 const ll INF = 0x3f3f3f3f3f3f3f3f; 7 ll n, m; 8 ll cal(ll i, ll j) 9 { 10 return i *

ZOJ 3646 Matrix Transformer 二分匹配,思路,经典 难度:2

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4836 因为要使对角线所有元素都是U,所以需要保证每行都有一个不同的列上有U,设(i,j)的位置是U, 以U为边,连接点i和点j+n,也即连接行点和列点,最大匹配为n则必定有解,否则必定无解 #include <cstdio> #include <iostream> #include <cstring> #include <cctype>

POJ 3233 - Matrix Power Series ( 矩阵快速幂 + 二分)

POJ 3233 - Matrix Power Series ( 矩阵快速幂 + 二分) #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long LL; #define MAX_SIZE 30 #define CLR( a, b ) memset( a, b, sizeof(a) ) int MOD = 0; int n, k; st

POJ 3233 Matrix Power Series(矩阵+二分)

题目大意:求由矩阵 A构成的矩阵 S = A + A^2 + A^3 + - + A^k.k的取值范围是:10^9数据很大,应该二分. 对于一个k来说,s(k) = (1+A^(k/2)) *( A+A^2+--+A^(k/2)).如果k为奇数的话需要加上A^(k/2 + 1). 所以二分求和,复杂度就降下来了,当然还得用到矩阵快速幂. Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions

POJ3233:Matrix Power Series(矩阵快速幂+二分)

http://poj.org/problem?id=3233 题目大意:给定矩阵A,求A + A^2 + A^3 + … + A^k的结果(两个矩阵相加就是对应位置分别相加).输出的数据mod m.k<=10^9.这道题两次二分,相当经典.首先我们知道,A^i可以二分求出.然后我们需要对整个题目的数据规模k进行二分.比如,当k=6时,有:A + A^2 + A^3 + A^4 + A^5 + A^6 =(A + A^2 + A^3) + A^3*(A + A^2 + A^3)应用这个式子后,规模