CF476D Dreamoon and Sets 题解 贪心

Dreamoon and Sets

传送门

Dreamoon likes to play with sets, integers and gcd ⁡ \gcd gcd . gcd ⁡ ( a , b ) \gcd(a,b) gcd(a,b) is defined as the largest positive integer that divides both a a a and b b b .

Let S S S be a set of exactly four distinct integers greater than 0 0 0 . Define S S S to be of rank k k k if and only if for all pairs of distinct elements s i s_{i} si , s j s_{j} sj from S S S , gcd ⁡ ( s i , s j ) = k \gcd(s_{i},s_{j})=k gcd(si,sj)=k .

Given k k k and n n n , Dreamoon wants to make up n n n sets of rank k k k using integers from 1 1 1 to m m m such that no integer is used in two different sets (of course you can leave some integers without use). Calculate the minimum m m m that makes it possible and print one possible solution.

Input

The single line of the input contains two space separated integers n n n , k k k ( 1 ≤ n ≤ 10000 , 1 ≤ k ≤ 100 1 \le n \le 10000,1 \le k \le 100 1n10000,1k100 ).

Output

On the first line print a single integer — the minimal possible m m m .

On each of the next n n n lines print four space separated integers representing the i i i -th set.

Neither the order of the sets nor the order of integers within a set is important. If there are multiple possible solutions with minimal m m m , print any one of them.

Examples

input #1

1 1

output #1

5
1 2 3 5

input #2

2 2

output #2

22
2 4 6 22
14 18 10 16

Note

For the first example it’s easy to see that set 1 , 2 , 3 , 4 {1,2,3,4} 1,2,3,4 isn’t a valid set of rank 1 since gcd ⁡ ( 2 , 4 ) = 2 ≠ 1 \gcd(2,4)=2\not=1 gcd(2,4)=2=1 .

题目翻译(Chinese)

输入 n , k n,k n,k,输出 n n n 个四元组。

满足以下条件

  • 对于任意一个四元组,其中任意两个不同数的最大公约数是 k k k
  • 每个数仅在所有的四元组内出现一次

要求 n n n 个四元组内最大的数最小。

第一行输出最大的数,下面 n n n 行输出四元组。

1 ≤ n ≤ 10000 1\le n\le 10000 1n10000

1 ≤ k ≤ 100 1\le k\le 100 1k100

输出四元组时,顺序和整数的顺序都不重要,你可以任意输出。

注明

以上来自 C o d e F o r c e s ,翻译:洛谷。 以上来自 CodeForces,翻译:洛谷。 以上来自CodeForces,翻译:洛谷。 T h e The The P r o b l e m Problem Problem i s is is f r o m from from C o d e F o r c e s CodeForces CodeForces, t r a n s l a t e translate translate b y by by L u o g u . Luogu. Luogu.

DeepL的翻译太抽象了,就用洛谷了。(The translation of DeepL is too abstract, so I’ll use Lougu.)

解题思路(Solution)

中文版(Chinese Version)

结论:若我们将集合中的每个数字除以 k k k,那么这个集合的秩()就是 1 1 1。因此,我们可以找到 n n n 个秩为 1 1 1 的集合,然后将每个数字乘以 k k k
至于如何找到 n n n 个秩为 1 1 1 的集合,我们可以使用 { 6 a + 1 , 6 a + 2 , 6 a + 3 , 6 a + 5 } \{6a + 1, 6a + 2, 6a + 3, 6a + 5\} {6a+1,6a+2,6a+3,6a+5} 作为有效的秩为 1 1 1 的集合,并取 a = 0 a = 0 a=0 n − 1 n - 1 n1 组成 n n n 个集合,因此 m = n × k × 6 − k m = n \times k \times 6 - k m=n×k×6k

证明 m m m 最小的方法是在每个集合中连续取三个奇数。如果我们取较少的奇数,那么一个集合中就会有一个以上的偶数,而显然该集合满足 gcd ⁡ ( { x 1 , x 2 , … , x n } ) m o d    2 = 0 \gcd(\{x_1,x_2,\dots,x_n\}) \mod 2=0 gcd({x1,x2,,xn})mod2=0

英文版(Engish Version)

Click here.

AC Code

#include<bits/stdc++.h>
using namespace std;
int n, k;
signed main() {
    cin >> n >> k, printf("%d\n", (n * 6 - 1)*k);
    for (register int i = 1, x; i <= n; ++i) x = i * 6 - 5, printf("%d %d %d %d\n", (x * k), ((x + 1) * k), ((x + 2) * k), ((x + 4) * k));
    return 0;
}

重要提醒(The Most Important Thing)

今天是妇女节,祝全球女士节日快乐。(It’s Women’s Day. Happy Women’s Day to women all over the world.)

  • 28
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值