Codeforces Round #272 (Div. 2) D.Dreamoon and Sets 找规律

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

51 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<<(6*n-1)*k<<endl;
    while(n--)
    {
        cout<<(6*n+1)*k<<" "<<(6*n+2)*k<<" "<<(6*n+3)*k<<" "<<(6*n+5)*k<<endl;
    }
}

正解代码

///1085422276
#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=0,f=1;
    char ch=getchar();
    while(ch<‘0‘||ch>‘9‘)
    {
        if(ch==‘-‘)f=-1;
        ch=getchar();
    }
    while(ch>=‘0‘&&ch<=‘9‘)
    {
        x=x*10+ch-‘0‘;
        ch=getchar();
    }
    return x*f;
}
//****************************************
///****************************************************************
/// Miller_Rabin 算法进行素数测试
///速度快,而且可以判断 <2^63的数
//****************************************************************
const int S=20;///随机算法判定次数,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=0;
    while(b)
    {
        if(b&1){ret+=a;ret%=c;}
        a<<=1;
        if(a>=c)a%=c;
        b>>=1;
    }
    return ret;
}

///计算  x^n %c
long long pow_mod(long long x,long long n,long long mod)//x^n%c
{
    if(n==1)return x%mod;
    x%=mod;
    long long tmp=x;
    long long ret=1;
    while(n)
    {
        if(n&1) ret=mult_mod(ret,tmp,mod);
        tmp=mult_mod(tmp,tmp,mod);
        n>>=1;
    }
    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=1;i<=t;i++)
    {
        ret=mult_mod(ret,ret,n);
        if(ret==1&&last!=1&&last!=n-1) return true;//合数
        last=ret;
    }
    if(ret!=1) return true;
    return false;
}

/// Miller_Rabin()算法素数判定
///是素数返回true.(可能是伪素数,但概率极小)
///合数返回false;

bool Miller_Rabin(long long n)
{
    if(n<2)return false;
    if(n==2)return true;
    if((n&1)==0) return false;//偶数
    long long x=n-1;
    long long t=0;
    while((x&1)==0){x>>=1;t++;}
    for(int i=0;i<S;i++)
    {
        long long a=rand()%(n-1)+1;///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 > 0 )
    {
        Rem = M % N;
        M = N;
        N = Rem;
}
    return M;
}
int main(){
   n=read(),m=read();
    int ans[maxn];
     int k=0;
     int tmp=1;
     G.clear();
     for(int i=1;i<=n;i++){
            while(G.size()!=4){
                    bool flag=1;
                 for(int j=0;j<G.size();j++){
                if(gcd(G[j],m*tmp)!=m)flag=0;
                 }
                if(flag) G.push_back(tmp*m);
                tmp++;
            }
              for(int j=0;j<G.size();j++)
                   a[++k]=G[j];
                   G.clear();
          if(!Miller_Rabin(tmp))tmp++;
     }
     cout<<a[k]<<endl;
     for(int i=1;i<=k;i+=4){
         cout<<a[i]<<" "<<a[i+1]<<" "<<a[i+2]<<" "<<a[i+3]<<endl;
     }

  return 0;
}

暴力代码

时间: 2024-10-10 19:30:12

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

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

题目链接 题意: 1-m中,四个数凑成一组,满足任意2个数的gcd=k,求一个最小的m使得凑成n组解.并输出 分析: 直接粘一下两个很有意思的分析.. 分析1: 那我们就弄成每组数字都互质,然后全体乘以k不就行了么…… 然后看了看样例…… 这个该怎么说……我是觉得额这道题的output暴露了数据规律怎么破……我算是看出规律再证明的方式A的这道题 当时我看到22那个样例的时候……在想他干嘛要把22放这里……然后发现 2/4/6/10 14/16/18/22也是行的哇…… 化成乘以k之前的数据……

Codeforces Round #272 (Div. 2) B. Dreamoon and WiFi (超几何分布)

题目链接:Codeforces Round #273 (Div. 2) B. Dreamoon and WiFi 题意:"+"表示前进1个单位,"-"表示后退1个单位,问以0为起点经过S1,S2两个命令后达到的位置相同的概率. 思路:统计"+"和"-"的数量.如果S2中的"+"或者"-"比S1中的多,概率是0.其他条件下,形成的是超几何分布. AC代码: #include <std

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 occasionally. He wants to calculate the sum of all nice integers. Positive integer x is called nice if  and , where k is some integer number in range[1, a

Codeforces Round #272 (Div. 2)C. Dreamoon and Sums 数学推公式

C. Dreamoon and Sums Dreamoon loves summing up something for no reason. One day he obtains two integers a and b occasionally. He wants to calculate the sum of all nice integers. Positive integer x is called nice if  and , where k is some integer numb

Codeforces Round #272 (Div. 2) C. Dreamoon and Sums (数学 思维)

题目链接 这个题取模的时候挺坑的!!! 题意:div(x , b) / mod(x , b) = k( 1 <= k <= a).求x的和 分析: 我们知道mod(x % b)的取值范围为 1  - (b-1).那么我们可以从这一点入口来进行解题.. mod (x, b) = 1 时, x  =  b + 1, 2b + 1, 3b + 1..... a * b + 1. mod (x , b) = 2 时, x =  2b + 2, 4b + 2, 6b + 2, ..... 2a * b

Codeforces Round #272 (Div. 2) ABCDE

A. Dreamoon and Stairs 题解: 首先写出尽可能2多的步数,然后判断能否%m,不能就加上最小的数使其能%m就行了 代码: #include<bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair #define se second #define fs first #define LL long long #define CLR(x) memset(x,0,sizeof x)

Codeforces Round #272 (Div. 2) Dreamoon and WiFi 暴力

B. Dreamoon and WiFi Dreamoon is standing at the position 0 on a number line. Drazil is sending a list of commands through Wi-Fi to Dreamoon's smartphone and Dreamoon follows them. Each command is one of the following two types: Go 1 unit towards the

Codeforces Round #272 (Div. 2)

A. Dreamoon and Stairs 题意:给出n层楼梯,m,一次能够上1层或者2层楼梯,问在所有的上楼需要的步数中是否存在m的倍数 找出范围,即为最大步数为n(一次上一级),最小步数为n/2+n%2 在这个范围里找即可 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<algorithm> 6 using n

Codeforces Round #272 (Div. 2) A

题目: A. Dreamoon and Stairs time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Dreamoon wants to climb up a stair of n steps. He can climb 1 or 2 steps at each move. Dreamoon wants the number o