思维题练习

news/2025/9/27 12:12:39/文章来源:https://www.cnblogs.com/TH911/p/19114942

本文选取题目源于此处,以及一些平时的好的思维题。

大体按照主观难度排序。

[FJCPC 2025] 构造大师贝贝

注意到 \(T\leq1000\),但是 \(n\leq10^{12}\)。那么从时间复杂度的角度考虑,应当为一个类似于 \(\mathcal O(T\log n)\) 的复杂度。

但是这道题的思维难度也就在这里了,将 \(n\) 变为完全平方数,每次加上约数 \(x\),很不好想到有什么带 log 的做法。

本题为 Special Judge,只需要输出任意一种构造方案。经过一些胡乱尝试猜测,想到也许可以想办法将 \(n\) 构造为形如 \(x^{2k}\) 的完全平方数。

那么考虑将其构造为 \(2^{2k}\),每次只需要加上 \(\operatorname{lowbit}(n)\) 即可,\(\operatorname{lowbit}(n)\) 定义同树状数组。

参考代码
//#include<bits/stdc++.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<iomanip>
#include<cstdio>
#include<string>
#include<vector>
#include<cmath>
#include<ctime>
#include<deque>
#include<queue>
#include<stack>
#include<list>
using namespace std;
typedef long long ll;
ll lowbit(ll n){return n&-n;
}
bool check(ll n){ll x=sqrt(n);return x*x==n;
}
int main(){/*freopen("test.in","r",stdin);freopen("test.out","w",stdout);*/ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);int T;cin>>T;while(T--){ll n;cin>>n;vector<ll>ans;while(!check(n)){ans.push_back(lowbit(n));n+=lowbit(n);}cout<<ans.size()<<'\n';for(ll i:ans){cout<<i<<' ';}if(ans.size()){cout<<'\n';}}cout.flush();/*fclose(stdin);fclose(stdout);*/return 0;
}

[GDCPC 2024] 图

从原图中找出 \(k=\left\lceil\dfrac m{n-1}\right\rceil\) 条两点之间的边不相交路径。

考虑将原图划分为 \(k\) 张图 \(T_1,T_2,\cdots,T_k\),每一张图维护一条路径。\(k\) 的值可以启发我们维护一棵类似于树的图,实际上 \(T_i\) 中有环是不优的,可以放到其他图上产生新的路径。

最终答案即找 \(u\sim v\) 的路径,使得 \(T_1,T_2,\cdots,T_k\) 中均连通。维护前缀连通,在 \(T_k\) 中随便找两个连通的点,在 \(T_1\sim T_{k-1}\) 中暴力 DFS 找即可。

但其实 \(T_i\) 应当是一棵森林。

详细题解参见此处。

参考代码
//#include<bits/stdc++.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<iomanip>
#include<cstdio>
#include<string>
#include<vector>
#include<cmath>
#include<ctime>
#include<deque>
#include<queue>
#include<stack>
#include<list>
using namespace std;
constexpr const int N=2e5,M=2e5,K=M;
int n,m;
//vector<int>g[N+1];
vector<vector<vector<int> > >tree;
int k;
struct dsu{vector<int>f,size;
//	int f[N+1],size[N+1];int find(int x){if(f[x]==x){return x;}return f[x]=find(f[x]);}void build(int n){f.resize(n+1);size.resize(n+1);for(int i=1;i<=n;i++){f[i]=i;size[i]=1;}}void merge(int x,int y){x=find(x),y=find(y);if(size[x]<size[y]){f[x]=y;size[y]+=size[x];}else{f[y]=x;size[x]+=size[y];}}
};
vector<dsu>dsu;
void resize(int n,int k){dsu.resize(k+1);for(int i=1;i<=k;i++){dsu[i].build(n+1);}tree.resize(k+1);for(int id=1;id<=k;id++){ tree[id].resize(n+1);for(int i=1;i<=n;i++){tree[id][i].resize(0);}}
}
void addEdge(int u,int v){/*for(int i=1;i<=k;i++){if(dsu[i].find(u)!=dsu[i].find(v)){tree[i][u].push_back(v);tree[i][v].push_back(u);dsu[i].merge(u,v);break;}}*/int l=1,r=k,ans=-1;while(l<=r){int mid=l+r>>1;if(dsu[mid].find(u)!=dsu[mid].find(v)){ans=mid;r=mid-1;}else{l=mid+1;}}if(ans!=-1){tree[ans][u].push_back(v);tree[ans][v].push_back(u);dsu[ans].merge(u,v);}
}
bool dfs(int x,int fx,int to,int id,vector<int>&ans){ans.push_back(x);if(x==to){return true;}for(int i:tree[id][x]){if(i==fx){continue;}if(dfs(i,x,to,id,ans)){return true;}}ans.pop_back();return false;
}
namespace debug{void dfs1(int x,int fx,int id){cerr<<x<<" ";for(int i:tree[id][x]){if(i==fx){continue;}dfs1(i,x,id);}}
}
int main(){/*freopen("test.in","r",stdin);freopen("test.out","w",stdout);*/ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);int T;cin>>T;while(T--){cin>>n>>m;k=(m+n-2)/(n-1);resize(n,k);while(m--){int u,v;cin>>u>>v;addEdge(u,v);}bool flag=true; for(int i=1;flag&&i<=n;i++){for(int j:tree[k][i]){flag=false;cout<<i<<' '<<j<<'\n';for(int id=1;id<=k;id++){vector<int>ans;dfs(i,0,j,id,ans);cout<<ans.size()<<' ';for(int x:ans){cout<<x<<' ';}cout<<'\n';}break;}}if(flag){cout<<"-1\n";}}cout.flush();/*fclose(stdin);fclose(stdout);*/return 0;
}
/*
1
3 1
1 31 3
2 1 3
*/
/*
1
4 7
1 2
2 3
3 4
4 1
1 3
2 4
1 41 4
4 1 2 3 4
2 1 4
2 1 4
*/
/*
3
3 1
1 3
4 7
1 2
2 3
3 4
4 1
1 3
2 4
1 4
5 5
1 2
2 3
3 4
4 5
3 51 3
2 1 3
1 4
4 1 2 3 4
2 1 4
2 1 4
3 5
3 3 4 5
2 3 5
*/

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/919438.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

NXP - 用MCUXpresso IDE导入lpcopen_2_10_lpcxpresso_nxp_lpcxpresso_1769.zip中的工程 - 教程

NXP - 用MCUXpresso IDE导入lpcopen_2_10_lpcxpresso_nxp_lpcxpresso_1769.zip中的工程 - 教程pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !imp…

US$42 BDM01 Adapter for Yanhua Mini ACDP Module1 BMW CAS1-CAS4+

BDM01 Adapter for Yanhua Mini ACDP Module1 BMW CAS1-CAS4+Package List:1pc x BDM01 Adapter for Yanhua Mini ACDP Module1 Pictures of BDM01 Adapter for Yanhua Mini ACDP Module1 BMW CAS1-CAS4+BDM01 Adapter…

江苏网站集约化建设frontpage怎么改网站名字

教你快速上手AI应用——吴恩达AI系列教程 人工智能风靡全球,它的应用已经渗透到我们生活的方方面面,从自动驾驶到智能家居,再到医疗辅助和量化交易等等。他们逐渐改变了我们的生活方式,然而,对于许多人来说,AI仍然是一个神秘且无法理解的领域。 为了帮助更多的人理解并掌握AI…

北京网站案例站长统计app软件

前言 Perfectly Clear WorkBench 是一款图像修复工具&#xff0c;可以帮助用户对自己的图片素材进行修复&#xff0c;很多的照片因为拍摄问题&#xff0c;或者设备限制&#xff0c;会导致拍摄效果不好&#xff0c;使用这款软件可以进行一定程度的修复&#xff0c;当拍摄时亮度…

spatial项目的主要领导者斯坦福大学ppl实验室的 Kunle Olukotun 教授和 Christos Kozyrakis 教授

Kunle Olukotun 教授是一位出身于印度的计算机科学家,他的知名成就是开创了多核处理器的先河。标签是 Sun 公司的 Niagara 系列多核处理器,当时大家都还在单核上混。Olukotun团队的解决方案:他们提出了 芯片多处理器…

程序杂谈:概述

程序语言是一切计算机程序的载体,可谓是计算机技术的核心。 这个世界上有各种各样的程序语言,本合集将介绍尽可能多的、著名的程序设计语言。了解更多的程序语言(以及背后的哲学、生态)有助于技术选型。选择正确的…

字符串基础

字符串Hash我们定义一个把字符串映射到整数的函数 \(f\) ,这个 \(f\) 称为是 \(Hash\) 数。 我们希望这个函数 \(f\) 可以方便地帮我们判断两个字符串是否相等。基础公式: $f(s)= {\textstyle \sum_{i=1}^{l}} s[i]…

免费微网站建设网站关键字字数

王江民&#xff0c;中国最早的反病毒专家&#xff0c;被业界尊称“中国杀毒软件之父”、“中国反病毒第一人。以38岁为分界&#xff0c;王江民的前半生&#xff0c;是中国青年身残志坚的楷模&#xff0c;他的后半生是中国安全软件的奠基人&#xff0c;带领中国商用软件第一个走…

营销型网站建设有哪些官方网站建设必要性

题干&#xff1a; 分东西 时间限制&#xff1a;1000 ms | 内存限制&#xff1a;65535 KB 难度&#xff1a;1 输入 第一行输出一个数i表示有i组情况&#xff08;0<i<10&#xff09; 接下来的i行&#xff0c;每一行输入两个个数M(0<M<1000000)和N(0<N<2…

Kubernetes 进阶实战:CRD、Gateway API 与优先级调度 - 实践

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

初识MYSQL —— 数据库基础 - 指南

初识MYSQL —— 数据库基础 - 指南pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco&…

多态下,构造函数和析构函数的顺序,以及父类、子类的转换

1 #include <iostream>2 #include <cstdio>3 #include <cstdlib>4 #include <cstring>5 using namespace std;6 7 #define ll long long8 9 const int maxn=1e5+10;10 11 class A {12 public:…

US$49 B48 amp; MSV90 ISN Reading via OBD Authorization for Yanhua Mini ACDP

Software License for YANHUA ACDP B48 Integrated Interface BoardWhen you buy Yanhua DME B48 Integrated Interface Board (SK247-F3), you will need to purchase this software license as well.No need shippin…

泰安微网站建设凡客诚品线下店

From: http://blog.csdn.net/wangfeng2500/article/details/7650062 在TCP层&#xff0c;有个FLAGS字段&#xff0c;这个字段有以下几个标识&#xff1a;SYN, FIN, ACK, PSH, RST, URG. 其中&#xff0c;对于我们日常的分析有用的就是前面的五个字段。 它们的含义是&#x…

在CodeBolcks下wxSmith的C++编程教程——使用 wxGrid

0.前言 欢迎来到 wxSmith 教程页面!wxSmith 与 Code::Blocks、wxWidgets 和 C++ 编译器相结合,为您提供一种所见即所得的方式来创建具有图形用户界面 (GUI) 的应用程序。该组合形成了一个用于快速应用程序开发 (R…

题解:P12479 [集训队互测 2024] 长野原龙势流星群

题目: 唉不是,这个 trick 我见过啊 QAQ! 我们想一下特殊点,发现最大的点肯定选自己,然后又会发现他的父亲也必选他,所以每次找最大的点和他父亲合并成新点即可。 合并了贪心选点的过程。 #include<bits/stdc+…

详细介绍:Docker(一)—— Docker入门到精通:从基础概念到容器管理

详细介绍:Docker(一)—— Docker入门到精通:从基础概念到容器管理2025-09-27 11:55 tlnshuju 阅读(0) 评论(0) 收藏 举报pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto…

linux下nginx

sudo apt install nginx sudo systemctl start nginx # 设置开机自启 sudo systemctl enable nginx配置文件 MacOS (Homebrew 安装): /usr/local/etc/nginx/nginx.conf Linux: /etc/nginx/nginx.conf Debian/Ubuntu 系…

青岛网站制作辰星辰凤岗本地网站

隐藏方法&#xff1a;1、打开apache的http.conf配置文件&#xff0c;开启mod_rewrite.so模块&#xff1b;2、AllowOverride None项中将None改为All&#xff1b;3、修改“.htaccess”的配置内容&#xff0c;将原代码替换为官方手册提供的代码。thinkphp现在的php主流框架之一&am…

网站建设结构总结网站建站和维护

需求&#xff1a; 上传文件&#xff0c;但是后端接口不支持多文件上传&#xff0c;但是一次性发出很多请求的话如果有100个文件那对后端的压力又太大了在上传的时候还需要有停止上传的按钮 进程&#xff1a; async await 只能做到第一步&#xff0c;但是无法在上传中的时候关…