题目链接  Divisibility

题意 给定$n$和$k$,构造一个集合$\left\{1, 2, 3, ..., n \right\}$的子集,使得在这个集合中恰好有$k$对正整数$(x, y)$, $x < y$

   满足$x$是$y$的约数。

选定$1$和$2$,

首先把满足 $x > [\frac{n}{2}]\ $的质数$x$留出来,

然后把满足 $ [\frac{n}{3}]\  < x <=  [\frac{n}{2}]\ $的质数,以及他们的两倍留出来,
 
留出来的这些数先不选,从$3$开始一个个开始选。
 
接近$k$的时候开始选刚刚留出来的那些数,
 
先选满足 $x > [\frac{n}{2}]\ $的质数,满足题意的正整数对数加$1$,
 
再选满足 $ [\frac{n}{3}]\  < x <=  [\frac{n}{2}]\ $的质数的两倍,满足题意的正整数对数加$2$,
 
最后选满足 $ [\frac{n}{3}]\  < x <=  [\frac{n}{2}]\ $的质数,满足题意的正整数对数加$2$。
 
选完这些如果还是到不了$k$,那么无解。
 
本来想着$n$小的时候直接特判($O(2^{n})$暴力)的,但是把特判去掉交上去居然也通过了……
 
#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b)	for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i) const int N = 3e5 + 10; int a[N], b[N], c[N], f[N];
int n, k, num1 = 0, num2 = 0;
vector <int> c1, c21, c22;
vector <int> ans; void print(){
puts("Yes");
sort(ans.begin(), ans.end());
int sz = ans.size(); printf("%d\n", sz);
int fg = 0;
for (auto u : ans){
if (!fg) fg = 1;
else putchar(32);
printf("%d", u);
} putchar(10);
exit(0);
} void calc(){
int x1 = c1.size(), x21 = c21.size(), x22 = c22.size();
if (k & 1){
if (num1 == 0){ puts("No"); exit(0);} while (k >= 3 && x22 > 0){
k -= 2;
--x22;
ans.push_back(c22[x22]);
c22.pop_back();
} while (k >= 3 && x21 > 0){
k -= 2;
--x21;
ans.push_back(c21[x21]);
c21.pop_back();
} while (k > 0 && x1 > 0){
k--;
--x1;
ans.push_back(c1[x1]);
c1.pop_back();
} if (k > 0){ puts("No"); exit(0); }
print();
} else{
while (k >= 2 && x22 > 0){
k -= 2;
--x22;
ans.push_back(c22[x22]);
c22.pop_back();
} while (k >= 2 && x21 > 0){
k -= 2;
--x21;
ans.push_back(c21[x21]);
c21.pop_back();
} while (k > 0 && x1 > 0){
k--;
--x1;
ans.push_back(c1[x1]);
c1.pop_back();
} if (k > 0){ puts("No"); exit(0);}
print();
}
} int main(){ scanf("%d%d", &n, &k); rep(i, 2, 3e5 + 1){
if (!b[i]){
for (int j = i + i; j <= 3e5 + 1; j += i)
b[j] = 1;
}
} rep(i, 1, 3e5 + 1){
for (int j = i; j <= 3e5 + 1; j += i) ++f[j];
--f[i];
} c[1] = 1, c[2] = 1;
rep(i, n / 2 + 1, n) if (!b[i]){
if (i <= 2) continue;
c[i] = 1;
c1.push_back(i);
++num1;
} rep(i, n / 3 + 1, n / 2) if (!b[i]){
if (i <= 2) continue;
c[i] = 1;
c[i << 1] = 1;
c21.push_back(i);
c22.push_back(i * 2);
++num2;
} --k;
ans.push_back(1);
ans.push_back(2);
if (k == 0) print(); rep(i, 3, n){
if (c[i]) continue;
if (k >= f[i]){
k -= f[i];
ans.push_back(i);
}
else calc();
} calc();
return 0;
}

  

 

Codeforces 922F Divisibility (构造 + 数论)的更多相关文章

  1. Codeforces 922F Divisibility 构造

    Divisibility 我们考虑删数字 首先我们可以发现有一类数很特殊就是大于 n / 2的素数, 因为这些素数的贡献只有1, 并且在n大的时候, 这些素数的个数不是很少, 我们可以最后用这些数去调 ...

  2. CF45G Prime Problem 构造+数论

    正解:构造+数论 解题报告: 传送门! maya这题好神仙啊我jio得,,,反正我当初听的时候是没有太懂的,,, 首先这题你要知道一些必要的数学姿势 比如哥德巴赫猜想巴拉巴拉的 然后直接讲题趴QAQ ...

  3. codeforces 1041 e 构造

    Codeforces 1041 E 构造题. 给出一种操作,对于一棵树,去掉它的一条边.那么这颗树被分成两个部分,两个部分的分别的最大值就是这次操作的答案. 现在给出一棵树所有操作的结果,问能不能构造 ...

  4. [CodeForces - 1225C]p-binary 【数论】【二进制】

    [CodeForces - 1225C]p-binary [数论][二进制] 标签: 题解 codeforces题解 数论 题目描述 Time limit 2000 ms Memory limit 5 ...

  5. codeforces 487C C. Prefix Product Sequence(构造+数论)

    题目链接: C. Prefix Product Sequence time limit per test 1 second memory limit per test 256 megabytes in ...

  6. Codeforces 550C —— Divisibility by Eight——————【枚举 || dp】

     Divisibility by Eight time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  7. Codeforces - 474D - Flowers - 构造 - 简单dp

    https://codeforces.com/problemset/problem/474/D 这道题挺好的,思路是这样. 我们要找一个01串,其中0的段要被划分为若干个连续k的0. 我们设想一个长度 ...

  8. [math] Codeforces 597A Divisibility

    题目:http://codeforces.com/problemset/problem/597/A Divisibility time limit per test 1 second memory l ...

  9. Codeforces Global Round 8 B. Codeforces Subsequences(构造)

    题目链接:https://codeforces.com/contest/1368/problem/B 题意 构造最短的至少含有 $k$ 个 $codeforces$ 子序列的字符串. 题解 如下表: ...

随机推荐

  1. [BZOJ2331]地板(插头DP)

    Description lxhgww的小名叫"小L",这是因为他总是很喜欢L型的东西.小L家的客厅是一个的矩形,现在他想用L型的地板来铺满整个客厅,客厅里有些位置有柱子,不能铺地板 ...

  2. Linux命令之----tree

    命令简介 tree命令的中文意思为“树”,功能是以树形结构列出指定目录下的所有内容,包括所有文件.子目录及子目录里的目录和文件. 命令格式 tree [option] [directory]tree ...

  3. POJ 2771 Guardian of Decency (二分图最大点独立集)

    Guardian of Decency Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 6133   Accepted: 25 ...

  4. loj2173 「FJOI2016」建筑师

    ref 真是道组合数学神题啊--第一次见第一类斯特林数-- #include <iostream> #include <cstdio> using namespace std; ...

  5. Python框架之Django学习笔记(四)

    第一个基于Django的页面:Hello World 正如我们的第一个目标,创建一个网页,用来输出这个著名的示例信息:Hello world. 第一个视图 Hello world视图非常简单. 这些是 ...

  6. c++ primer 6 练习题 (非复习题)

    第7章 7.13-1调和平均数 //7.13-1 excise.cpp 调和平均数 #include <iostream> double calculate(double a,double ...

  7. LeetCode668马在棋盘上的概率

    已知一个 NxN 的国际象棋棋盘,棋盘的行号和列号都是从 0 开始.即最左上角的格子记为 (0, 0),最右下角的记为 (N-1, N-1). 现有一个 “马”(也译作 “骑士”)位于 (r, c)  ...

  8. Leetcode 655.输出二叉树

    输出二叉树 在一个 m*n 的二维字符串数组中输出二叉树,并遵守以下规则: 行数 m 应当等于给定二叉树的高度. 列数 n 应当总是奇数. 根节点的值(以字符串格式给出)应当放在可放置的第一行正中间. ...

  9. 菜鸟之路——机器学习之线性回归个人理解及Python实现

    这一节很简单,都是高中讲过的东西 简单线性回归:y=b0+b1x+ε.b1=(Σ(xi-x–)(yi-y–))/Σ(xi-x–)ˆ2       b0=y--b1x-    其中ε取 为均值为0的正态 ...

  10. 【转】UGUI之用脚本动态的改变Button的背景图片 和 颜色

    http://blog.csdn.net/u014771617/article/details/45102701 public Button button;void Start(){ColorBloc ...