codeforces 476D Dreamoon and Sets(数学)
来源:互联网 发布:时代互联域名证书 编辑:程序博客网 时间:2024/06/16 16:53
题目链接
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 si, sjfrom 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.
The single line of the input contains two space separated integers n, k (1 ≤ n ≤ 10 000, 1 ≤ k ≤ 100).
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.
1 1
51 2 3 5
2 2
222 4 6 2214 18 10 16
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)。
代码如下:
#include<stdio.h>#include<algorithm>#include<queue>#include<stack>#include<map>#include<set>#include<vector>#include<iostream>#include<string.h>#include<string>#include<math.h>#include<stdlib.h>#define inff 0x3fffffff#define eps 1e-8#define nn 210000#define mod 1000000007typedef long long LL;const LL inf64=LL(inff)*inff;using namespace std;LL a[11000][6];int n,k;void init(){ int i,j; LL id=1; for(i=1;i<=10000;i++) { for(j=1;j<=3;j++) { a[i][j]=id++; } a[i][4]=++id; id+=2; }}int main(){ int i,j; init(); while(scanf("%d%d",&n,&k)!=EOF) { printf("%I64d\n",a[n][4]*(LL)k); for(i=1;i<=n;i++) { for(j=1;j<=4;j++) { printf("%I64d%c",a[i][j]*k,j==4?'\n':' '); } } } return 0;}
- codeforces 476D Dreamoon and Sets(数学)
- Codeforces 476D. Dreamoon and Sets
- 【构造】 Codeforces 476D Dreamoon and Sets
- Codeforces 476D Dreamoon and Sets 规律+构造
- D. Dreamoon and Sets(Codeforces Round #272)
- D. Dreamoon and Sets(Codeforces Round #272)
- Dreamoon and Sets CodeForces
- Dreamoon and Sets CodeForces
- Dreamoon and Sets CodeForces
- 【CODEFORCES】 B. Dreamoon and Sets
- Codeforces Round #272 (Div. 2) D Dreamoon and Sets(构造)
- 【codeforces】477B Dreamoon and Sets 构造
- CodeForces 476B Dreamoon and WiFi 数学 概率 DP
- CodeForces 477B-E - Dreamoon and Sets-构造规律题
- 【构造】【cf477B】Dreamoon and Sets
- 477 B. Dreamoon and Sets
- CF 476C Dreamoon and Sums[数学]
- codeforces-#476B. Dreamoon and WiFi(dfs、二进制枚举)
- Cocos2d-X 学习笔记5 Win7 + vs2012 + cocos2d-x2.2 配置开发环境
- Android: 触屏fling/scroll/drag的区别及其详细过程
- cocos2d-x Tiled map editor 创建地图导入项目
- Mysql or Mongodb LBS快速实现方案
- 编码规范系列(一):Eclipse Code Templates设置
- codeforces 476D Dreamoon and Sets(数学)
- 论证是一门学问 如何让你的观点有说服力-7 概括 Generalizations
- 线性回归与梯度下降法
- 修改设备驱动加载顺序
- activiti 学习由浅入深
- ZOJ Problem Set - 3829Known Notation(贪心)
- 经典面试智力题200+题和解答
- android 动态设置TextView值,例:金额增加
- Windows平台执行sql文件Mysql表名变小写的解决过程