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 ;
}

暴力代码

Codeforces Round #272 (Div. 2) D.Dreamoon and Sets 找规律的更多相关文章

  1. Codeforces Round #272 (Div. 2) D. Dreamoon and Sets 构造

    D. Dreamoon and Sets 题目连接: http://www.codeforces.com/contest/476/problem/D Description Dreamoon like ...

  2. Codeforces Round #272 (Div. 2) D. Dreamoon and Sets (思维 数学 规律)

    题目链接 题意: 1-m中,四个数凑成一组,满足任意2个数的gcd=k,求一个最小的m使得凑成n组解.并输出 分析: 直接粘一下两个很有意思的分析.. 分析1: 那我们就弄成每组数字都互质,然后全体乘 ...

  3. Codeforces Round #272 (Div. 2) E. Dreamoon and Strings 动态规划

    E. Dreamoon and Strings 题目连接: http://www.codeforces.com/contest/476/problem/E Description Dreamoon h ...

  4. Codeforces Round #272 (Div. 2) B. Dreamoon and WiFi dp

    B. Dreamoon and WiFi 题目连接: http://www.codeforces.com/contest/476/problem/B Description Dreamoon is s ...

  5. Codeforces Round #272 (Div. 2) A. Dreamoon and Stairs 水题

    A. Dreamoon and Stairs 题目连接: http://www.codeforces.com/contest/476/problem/A Description Dreamoon wa ...

  6. Codeforces Round #272 (Div. 2) E. Dreamoon and Strings dp

    题目链接: http://www.codeforces.com/contest/476/problem/E E. Dreamoon and Strings time limit per test 1 ...

  7. Codeforces Round #272 (Div. 1) A. Dreamoon and Sums(数论)

    题目链接 Dreamoon loves summing up something for no reason. One day he obtains two integers a and b occa ...

  8. Codeforces Round #272 (Div. 2)-C. Dreamoon and Sums

    http://codeforces.com/contest/476/problem/C C. Dreamoon and Sums time limit per test 1.5 seconds mem ...

  9. Codeforces Round #272 (Div. 2)-B. Dreamoon and WiFi

    http://codeforces.com/contest/476/problem/B B. Dreamoon and WiFi time limit per test 1 second memory ...

随机推荐

  1. 【alert(1) to win】不完全攻略

    alert(1) to win 一个练习XSS的平台,地址:https://alf.nu/alert1 Warmup 给出了一段JavaScript代码 function escape(s) { re ...

  2. Introduction of Version Control/Git, SVN

    什么是版本控制? 你可以把一个版本控制系统(缩写VCS)理解为一个“数据库”,在需要的时候,它可以帮你完整地保存一个项目的快照.当你需要查看一个之前的快照(称之为“版本”)时,版本控制系统可以显示出当 ...

  3. Pytorch 加载保存模型【直播】2019 年县域农业大脑AI挑战赛---(三)保存结果

    在模型训练结束,结束后,通常是一个分割模型,输入 1024x1024 输出 4x1024x1024. 一种方法就是将整个图切块,然后每张预测,但是有个不好处就是可能在边界处断续. 由于这种切块再预测很 ...

  4. 浅谈FFC

    FFC(Flexible Formatting Context) CSS3引入了一种新的布局模型——flex布局(之前有文章介绍过).flex是flexible box的缩写,一般称之为弹性盒模型.和 ...

  5. 搭建Cookie池

    很多时候我们在对网站进行数据抓取的时候,可以抓取一部分页面或者接口,这部分可能没有设置登录限制.但是如果要抓取大规模数据的时候,没有登录进行爬取会出现一些弊端.对于一些设置登录限制的页面,无法爬取对于 ...

  6. 易忘小技巧--yum

    写在前面:日常维护系统中,如果不是天天接触,难免会忘记一些实用小技巧,本文记录自己使用的小技巧,有新发现时,会长期更新 安装epel和dnf源 # 安装epel yum -y install epel ...

  7. jQuery元素节点的插入

    jquery插入节点的的方法,总的来说有8种,但是只要学会了其中的两个就能理解全部了, 这里我们学习append()和appendTo()两个方法: append()方法是向元素的内部追加内容: &l ...

  8. C++关键字:explicit

    #include "pch.h" #include <iostream> using namespace std; class BaseClass { public: ...

  9. 39页第3题 求x的n次幂

    /*计算x的n次幂*/ #include<stdio.h> main(void) { int i,n; double x,y; printf("Enter x:");/ ...

  10. JavaScript 面向对象的编程(二) 类的封装

    类的定义 方式一 var Book = function(id, name, price){ //私有属性,外部不能直接访问 var num = 1; //私有方法, function checkId ...