CodeForces

来源:互联网 发布:mac用什么五笔 编辑:程序博客网 时间:2024/06/15 19:24
D. Dreamoon and Sets
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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

Let S be a set of exactly four distinct integers greater than 0. Define S to be of rank k if and only if for all pairs of distinct elements sisj from S.

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

Input

The single line of the input contains two space separated integers nk (1 ≤ n ≤ 10 000, 1 ≤ k ≤ 100).

Output

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

On each of the next n lines print four space separated integers representing the 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, print any one of them.

Examples
input
1 1
output
51 2 3 5
input
2 2
output
222 4 6 2214 18 10 16
Note

For the first example it's easy to see that set {1, 2, 3, 4} isn't a valid set of rank 1 since .


题意:输入 n,k 然后让你在 1 到 m (题目要求m尽可能的小)中找出n个集合,每个集合有四个数 且这四个数的gcd为k(所有数字不重复)。最终输出m,和n个集合(集合的顺序和集合中的整数的顺序都不重要) 。

思路:首先想到公式  若gcd(a,b)==1则,gcd(a*k,b*k)==k; 然后此时可以直接去寻找 k==1的情况。可以找到规律 1,2,3,5  7,8,9,11  13,14,15,17  .... 

代码:

 

#include <cstdio>#include <iostream>#define ll long longusing namespace std;ll ans[10005][4];void init(){    ll j=1;    for(int i=0;i<=10000;i++)    {        ans[i][0]=j++;        ans[i][1]=j++;        ans[i][2]=j++;        j++;        ans[i][3]=j++;        j++;    }}int main(){    int n,k;    init();    while(~scanf("%d %d",&n,&k))    {        printf("%lld\n",ans[n-1][3]*k);        for(int i=0;i<n;i++)        {            printf("%lld %lld %lld %lld\n",ans[i][0]*k,ans[i][1]*k,ans[i][2]*k,ans[i][3]*k);        }    }    return 0;}