http://hihocoder.com/problemset/problem/1286

题解

NB分析题。

首先我们令\(s[i][j]\)表示以\((i,j)\)为左上角的矩形的权值和。

因为\(a[i][j]+1=a[i+1][j+1]\)。

所以\(s[i][j]+n*m=s[i+1][j+1]\)。

再有当\(i\geq m\)时\(s[i][1]=s[i+1][1]\)。

\(j\geq n\)时\(s[1][i]=s[1][i+1]\)。

那么我们若知道一个\((i,j)\)值,那么所有的\((i+x,j+x)\)中最小的合法解可以通过解同余方程求出。

根据发现的性质,只需要枚举\(n+m\)个\((i,j)\)即可。

代码

#include<bits/stdc++.h>
#define N 200009
using namespace std;
typedef long long ll;
ll n,m,k;
ll dp[N];
inline ll rd(){
ll x=0;char c=getchar();bool f=0;
while(!isdigit(c)){if(c=='-')f=1;c=getchar();}
while(isdigit(c)){x=(x<<1)+(x<<3)+(c^48);c=getchar();}
return f?-x:x;
}
struct node{
int x,y;
inline bool operator <(const node &b)const{
if(x+y!=b.x+b.y)return x+y<b.x+b.y;
return x<b.x;
}
}ans;
inline ll Sum(ll n){return n*(n+1)/2;}
inline ll sum(ll n,ll m){
if(!n||!m)return 0;
if(n>m)swap(n,m);
return dp[n]*2-Sum(n)+(m-n)*Sum(n);
}
inline ll calc(ll x,ll y,ll n,ll m){
return sum(x+n-1,y+m-1)-sum(x-1,y+m-1)-sum(x+n-1,y-1)+sum(x-1,y-1);
}
inline void prework(ll n){
for(int i=1;i<=n;++i)dp[i]=dp[i-1]+1ll*i*(i+1)/2;
}
ll xx,yy,g;
ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}
void exgcd(ll a,ll b){
if(!b){
xx=1;yy=0;
return;
}
exgcd(b,a%b);
ll k=xx;
xx=yy;
yy=k-(a/b)*yy;
}
inline int solve(ll x,ll y,ll k){
x%=k;y%=k;
x=k-x;
if(x%g)return -1;
ll o=(xx*(x/g)%(k/g)+(k/g))%(k/g);
return o;
}
int main(){
int Q=rd();
prework(200000);
while(Q--){
n=rd();m=rd();k=rd();
g=gcd(1ll*n*m%k,k);
exgcd(1ll*n*m%k,k);/
bool tag=0;
int aa=1e9;
ans=node{aa,aa};
for(int i=1;i<=n;++i){
int x=solve(calc(1,i,n,m),1ll*n*m,k);
if(x!=-1){
tag=1;
node du=node{1+x,i+x};
if(du<ans)ans=du;
}
}
for(int i=1;i<=m;++i){
int x=solve(calc(i,1,n,m),1ll*n*m,k);
if(x!=-1){
tag=1;
node du=node{i+x,1+x};
if(du<ans)ans=du;;
}
}
if(tag){
printf("%d %d\n",ans.x,ans.y);
}
else puts("-1");
}
return 0;
}

hihocoder1286 : 子矩阵求和的更多相关文章

  1. hihoCoder [Offer收割]编程练习赛3 D子矩阵求和

    子矩阵求和 http://hihocoder.com/discuss/question/3005 声明一下: n是和x一起的,m是和y一起的 x是横着的,y是纵着的,x往右为正,y往下为正 (非常反常 ...

  2. HDU 6336 子矩阵求和

    Problem E. Matrix from Arrays Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 ...

  3. 【hiho一下 第146周】子矩阵求和

    [题目链接]:http://hihocoder.com/contest/hiho146/problem/1 [题意] [题解] 设s[i][j]表示左上角的坐标为(i,j)的n*m的矩阵的和; 有s[ ...

  4. HDU 6336.Problem E. Matrix from Arrays-子矩阵求和+规律+二维前缀和 (2018 Multi-University Training Contest 4 1005)

    6336.Problem E. Matrix from Arrays 不想解释了,直接官方题解: 队友写了博客,我是水的他的代码 ------>HDU 6336 子矩阵求和 至于为什么是4倍的, ...

  5. 【HDOJ-1081】To The Max(动态规划)

    To the Max Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem ...

  6. AtCoder Grand Contest 018题解

    传送门 \(A\) 根据裴蜀定理显然要\(k|\gcd(a_1,...,a_n)\),顺便注意不能造出大于\(\max(a_1,...,a_n)\)的数 int n,g,k,x,mx; int mai ...

  7. 【JZOJ4817】【NOIP2016提高A组五校联考4】square

    题目描述 输入 输出 样例输入 3 4 1 1 0 1 0 1 1 0 0 1 1 0 5 1 1 2 3 2 1 3 2 3 2 3 4 1 1 3 4 1 2 3 4 样例输出 1 1 1 2 2 ...

  8. 【ACM程序设计】前缀和

    前缀和 ​ 前缀和是指某序列的前n项和,可以把它理解为数学上的数列的前n项和 作用: 一种预处理,求出的前缀和数组可以使得,输出原序列中从第l个数到第r个数和的时间复杂度变成了O(1) . 一维前缀和 ...

  9. 2021-2022 ICPC, NERC, Northern Eurasia Onsite (Unrated, Online Mirror, ICPC Rules, Teams Preferred) J. Job Lookup

    题意 n个节点,n<=200,你需要构造这n个几点成为一棵树,并且这棵树的中序遍历为1-n; 你构造树的节点之间的最短路构成一个n×n的最短距离矩阵d: 同时给你n×n的权重矩阵c:最最小的Σd ...

随机推荐

  1. sourceInsight下标题栏显示文件完整路径

    用sourceInsight看代码方便,但是标题栏中不显示文件的完整路径,有时候就会很麻烦,做如下设置即可显示完整的路径 options -> preferences -> Display ...

  2. Sql注入校验

    /// <summary> /// Sql注入校验 /// </summary> /// <param name="listWord">字符&l ...

  3. 解决172.17 或者172.18 机房环境下harbor服务器不通的问题

    直接改docker-compose.yml文件: 把原来的network选项注释掉,自定义 #networks: # harbor: # external: false networks: harbo ...

  4. mysql先分组,然后取每个分组中的第2大的记录

    文章参考http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/ 首先建表: ...

  5. 前端 CSS 盒子模型 边框 border属性

    边框 border:边框的意思,描述盒子的边框 边框有三个要素: 粗细 线性样式 颜色 border: solid border特性 如果颜色不写,默认是黑色.如果粗细不写,不显示边框.如果只写线性样 ...

  6. centos6.9 安装mysql8

    centos6.9 安装 mysql8 # 安装mysql8 1.下载https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.16-2.el6.x86 ...

  7. 浅拷贝&深拷贝

    浅拷贝新的对象指向原来对象的地址 深拷贝新的对象中,原来是可变对象,会新复制一份值指向新的地址[11,22,33]若原来的对象里含有可变对象,里面的这个可变对象也会指向新的地址['qwer', 123 ...

  8. HashMap中确定数组位置为什么要用hash进行扰动

    HashMap数据存储的过程先根据key获得hash值,通过 (n - 1) & hash 判断当前元素存放的位置(这里的 n 指的是数组的长度),如果当前位置存在元素的话,就判断该元素与要存 ...

  9. layoutSubviews何时调用的问题(原文:http://www.cnblogs.com/pengyingh/articles/2417211.html)

    今天跟旺才兄学习了一下UIView的setNeedsDisplay和setNeedsLayout方法.首先两个方法都是异步执行的.而setNeedsDisplay会调用自动调用drawRect方法,这 ...

  10. Linux服务器安装系统之1-LSI阵列卡raid5配置方法