【Atcoder - AGC027D】Modulo Matrix

一道编程竞赛题目,要求构造一个N*N的矩阵,使得相邻元素满足特定模运算条件,同时元素互不相同。通过染色思想和质数权值策略解决超出数值范围的问题。
摘要由CSDN通过智能技术生成


@题目描述 - English@

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 1100 points

Problem Statement
You are given an integer N.Construct any one N-by-N matrix a that satisfies the conditions below. It can be proved that a solution always exists under the constraints of this problem.

1≤a(i,j)≤10^15

a(i,j) are pairwise distinct integers.

There exists a positive integer m such that the following holds: Let x and y be two elements of the matrix that are vertically or horizontally adjacent. Then, max(x,y) mod min(x,y) is always m.

Constraints
2≤N≤500

Input
Input is given from Standard Input in the following format:
N

Output
Print your solution in the following format:
a(1,1)…a(1,N)

a(N,1) … a(N,N)

Sample Input 1
2
Sample Output 1
4 7
23 10

For any two elements x and y that are vertically or horizontally adjacent, max(x,y) mod min(x,y) is always 3.

@中文题意@

构造一个元素互不相同的N*N矩阵,矩阵元素为不超过10^15的正整数。使得对于矩阵中相邻的两个元素x,y, max ⁡ ( x , y ) m o d    min ⁡ ( x , y ) \max(x,y) \mod \min(x,y) max(x,y)modmin(x,y)为一个正整数常数。

@分析@

假如没有元素互不相同的限制,我们可以对矩阵黑白二染色,形成棋盘状。然后在黑格里填N,白格里面填N+1。

那么现在有了元素互不相同的限制,我们仍然可以沿用这样一个染色思想:令(1,1)为黑格,交替染色,使得白格里的元素总大于它四周黑格的元素。并且为了简化问题,不妨让模数值为 1,这样问题转化为(白格元素值-1)是它四周黑格元素的公倍数。一样为了简化问题,我们令白格元素=四周黑格元素的LCM + 1。

构造思路出来了,但是还有一个很严重的问题:直接这样构造是会超出题目所给的10^15的范围。
【于是考场上我就想到这里而已……QAQ】

我们给每一条黑格斜线定一个素数权值,给每一个黑格定权值为穿过它两条斜线——自左上至右下一条,自右上至左下一条——的权值之积。这样,每个白格的元素就为四个素数之积+1。

不难发现自右上至左下的斜线恰有N条,我们令这N条斜线的权值为第1~N个质数,令自左上至右下的斜线的权值为N+1开始的质数。

则白格的最大值为 第 499 个 质 数 ∗ 第 500 个 质 数 ∗ 第 999 个 质 数 ∗ 第 1000 个 质 数 − 1 = 3559 ∗ 3571 ∗ 7907 ∗ 7919 − 1 = 795 , 7926 , 4323 , 2737 − 1 ≈ 7 ∗ 1 0 1 4 − 1 第499个质数 * 第500个质数 * 第999个质数 * 第1000个质数 - 1= 3559 * 3571 * 7907 * 7919 - 1= 795,7926,4323,2737 - 1≈ 7*10^14 - 1 499

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值