惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Proofpoint News Feed
WordPress大学
WordPress大学
酷 壳 – CoolShell
酷 壳 – CoolShell
T
Tailwind CSS Blog
J
Java Code Geeks
Engineering at Meta
Engineering at Meta
博客园_首页
G
Google Developers Blog
有赞技术团队
有赞技术团队
Vercel News
Vercel News
Last Week in AI
Last Week in AI
博客园 - 三生石上(FineUI控件)
Jina AI
Jina AI
IT之家
IT之家
The GitHub Blog
The GitHub Blog
Google DeepMind News
Google DeepMind News
Microsoft Security Blog
Microsoft Security Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - Franky
A
About on SuperTechFans
The Register - Security
The Register - Security
腾讯CDC
月光博客
月光博客
GbyAI
GbyAI
博客园 - 叶小钗
Blog — PlanetScale
Blog — PlanetScale
D
Docker
D
DataBreaches.Net
博客园 - 聂微东
C
Check Point Blog
T
The Blog of Author Tim Ferriss
人人都是产品经理
人人都是产品经理
F
Full Disclosure
Martin Fowler
Martin Fowler
Apple Machine Learning Research
Apple Machine Learning Research
雷峰网
雷峰网
美团技术团队
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 司徒正美
小众软件
小众软件
量子位
L
LangChain Blog
S
SegmentFault 最新的问题
Hugging Face - Blog
Hugging Face - Blog
大猫的无限游戏
大猫的无限游戏
MongoDB | Blog
MongoDB | Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
I
InfoQ
F
Fortinet All Blogs

distjr_的博客

最新 | 画中少女 | distjr_的博客 天津行记 | distjr_的博客 最终选择去天津大学了 | distjr_的博客 上伊那牡丹,看完了 | distjr_的博客 上伊那牡丹,看完了 | distjr_的博客 帖子测试 | distjr_的博客 最新 | 关于我的高中三年 | distjr_的博客 最新 | KeepOpen:一款保持移动硬盘开启状态的小工具 | distjr_的博客 最新 | KeepOpen:一款保持移动硬盘开启状态的小工具 | distjr_的博客 最新 | 关于我的高中三年 | distjr_的博客 一款基于Python的随机学生点名器 | distjr_的博客 最新 | 一款基于Python的随机学生点名器 | distjr_的博客 U571839 千本桜 题解 | distjr_的博客 U571839 千本桜 题解 | distjr_的博客 土地 | distjr_的博客 土地 | distjr_的博客 一九六八年 | distjr_的博客 一九六八年 | distjr_的博客 原野 | distjr_的博客 原野 | distjr_的博客 一种基于人工智能技术的系统发生树绘制与物种演化路径推断系统 | distjr_的博客 一种基于人工智能技术的系统发生树绘制与物种演化路径推断系统 | distjr_的博客 回来?Ⅱ | distjr_的博客 回来?Ⅱ | distjr_的博客 U314392 distjr_想买书 题解 | distjr_的博客 U314392 distjr_想买书 题解 | distjr_的博客 天涯边 | distjr_的博客 天涯边 | distjr_的博客 CF509D Restoring Numbers 题解 | distjr_的博客 回来?Ⅰ | distjr_的博客 回来?Ⅰ | distjr_的博客 一场提前告知的自杀 | distjr_的博客 一场提前告知的自杀 | distjr_的博客 test | distjr_的博客 test | distjr_的博客 置顶 | 关于我自己 | distjr_的博客 置顶 | 关于我自己 | distjr_的博客
CF509D Restoring Numbers 题解 | distjr_的博客
文章作者: distjr_ · 2023-11-05 · via distjr_的博客

题目描述

给你一个 $n \times m$ 的矩阵 $v$。

$v_{i, j} = (a_i + b_j) \bmod{k}$。

求两个序列及其模数 $k$。

思路

注意到,如果我们将 $a$ 序列中的所有数加上一,$b$ 序列中的所有数减去一,整个矩阵不会改变。

因此我们不妨设 $a_1 = 0$,通过第一列推出 $b$ 序列的值,进而推出 $a$ 序列的值。现在我们为每个格子 $(i, j)$ 计算一个误差 $error = a_i + b_j - v_{i, j}$。

如果所有的误差均为 $0$,则已经找到了一个满足条件的矩阵。否则意味着误差一定是 $k$ 的倍数,即 $k$ 一定是所有误差的公因数。另外 $k$ 还必须大于所有 $v$ 矩阵中出现的数。如果满足这两个条件则有解,否则无解。

代码

/**
 * @file CF509D.cpp
 * @author distjr_
 * @brief Solution of CF509D
 */
#include <cstdio>
#include <cstdlib>
#define MAXN 105
#define int long long
// #define DEBUG
using namespace std;

int n, m, v[MAXN][MAXN], a[MAXN], b[MAXN], error, k = 0;
bool flag = true;
int (*gcd)(int, int) = [](int a, int b)
{ return ((b == 0) ? a : gcd(b, a % b)); };


signed main()
{
    scanf("%lld %lld", &n, &m);
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= m; ++j)
            scanf("%lld", &v[i][j]);
    for (int i = 1; i <= m; ++i)
        b[i] = v[1][i];
    for (int i = 1; i <= n; ++i)
        a[i] = v[i][1] - b[1];
    for (int i = 1; i <= n; ++i)
        for (int j = 1; j <= m; ++j)
            error = abs(a[i] + b[j] - v[i][j]), k = gcd(k, error);
    if (!k)  // k为0,故最后所有的误差值都是0,直接给k赋值为1e9+7即可
        k = 1000000007;
    for (int i = 1; i <= n && flag; ++i)
        for (int j = 1; j <= m; ++j)
            if (v[i][j] >= k)
            {
                flag = false;
                break;
            }
    if (!flag)
    {
        printf("NO\n");
        return 0;
    }
    printf("YES\n%lld\n", k);
    for (int i = 1; i <= n; ++i)
        printf("%lld ", (a[i] + k) % k);
    printf("\n");
    for (int i = 1; i <= m; ++i)
        printf("%lld ", (b[i] + k) % k);
    printf("\n");
    return 0;
}

以上。如有问题烦请指出,谢谢。