D. Dreamoon and Sets
 

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
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个集合均为4个元素,且每个集合内任意两两,元素的最大公约数为k,集合不允许有交集,当n*4个元素最大值最小时,输出所有n个集合,

题解:我是暴力水过去的,正解是:

两两元素之间是互质的,然后为了满足元素最大值最小,

在除掉k后,任意集合内肯定是由3个奇数,1个偶数组成,

因为若少一个奇数,就会有至少一对数不互质,

若多一个奇数,元素最大值就会变大。

所以,从1开始构建所有元素集合即可。

#include<iostream>
#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
int n,k;
cin>>n>>k;
cout<<(*n-)*k<<endl;
while(n--)
{
cout<<(*n+)*k<<" "<<(*n+)*k<<" "<<(*n+)*k<<" "<<(*n+)*k<<endl;
}
}

正解代码

///
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,127,sizeof(a));
#define TS printf("111111\n");
#define FOR(i,a,b) for( int i=a;i<=b;i++)
#define FORJ(i,a,b) for(int i=a;i>=b;i--)
#define READ(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define inf 100000000
inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
//****************************************
///****************************************************************
/// Miller_Rabin 算法进行素数测试
///速度快,而且可以判断 <2^63的数
//****************************************************************
const int S=;///随机算法判定次数,S越大,判错概率越小 ///计算 (a*b)%c. a,b都是long long的数,直接相乘可能溢出的
/// a,b,c <2^63
long long mult_mod(long long a,long long b,long long c)
{
a%=c;
b%=c;
long long ret=;
while(b)
{
if(b&){ret+=a;ret%=c;}
a<<=;
if(a>=c)a%=c;
b>>=;
}
return ret;
} ///计算 x^n %c
long long pow_mod(long long x,long long n,long long mod)//x^n%c
{
if(n==)return x%mod;
x%=mod;
long long tmp=x;
long long ret=;
while(n)
{
if(n&) ret=mult_mod(ret,tmp,mod);
tmp=mult_mod(tmp,tmp,mod);
n>>=;
}
return ret;
} ///以a为基,n-1=x*2^t a^(n-1)=1(mod n) 验证n是不是合数
///一定是合数返回true,不一定返回false
bool check(long long a,long long n,long long x,long long t)
{
long long ret=pow_mod(a,x,n);
long long last=ret;
for(int i=;i<=t;i++)
{
ret=mult_mod(ret,ret,n);
if(ret==&&last!=&&last!=n-) return true;//合数
last=ret;
}
if(ret!=) return true;
return false;
} /// Miller_Rabin()算法素数判定
///是素数返回true.(可能是伪素数,但概率极小)
///合数返回false; bool Miller_Rabin(long long n)
{
if(n<)return false;
if(n==)return true;
if((n&)==) return false;//偶数
long long x=n-;
long long t=;
while((x&)==){x>>=;t++;}
for(int i=;i<S;i++)
{
long long a=rand()%(n-)+;///rand()需要stdlib.h头文件
if(check(a,n,x,t))
return false;//合数
}
return true;
} #define maxn 100000+5
int a[maxn],n,m;
vector<int >G;
int gcd(int M,int N )
{
int Rem;
while( N > )
{
Rem = M % N;
M = N;
N = Rem;
}
return M;
}
int main(){
n=read(),m=read();
int ans[maxn];
int k=;
int tmp=;
G.clear();
for(int i=;i<=n;i++){
while(G.size()!=){
bool flag=;
for(int j=;j<G.size();j++){
if(gcd(G[j],m*tmp)!=m)flag=;
}
if(flag) G.push_back(tmp*m);
tmp++;
}
for(int j=;j<G.size();j++)
a[++k]=G[j];
G.clear();
if(!Miller_Rabin(tmp))tmp++;
}
cout<<a[k]<<endl;
for(int i=;i<=k;i+=){
cout<<a[i]<<" "<<a[i+]<<" "<<a[i+]<<" "<<a[i+]<<endl;
} return ;
}

暴力代码

最新文章

  1. java,js,jstl,EL的简单交互
  2. php curl 库使用
  3. 第三个Sprint冲刺第三天
  4. css基本知识框架(转)
  5. a read only variable
  6. 【Open Search产品评测】-- 淘点点:基于OpenSearch,轻松实现一整套O2O类搜索解决方案
  7. Tomcat配置HTTPS方式
  8. 带你初识Angular中MVC模型
  9. Vijos_1218_数字游戏_(划分型动态规划+环状动态规划)
  10. phpmyadmin 自动登录的办法
  11. 视频流PS,PS封装H264
  12. Hibernate学习(九)———— 二级缓存和事务级别详讲
  13. Ubuntu Server 18.04 网络设置不生效的解决
  14. wps表格开发C#
  15. about use Vue of methods
  16. [No0000116]SQLServer启用sa账户
  17. [CodeForces - 614C] C - Peter and Snow Blower
  18. HTML转义字符 Unicode和CSS伪类介绍
  19. PHP字符串函数运用小案例
  20. 43-python-自己的词典

热门文章

  1. CAD从二进流加载数据(com接口VB语言)
  2. Redis系列(一)--基础API
  3. java虚拟机(六)--垃圾收集器和内存分配策略
  4. C语言编辑编译及集成开发环境
  5. jenkins自动部署测试环境
  6. 每日命令:(8)cp
  7. Django-F和Q函数作用与使用
  8. list tuple dict (列表,元祖,字典间的相互转换)
  9. SpringBoot yaml的配置及使用
  10. 常量Constant