All Your Paths are Different Lengths

题目链接https://atcoder.jp/contests/arc102/tasks/arc102_b


题解

构造题有技巧,如果题目中要求了20和60,那就从这里入手好了。

发现没法入手因为太平凡了....

但是,他要求了每种值只出现了一次,容易联想到弄出来$log$个$2$的幂次。

诶?想到这里发现,$20$好像差不多就是$log$大小。

我们就放$20$个点,第$i$个点指向第$i + 1$个点两条边,$2^{i - 1}$和$0$。

发现不能放20个因为有可能爆,那就放恰好$log$个就好。

接着处理剩下的部分。

其实就是想数位$dp$一样,处理$L$的每个$1$,把当前的$1$变成$0$然后加上前面的所有$1$,看看后面还能有多少连上就好。

诶呀说不明白,看代码吧。

代码

#include <bits/stdc++.h>

#define N 1000010 

using namespace std;

int Log[N];

char *p1, *p2, buf[100000];

#define nc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1 ++ )

int rd() {
int x = 0, f = 1;
char c = nc();
while (c < 48) {
if (c == '-')
f = -1;
c = nc();
}
while (c > 47) {
x = (((x << 2) + x) << 1) + (c ^ 48), c = nc();
}
return x * f;
} int bin[21]; int a[20]; struct Node {
int x, y, z;
}e[100]; int main() {
int n = rd();
n -- ;
bin[0] = 1;
for (int i = 1; i <= 20; i ++ ) {
bin[i] = bin[i - 1] << 1;
}
int k = 0;
int m = n;
bool flag = false;
while (m) {
k ++ ;
if (m % 2 == 0) {
flag = true;
}
m /= 2;
}
int tot = 0;
if (!flag) {
cout << k + 1 << ' ' ;
for (int i = 1; i <= k; i ++ ) {
// i -> i + 1
tot ++ ;
e[tot].x = i, e[tot].y = i + 1, e[tot].z = bin[i - 1];
tot ++ ;
e[tot].x = i, e[tot].y = i + 1, e[tot].z = 0;
}
cout << tot << endl ;
for (int i = 1; i <= tot; i ++ ) {
printf("%d %d %d\n", e[i].x, e[i].y, e[i].z);
}
return 0;
}
// puts("Fuck");
cout << k << ' ' ;
for (int i = 1; i < k; i ++ ) {
tot ++ ;
e[tot].x = i, e[tot].y = i + 1, e[tot].z = bin[k - i - 1];
tot ++ ;
e[tot].x = i, e[tot].y = i + 1, e[tot].z = 0;
}
int cnt = 0;
for (int i = 0; i <= 20; i ++ ) {
if (n & bin[i]) {
a[ ++ cnt] = i;
}
}
int pre = bin[a[cnt]];
for (int i = cnt - 1; i; i -- ) {
tot ++ ;
e[tot].x = 1, e[tot].y = k - a[i], e[tot].z = pre;
pre += bin[a[i]];
}
tot ++ ;
e[tot].x = 1, e[tot].y = k, e[tot].z = n;
cout << tot << endl ;
for (int i = 1; i <= tot; i ++ ) {
printf("%d %d %d\n", e[i].x, e[i].y, e[i].z);
}
return 0;
}

小结:Atcoder全是构造世人皆知.....这个因为都只出现一次,很容易想到二进制。然后数位dp就好了。

[Arc102B]All Your Paths are Different Lengths_构造_二进制拆分的更多相关文章

  1. 量子杨-Baxter方程新解系的一般量子偶构造_爱学术 https://www.ixueshu.com/document/f3385115a33571aa318947a18e7f9386.html

    量子杨-Baxter方程新解系的一般量子偶构造_爱学术 https://www.ixueshu.com/document/f3385115a33571aa318947a18e7f9386.html

  2. AtCoder Regular Contest 102 (ARC102) D All Your Paths are Different Lengths 构造

    原文链接https://www.cnblogs.com/zhouzhendong/p/ARC102D.html 题目传送门 - ARC102D 题意 给定 $L$,请你构造一个节点个数为 $n$ ,边 ...

  3. 面向对象程序设计-C++_课时26拷贝构造Ⅰ_课时27拷贝构造Ⅱ

    一旦写了一个类,给它3个函数: 1default construtor 2virtual destructor 3copy constructor Constructions vs. assignme ...

  4. Atcoder AGC031C Differ By 1 Bit (构造、二进制)

    哎呀这个C怎么比B还水....(我现在大概也就会做点这种水题了吧) 题目链接 https://atcoder.jp/contests/agc031/tasks/agc031_c 题目大意 符号约定: ...

  5. CF1041E Tree Reconstruction_构造_思维题

    不难发现,每次询问结果一定是 (i,n)(i,n)(i,n), 而 iii 出现的次数恰好是 iii 到 i′i'i′ 的距离(i′i'i′ 是第一个不与 iii 相等的数).我们可以将这颗树构造成一 ...

  6. CF1041F Ray in the tube构造_思维

    不难发现起点必定是一个点. 每次间隔的距离一定是 2k2^k2k,关键就是要判断两点是否在同一跳跃距离上可被同时覆盖. 我们可以对上边进行 x1≡x_{1}\equivx1​≡ x2mod(2∗dx) ...

  7. CF1012B Chemical table 构造_思维_并查集

    我们可以将横坐标和纵坐标看成是点.发现这些点之间是有传递性的. 题中说明,如果有矩阵中三个顶点被选,则底角的点也会被覆盖,发现这些点之间是有传递性的.那么我们最终达到的目的就是使整个图中只有 111 ...

  8. CF #487 (Div. 2) D. A Shade of Moonlight 构造_数形结合

    题意: 给 nnn个长度为 lll 且互不相交的开区间 (xi,xi+l)(x_{i}, x_{i}+l)(xi​,xi​+l) ,每个区间有一个移动速度 vvv,v∈1,−1v∈1,-1v∈1,−1 ...

  9. Codeforces Round #493 (Div. 2) C. Convert to Ones 乱搞_构造_好题

    题意: 给你一个长度为 nnn 的 010101串 ,你有两种操作: 1.将一个子串翻转,花费 XXX 2.将一个子串中的0变成1,1变成0,花费 YYY 求你将这个01串变成全是1的串的最少花费. ...

随机推荐

  1. Educational Codeforces Round 67 (Rated for Div. 2) B题【前缀+二分】【补题ING系列】

    题意:给出一个字符串s, 可以从左往右拿走s的字符, 至少要到s的第几个位置才能拼成t 思路:用二维数组记录前缀,然后二分即可. #include<bits/stdc++.h> using ...

  2. 014_编写批量修改扩展名脚本,如批量将 txt 文件修改为 doc 文件

    #!/bin/bash#执行脚本时,需要给脚本添加位置参数 带参for i in "ls *.$1"do     #去尾     mv $i ${i%.*}.$2done

  3. Shell 01 shell概述

    1.查看shells:/etc/shells /bin/sh /bin/bash /sbin/nologin /usr/bin/sh /usr/bin/bash /usr/sbin/nologin / ...

  4. 通过无线网络使用ADB ( Connect to android with ADB over TCP )

    之前用USB数据线连接电脑和android手机,用adb操控手机. 后来电脑前面板的USB全坏了,键盘.鼠标.USBKkey.打印机都需USB,  少一个USB口,只得对不常用设备进行插拔切换. 于是 ...

  5. ckeditor粘贴word文档图片的思路

    由于工作需要必须将word文档内容粘贴到编辑器中使用 但发现word中的图片粘贴后变成了file:///xxxx.jpg这种内容,如果上传到服务器后其他人也访问不了,网上找了很多编辑器发现没有一个能直 ...

  6. java+web+多级文件上传

    文件夹数据库处理逻辑 publicclass DbFolder { JSONObject root; public DbFolder() { this.root = new JSONObject(); ...

  7. [Luogu] 借教室

    https://www.luogu.org/problemnew/show/P1083 二分第i天不满足 前缀和 + 差分判断 #include <iostream> #include & ...

  8. hive安装运行hive报错通解

    参考博文:https://blog.csdn.net/lsxy117/article/details/47703155 大部分问题还是hadoop的配置文件的问题: 修改配置文件hadoop/conf ...

  9. jvm 线程状态

    NEW: Just starting up, i.e., in process of being initialized.NEW_TRANS: Corresponding transition sta ...

  10. Uncaught TypeError: l.push is not a function

    layui.use([ 'jquery', 'layer', 'element' ], function() {} 而不是 layui.use( 'jquery', 'layer', 'element ...