Restoring Numbers
Vasya had two arrays consisting of non-negative integers: a of size n and b of size m. Vasya chose a positive integerk and created an n × m matrix v using the following formula:

Vasya wrote down matrix v on a piece of paper and put it in the table.
A year later Vasya was cleaning his table when he found a piece of paper containing an n × m matrix w. He remembered making a matrix one day by the rules given above but he was not sure if he had found the paper with the matrix v from those days. Your task is to find out if the matrix w that you've found could have been obtained by following these rules and if it could, then for what numbers k, a1, a2, ..., an, b1, b2, ..., bm it is possible.
The first line contains integers n and m (1 ≤ n, m ≤ 100), separated by a space — the number of rows and columns in the found matrix, respectively.
The i-th of the following lines contains numbers wi, 1, wi, 2, ..., wi, m (0 ≤ wi, j ≤ 109), separated by spaces — the elements of the i-th row of matrix w.
If the matrix w could not have been obtained in the manner described above, print "NO" (without quotes) in the single line of output.
Otherwise, print four lines.
In the first line print "YES" (without quotes).
In the second line print an integer k (1 ≤ k ≤ 1018). Note that each element of table w should be in range between 0and k - 1 inclusively.
In the third line print n integers a1, a2, ..., an (0 ≤ ai ≤ 1018), separated by spaces.
In the fourth line print m integers b1, b2, ..., bm (0 ≤ bi ≤ 1018), separated by spaces.
有 a 数列 长度为n b 数列长度为m 都是非负整数, 然后 他们两个数组组成一个n*m的矩阵,然后 w[i][j]=(a[i]+b[j])%m;
最后给出了这个矩阵和n 和m 就数列 a 和 数列 b 还有m , 令 a[0]-p=0;
那么我们得到了一个数列 0,a[2]-p-x1k...a[n]-xnk-p- ; b[1]+p-y1k...b[n]+p- yn k, 然后
再搞出一个矩阵 m[i][j] 然后在abs(a[i]+b[i]-w[i][j])=m[i][j] ; 当m中的每个元素都为0时 就可以知道w 中最大的那个+1就是答案了,还有就是有不为0的
可以将数列带进去看一下就发现 m[i][j]=x*k 这样求一次gcd就好了
#include <iostream>
#include <cstdio>
#include <string.h>
#include <stdlib.h>
using namespace std;
const int maxn=;
long long ma[maxn][maxn];
long long a[maxn],b[maxn];
long long e[maxn][maxn];
long long gcd(long long a, long long b){
if(b==) return a;
else return gcd(b,a%b);
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)==){
for(int i=; i<n; ++i)
for(int j=; j<m; ++j)
scanf("%I64d",&ma[i][j]);
a[]=;
long long K;
for(int i=; i<n; ++i)
a[i]=ma[i][]-ma[][];
for(int i=; i<m; ++i)
b[i]=ma[][i];
for(int i=; i<n; ++i)
for(int j=; j<m ; ++j)
e[i][j]=abs(a[i]+b[j]-ma[i][j]);
bool falg=false;
for(int i=; i<n; ++i)
for(int j=; j<m; ++j)
if(e[i][j]!=) falg=true;
K=;
if(falg==false){
for(int i=; i<n; ++i)
for(int j=; j<m; ++j)
K=max(K,ma[i][j]);
K++;
puts("YES");
printf("%I64d\n",K);
for(int i=; i<n; ++i)
printf("%I64d%c",(a[i]%K+K)%K,i==n-?'\n':' '); for(int i=; i<m; ++i)
printf("%I64d%c",(b[i]%K+K)%K,i==m-?'\n':' '); }else{
K=;
bool finde=false;
for(int i=; i<n; ++i){
if(finde) break;
for(int j=; j<m; ++j){
if(e[i][j]!=){
K=e[i][j];
finde=true ; break;
}
}
}
for(int i=; i<n; ++i)
for(int j= ; j<m; ++j){
K=gcd(K,e[i][j]);
}
finde=true;
for(int i=; i<n; ++i)
for(int j=; j<m; ++j)
if(ma[i][j]>=K) finde=false;
if(finde==false){
puts("NO"); continue;
}
puts("YES");
printf("%I64d\n",K);
for(int i=; i<n; ++i)
printf("%I64d%c",(a[i]%K+K)%K,i==n-?'\n':' ');
for(int i=; i<m; ++i)
printf("%I64d%c",(b[i]%K+K)%K,i==m-?'\n':' ');
} }
return ;
}
Restoring Numbers的更多相关文章
- codeforces 509 D. Restoring Numbers(数学+构造)
题目链接:http://codeforces.com/problemset/problem/509/D 题意:题目给出公式w[i][j]= (a[i] + b[j])% k; 给出w,要求是否存在这样 ...
- 题解【CodeForces1154A】Restoring Three Numbers
Description Polycarp has guessed three positive integers \(a\), \(b\) and \(c\). He keeps these numb ...
- CF1154A Restoring Three Numbers 题解
Content 已知有三个正整数 \(a,b,c\),现在给出 \(a+b,a+c,b+c,a+b+c\)(不保证有序)的值,试求出 \(a,b,c\). 数据范围:\(2\leqslant a+b, ...
- Codeforces Round #279 (Div. 2) E. Restoring Increasing Sequence 二分
E. Restoring Increasing Sequence time limit per test 1 second memory limit per test 256 megabytes in ...
- PatentTips – GPU Saving and Restoring Thread Group Operating State
BACKGROUND OF THE INVENTION The present invention relates generally to single-instruction, multiple- ...
- Codeforces Round #623 (Div. 2, based on VK Cup 2019-2020 - Elimination Round, Engine) C. Restoring
C. Restoring Permutation time limit per test1 second memory limit per test256 megabytes inputstandar ...
- Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range
在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
随机推荐
- break、continue、return之间的区别与联系
今天在部署程序的时候,监控日志发现这个问题了.return的问题就这么总结哈. 在软件开发过程中,逻辑清晰是非常之重要的. 代码的规范也是非常重要的.往往细节决定成败.在编写代码的时候,一定要理解语言 ...
- java中被遗忘的native关键字
我是无意间看见JNI( java调用动态链接库dll )这块的东西. 所有记下来:本地声明方法 装载完成dll文件后,将使用的方法用native关键字声明. public native static ...
- MySQL性能优化(四)-- MySQL explain详解
前言 MySQL中的explain命令显示了mysql如何使用索引来处理select语句以及连接表.explain显示的信息可以帮助选择更好的索引和写出更优化的查询语句. 一.格式 explain + ...
- 详解google Chrome浏览器(理论篇)
注解:各位读者,经博客园工作人员反馈,hosts涉及违规问题,我暂时屏蔽了最新hosts,若已经获取最新hosts的朋友们,注意保密,不要外传.给大家带来麻烦,对此非常抱歉!!! 开篇概述 1.详解g ...
- ThreadUtil 多线程处理List,回调处理具体的任务
每次想多线程处理一个大的结果集的时候 都需要写一大堆代码,自己写了个工具类 方便使用 package com.guige.fss.common.util; import com.guige.fss.c ...
- vue2.0+element-ui(01简单点的单页面)
前言: 在<Vue.js权威指南>刚出版的时候,自己就作为一名前端粉捧了一把场,可是真是应了那句“出来混,总是要还的“这句话了,那时候信心满满的买来书想要认真研究,最终却还是把它搁浅了.直 ...
- 《C++ Primer Plus》第12章 类和动态内存分配 学习笔记
本章介绍了定义和使用类的许多重要方面.其中的一些方面是非常微妙甚至很难理解的概念.如果其中的某些概念对于您来说过于复杂,也不用害怕——这些问题对于大多数C++的初学者来说都是很难的.通常,对于诸如复制 ...
- const T* 和 T* const
使用c++的时候,经常会在 const int *p 和 int * const p这个地方迷惑.这里记录一下: const int *p = int const *p //这里const后面的为* ...
- oracle 字段自增 两段代码搞定
(这几天做了个小小课程设计时用的是oracle数据库,第一次用,发现oracle和我们以前用的sql server .mysql是有如此多不同的地方,下面是遇到的问题之一和解决方法,和大家分享下) 用 ...
- 根据Uri获取图片绝对路径,解决Android4.4以上版本Uri转换
转:http://blog.csdn.net/q445697127/article/details/40537945 /** * 根据Uri获取图片绝对路径,解决Android4.4以上版本Uri转换 ...