题目链接:http://codeforces.com/problemset/problem/509/D

题意:题目给出公式w[i][j]= (a[i] + b[j])% k; 给出w,要求是否存在这样的数列,若存在则求出a,b 和k

题解:如果有符合条件的点的话那么a[i],b[j]可以任意转换比如说a[i],b[j]可以转化为a[i]-p,b[i]+p。所以只要存在

符合条件的解的a[i]可以为任意值。也就是说a[i]可以先赋值为0然后其他就都可以推出来了,当然开始推出后会发现

有负的,没事,还要模一个k,接下来就要验证是否存在k。

不妨设e[i][j]=abs(a[i]+b[j]-w[i][j])显然要使e[i][j]为0才能符合条件,所以要使e[i][j]%k=0。求一下所有e[i][j]

的gcd得出一个g。显然g就是k。如果g为0肯定存在而且k取最大值+1,否则,判断g是不是大于所有的w[i][j],如果是

那么k存在为最大值+1,不然不存在。

#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long ll;
const ll inf = 1e18;
ll a[200] , b[200] , w[200][200] , e[200][200];
ll gcd(ll x , ll y) {
return (y > 0) ? gcd(y , x % y) : x;
}
int main() {
int n , m;
cin >> n >> m;
for(int i = 0 ; i < n ; i++) {
for(int j = 0 ; j < m ; j++) {
cin >> w[i][j];
}
}
a[0] = 0;
for(int j = 0 ; j < m ; j++) {
b[j] = w[0][j] - a[0];
}
for(int j = 1 ; j < n ; j++) {
a[j] = w[j][0] - b[0];
}
ll gg = a[0] + b[0] - w[0][0];
for(int i = 0 ; i < n ; i++) {
for(int j = 0 ; j < m ; j++) {
e[i][j] = abs(a[i] + b[j] - w[i][j]);
gg = gcd(gg , e[i][j]);
}
}
ll k = 0;
if(gg == 0) {
for(int i = 0 ; i < n ; i++) {
for(int j = 0 ; j < m ; j++) {
k = max(k , w[i][j] + 1);
}
}
cout << "YES" << endl;
cout << k << endl;
for(int i = 0 ; i < n ; i++) {
if(a[i] < 0) {
cout << w[i][0] + k - b[0] << ' ';
}
else {
cout << a[i] << ' ';
}
}
cout << endl;
for(int i = 0 ; i < m ; i++) {
cout << b[i] << ' ';
}
cout << endl;
}
else {
int flag = 0;
for(int i = 0 ; i < n ; i++) {
for(int j = 0 ; j < m ; j++) {
if(gg <= w[i][j]) {
flag = 1;
break;
}
}
}
if(flag) {
cout << "NO" << endl;
}
else {
cout << "YES" << endl;
cout << gg << endl;
for(int i = 0 ; i < n ; i++) {
cout << w[i][0] + gg - b[0] << ' ';
}
cout << endl;
for(int i = 0 ; i < m ; i++) {
cout << b[i] << ' ';
}
cout << endl;
}
}
return 0;
}

codeforces 509 D. Restoring Numbers(数学+构造)的更多相关文章

  1. 【CodeForces】708 B. Recover the String 数学构造

    [题目]B. Recover the String [题意]找到一个串s,满足其中子序列{0,0}{0,1}{1,0}{1,1}的数量分别满足给定的数a1~a4,或判断不存在.数字<=10^9, ...

  2. [codeforces 509]C. Sums of Digits

    [codeforces 509]C. Sums of Digits 试题描述 Vasya had a strictly increasing sequence of positive integers ...

  3. [codeforces 55]D. Beautiful numbers

    [codeforces 55]D. Beautiful numbers 试题描述 Volodya is an odd boy and his taste is strange as well. It ...

  4. Restoring Numbers

                                                                                  D. Restoring Numbers   ...

  5. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) D. Artsem and Saunders 数学 构造

    D. Artsem and Saunders 题目连接: http://codeforces.com/contest/765/problem/D Description Artsem has a fr ...

  6. Codeforces 715A. Plus and Square Root[数学构造]

    A. Plus and Square Root time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  7. Sonya and Matrix CodeForces - 1004D (数学,构造)

    http://codeforces.com/contest/1004/problem/D 题意:网格图给定到中心点的曼哈顿距离数组, 求该图n,m及中心点位置 首先可以观察到距离最大值mx一定在某个角 ...

  8. CodeForces 682A Alyona and Numbers (水题,数学)

    题意:给定两个数 m,n,问你在从1到 n,和从 1到 m中任选两个数加起来是5的倍数,问你有多少个. 析:先计算 m 和 n中有多少个取模5是从0到4的,然后根据排列组合,相乘就得到了小于等于 m ...

  9. CodeForces - 83D:Numbers (数学&递归 - min25筛 )

    pro:给定三个整数L,R,P求[L,R]区间的整数有多少个是以P为最小因子的.L,R,P<2e9; sol: 一: 比较快的做法是,用函数的思想递归. 用solve(N,P)表示求1到N有多少 ...

随机推荐

  1. ubuntu .deb .tar.gz .tar.bz2 .rmp 和命令方式安装软件的方法

    今天在Ubuntu11.10中安装Google chrome浏览器是遇到了问题,下载好的".deb"格式的安装文件google-chrome-stable.deb双击后或者右键快捷 ...

  2. Golang版本的rocksdb-对gorocksdb的封装

    rocksdb的优秀特性不用多说,但是它是用c++语言写的,就是这一个特点就把很多人拦住了.虽然rocksdb官方也有Java版本,但是Golang的发展速度让人不容小觑,而且由于golang原生对高 ...

  3. 蓝桥杯 2n皇后问题 深搜

    默认大家会了n皇后问题 基础练习 2n皇后问题   时间限制:1.0s   内存限制:512.0MB     问题描述 给定一个n*n的棋盘,棋盘中有一些位置不能放皇后.现在要向棋盘中放入n个黑皇后和 ...

  4. pytest

    pytest可以生成多种样式的结果:1.生成JunitXML格式测试报告:命令: --junitxml=path(相对路径)2.生成result log 格式的测试报告: 命令:--resultlog ...

  5. 把Jar包加入windows系统服务

    之前在服务器上不一个Java服务时候,总是开着一堆黑框框,非常不雅,重点是极其容易误关,所以把可执行Jar文件加入Windows系统服务,看起来是个非常不错的选择!(实际上也确实是非常不错的选择) ! ...

  6. vue-cli中的跨域之proxytable

    为什么会有跨域? 浏览器有一个叫做同源策略的东西.同源策略限制了从同一个源加载的文档或脚本如何与来自另一个源的资源进行交互.这是一个用于隔离潜在恶意文件的重要安全机制. 同源策略规定了如果两个页面的协 ...

  7. 微信分享(移动web端)

    create-at 2019-02-16 引入微信JS-SDK http://res.wx.qq.com/open/js/jweixin-1.4.0.js (当前最新版本) js 相关代码 (移动端实 ...

  8. indexedDB添加,删除,获取,修改

    [toc] 在chrome(版本 70.0.3538.110)测试正常 编写涉及:css, html, js 在线演示codepen html代码 <h1>indexedDB</h1 ...

  9. ImageLoader_显示图片

    public class MainActivity extends AppCompatActivity { private ListView lv; private List<Bean.Resu ...

  10. cmd中,查询sqlcmd命令的选项

    像我这样的小白,有时候看到-d,-S,-P这些都不知道什么意思,后面知道了是一些命令的选项.如sqlcmd,打开cmd,输入sqlcmd -?        即可获得选项的含义. .