codeforces 509 D. Restoring Numbers(数学+构造)
题目链接: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(数学+构造)的更多相关文章
- 【CodeForces】708 B. Recover the String 数学构造
[题目]B. Recover the String [题意]找到一个串s,满足其中子序列{0,0}{0,1}{1,0}{1,1}的数量分别满足给定的数a1~a4,或判断不存在.数字<=10^9, ...
- [codeforces 509]C. Sums of Digits
[codeforces 509]C. Sums of Digits 试题描述 Vasya had a strictly increasing sequence of positive integers ...
- [codeforces 55]D. Beautiful numbers
[codeforces 55]D. Beautiful numbers 试题描述 Volodya is an odd boy and his taste is strange as well. It ...
- Restoring Numbers
D. Restoring Numbers ...
- 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 ...
- 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 ...
- Sonya and Matrix CodeForces - 1004D (数学,构造)
http://codeforces.com/contest/1004/problem/D 题意:网格图给定到中心点的曼哈顿距离数组, 求该图n,m及中心点位置 首先可以观察到距离最大值mx一定在某个角 ...
- CodeForces 682A Alyona and Numbers (水题,数学)
题意:给定两个数 m,n,问你在从1到 n,和从 1到 m中任选两个数加起来是5的倍数,问你有多少个. 析:先计算 m 和 n中有多少个取模5是从0到4的,然后根据排列组合,相乘就得到了小于等于 m ...
- CodeForces - 83D:Numbers (数学&递归 - min25筛 )
pro:给定三个整数L,R,P求[L,R]区间的整数有多少个是以P为最小因子的.L,R,P<2e9; sol: 一: 比较快的做法是,用函数的思想递归. 用solve(N,P)表示求1到N有多少 ...
随机推荐
- 【Python-Django后端开发】配置静态文件详解!!!
配置前端静态文件 1. 准备静态文件 2. 指定静态文件加载路径 STATIC_URL = '/static/' # 配置静态文件加载路径 STATICFILES_DIRS = [os.path.jo ...
- JavaScript ES6和ES5闭包的小demo
版权声明:署名,允许他人基于本文进行创作,且必须基于与原先许可协议相同的许可协议分发本文 (Creative Commons) 可能有些小伙伴不知道ES6的写法,这儿先填写一个小例子 let conn ...
- Pyenv虚拟环境的创建(虚拟机)
创建pyenv虚拟环境 sudo yum install openssl* 安装其所需要的库文件 git clone https://github.com/yyuu/pyenv.git ~/.pyen ...
- 大型系列课程之-七夕告白之旅Electron篇
上一篇分享了一下vbs的撩妹攻略,但细心的兄弟会发现,这种脚本式的攻城方案并不得心应手,有很多妹子害怕是病毒根本不敢点击,而且这个脚本界面风格也不漂亮,不能轻易打动妹子的心,怎么破,小编这次在为各位老 ...
- 前端本地proxy跨域代理配置
等了好久的接口,总算拿到了,结果却发现用本地localhost:9712去请求接口的时候,出现了跨域错误,而这个时候我们就需要进行下跨域配置了. 首先,找到项目中名为webpack.config.js ...
- 初识代理——Proxy
无处不在的模式——Proxy 最近在看<设计模式之禅>,看到代理模式这一章的时候,发现自己在写spring项目的时候其实很多时候都用到了代理,无论是依赖注入.AOP还是其他,可以说是无处不 ...
- SpringBoot 2 HTTP转HTTPS
@Bean public TomcatServletWebServerFactory servletContainer() { TomcatServletWebServerFactory tomcat ...
- 5月29日 Java性能调优指南 读后感
并行垃圾收集器 串行垃圾收集器 并发标记清除(CMS)垃圾收集器 Garbage First(G1)垃圾收集器 没有深入的学习G1的原理,只是看了大概的思想; SA工具:待学习
- 进程间通信与ipcs使用7例
进程间通信(IPC, inter-process communication)实现进程间消息的传递,对于用户地址空间相互独立的两个进程而言,实现通信可以通过以下方式: 由内核层面分配内存,两进程共享该 ...
- git语句(后续补充)
如果你是windows用户,需要下载一个git应用程序,一路点就行,没有什么需要注意的地方 安装完成后在任一文件夹内右键都有显示,单击git bash here即可 简易的命令行入门教程: Git 全 ...