当前位置:   article > 正文

codeforces 476D Dreamoon and Sets(数学)_codeforces dreamoon

codeforces dreamoon

题目链接

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 sisjfrom 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.

Sample test(s)
input
1 1
output
5
1 2 3 5
input
2 2
output
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} isn't a valid set of rank 1 since 


题意:给一个N,K。要找N个集合,每个集合有四个不同的数字,每个数字只能在一个集合。要让每个集合满足以下条件:

集合中任意两个数字的gcd(最大公约数)为K。

现在要让N个集合中最大的数字最小,并输出方案。

当K==1的时候,我们要让集合中的每个数两两互质。

当K!=1的时候,我们将K==1的时候的方案的每个数乘以K,就是答案。

所以我们只要解决K==1的情况即可。换句话说,问题就是找n个集合,每个集合中数两两互质,并且一个数只属于一个集合。要让N个集合中最大的数最小。

假设我们已知前N个集合的最大的数为x,那么对于第N+1个集合来说,我们就是要从x+1开始找,找四个互质的数,并且最大的数最小。

假设x+1为奇数,我们发现:x+1,x+2,x+3,x+5,这四个数一定能构成一个集合。

假设x+1为偶数,我们发现若x+1必选,最优情况下4个数为:x+1,x+2,x+4, x+6。

显然当x+1为偶数的时候,不选x+1,选x+2一定更优。当x+1为奇数的时候,选x+1显然最优。

初始的时候x=0。我们可以预处理出n个集合中的数。复杂度O(n)。

代码如下:

  1. #include<stdio.h>
  2. #include<algorithm>
  3. #include<queue>
  4. #include<stack>
  5. #include<map>
  6. #include<set>
  7. #include<vector>
  8. #include<iostream>
  9. #include<string.h>
  10. #include<string>
  11. #include<math.h>
  12. #include<stdlib.h>
  13. #define inff 0x3fffffff
  14. #define eps 1e-8
  15. #define nn 210000
  16. #define mod 1000000007
  17. typedef long long LL;
  18. const LL inf64=LL(inff)*inff;
  19. using namespace std;
  20. LL a[11000][6];
  21. int n,k;
  22. void init()
  23. {
  24. int i,j;
  25. LL id=1;
  26. for(i=1;i<=10000;i++)
  27. {
  28. for(j=1;j<=3;j++)
  29. {
  30. a[i][j]=id++;
  31. }
  32. a[i][4]=++id;
  33. id+=2;
  34. }
  35. }
  36. int main()
  37. {
  38. int i,j;
  39. init();
  40. while(scanf("%d%d",&n,&k)!=EOF)
  41. {
  42. printf("%I64d\n",a[n][4]*(LL)k);
  43. for(i=1;i<=n;i++)
  44. {
  45. for(j=1;j<=4;j++)
  46. {
  47. printf("%I64d%c",a[i][j]*k,j==4?'\n':' ');
  48. }
  49. }
  50. }
  51. return 0;
  52. }



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/75464
推荐阅读
相关标签
  

闽ICP备14008679号