Ehab and the Big Finale ( 交互题)

本文解析了Codeforces竞赛中的一道F级题目,详细介绍了如何通过有限次询问找到未知点x的方法,包括dy和sy两种询问方式,并提供了完整的C++代码实现。

题目

https://codeforces.com/contest/1174/problem/F

题意

给你一个n个节点树,有一个未知的x点,让你有限次询问找到x

询问的方式有两种

1、d y 询问x点与y点之间的距离
2、s y 询问y点到x点的路径中y点的下一个点。

思路

https://dudulu.net/blog/?p=1696

代码

#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i = (a);i <= (b);i++)
const int N = 2000005;
vector<int> q[N];
int vis[N],deep[N],siz[N],son[N];
int n;
int depx;
vector<int> qw;
void dfss(int u,int fa)
{
    siz[u] = 1;
    deep[u] = deep[fa] + 1;
    son[u] = -1;
    for(int i = 0;i < q[u].size();i++)
    {
        int v = q[u][i];
        if(v == fa) continue;
        dfss(v,u);
        siz[u] += siz[v];
        if(son[u] == -1||siz[v] > siz[son[u]])
        {
            son[u] = v;
        }
    }
}
void dffs(int u)
{

    qw.clear();
    int p = u;
    while(p != -1)
    {
        qw.push_back(p);
        p = son[p];
    }
    int v = qw.back();
    int outt;
    cout<<"d "<<v<<endl;
    cin>>outt;
    int depy = (depx+deep[v]-outt)/2;
    int y = qw[depy-deep[u]];

    if(deep[y] == depx)
    {
        cout<<"! "<<y<<endl;
        return ;
    }
    int yy;
    cout<<"s "<<y<<endl;
    cin>>yy;
    dffs(yy);
}

int main()
{
    ios::sync_with_stdio(false);
    int n;
    cin>>n;
    rep(i,1,n-1)
    {
        int u,v;
        cin>>u>>v;
        q[u].push_back(v);
        q[v].push_back(u);
    }
    cout<<"d 1"<<endl;
    fflush(stdout);
    cin>>depx;
    if(depx == 0)
    {
        cout<<"! 1"<<endl;
        return 0;
    }
    deep[0] = 0;

    dfss(1,0);
    dffs(1);
    return 0;
}

 

内容概要:本文设计了一种基于PLC的全自动洗衣机控制系统内容概要:本文设计了一种,采用三菱FX基于PLC的全自动洗衣机控制系统,采用3U-32MT型PLC作为三菱FX3U核心控制器,替代传统继-32MT电器控制方式,提升了型PLC作为系统的稳定性与自动化核心控制器,替代水平。系统具备传统继电器控制方式高/低水,实现洗衣机工作位选择、柔和过程的自动化控制/标准洗衣模式切换。系统具备高、暂停加衣、低水位选择、手动脱水及和柔和、标准两种蜂鸣提示等功能洗衣模式,支持,通过GX Works2软件编写梯形图程序,实现进洗衣过程中暂停添加水、洗涤、排水衣物,并增加了手动脱水功能和、脱水等工序蜂鸣器提示的自动循环控制功能,提升了使用的,并引入MCGS组便捷性与灵活性态软件实现人机交互界面监控。控制系统通过GX。硬件设计包括 Works2软件进行主电路、PLC接梯形图编程线与关键元,完成了启动、进水器件选型,软件、正反转洗涤部分完成I/O分配、排水、脱、逻辑流程规划水等工序的逻辑及各功能模块梯设计,并实现了大形图编程。循环与小循环的嵌; 适合人群:自动化套控制流程。此外、电气工程及相关,还利用MCGS组态软件构建专业本科学生,具备PL了人机交互C基础知识和梯界面,实现对洗衣机形图编程能力的运行状态的监控与操作。整体设计涵盖了初级工程技术人员。硬件选型、; 使用场景及目标:I/O分配、电路接线、程序逻辑设计及组①掌握PLC在态监控等多个方面家电自动化控制中的应用方法;②学习,体现了PLC在工业自动化控制中的高效全自动洗衣机控制系统的性与可靠性。;软硬件设计流程 适合人群:电气;③实践工程、自动化及相关MCGS组态软件与PLC的专业的本科生、初级通信与联调工程技术人员以及从事;④完成PLC控制系统开发毕业设计或工业的学习者;具备控制类项目开发参考一定PLC基础知识。; 阅读和梯形图建议:建议结合三菱编程能力的人员GX Works2仿真更为适宜。; 使用场景及目标:①应用于环境与MCGS组态平台进行程序高校毕业设计或调试与运行验证课程项目,帮助学生掌握PLC控制系统的设计,重关注I/O分配逻辑、梯形图与实现方法;②为工业自动化领域互锁机制及循环控制结构的设计中类似家电控制系统的开发提供参考方案;③思路,深入理解PL通过实际案例理解C在实际工程项目PLC在电机中的应用全过程。控制、时间循环、互锁保护、手动干预等方面的应用逻辑。; 阅读建议:建议结合三菱GX Works2编程软件和MCGS组态软件同步实践,重理解梯形图程序中各环节的时序逻辑与互锁机制,关注I/O分配与硬件接线的对应关系,并尝试在仿真环境中调试程序以加深对全自动洗衣机控制流程的理解。
This time Baby Ehab will only cut and not stick. He starts with a piece of paper with an array a a of length n n written on it, and then he does the following: he picks a range ( l , r ) (l,r) and cuts the subsegment a l , a l + 1 , … , a r a l ​ ,a l+1 ​ ,…,a r ​ out, removing the rest of the array. he then cuts this range into multiple subranges. to add a number theory spice to it, he requires that the elements of every subrange must have their product equal to their least common multiple (LCM). Formally, he partitions the elements of a l , a l + 1 , … , a r a l ​ ,a l+1 ​ ,…,a r ​ into contiguous subarrays such that the product of every subarray is equal to its LCM. Now, for q q independent ranges ( l , r ) (l,r), tell Baby Ehab the minimum number of subarrays he needs. Input The first line contains 2 2 integers n n and q q ( 1 ≤ n , q ≤ 10 5 1≤n,q≤10 5 )the length of the array a a and the number of queries. The next line contains n n integers a 1 a 1 ​ , a 2 a 2 ​ , … …, a n a n ​ ( 1 ≤ a i ≤ 10 5 1≤a i ​ ≤10 5 )the elements of the array a a. Each of the next q q lines contains 2 2 integers l l and r r ( 1 ≤ l ≤ r ≤ n 1≤l≤r≤n)the endpoints of this query's interval. Output For each query, print its answer on a new line. Examples Inputcopy Outputcopy 6 3 2 3 10 7 5 14 1 6 2 4 3 5 3 1 2 Note The first query asks about the whole array. You can partition it into [ 2 ] [2], [ 3 , 10 , 7 ] [3,10,7], and [ 5 , 14 ] [5,14]. The first subrange has product and LCM equal to 2 2. The second has product and LCM equal to 210 210. And the third has product and LCM equal to 70 70. Another possible partitioning is [ 2 , 3 ] [2,3], [ 10 , 7 ] [10,7], and [ 5 , 14 ] [5,14]. The second query asks about the range ( 2 , 4 ) (2,4). Its product is equal to its LCM, so you don't need to partition it further. The last query asks about the range ( 3 , 5 ) (3,5). You can partition it into [ 10 , 7 ] [10,7] and [ 5 ] [5]. 解法:条件就是两两互质,预处理质因⼦集,找出每个数下⼀个不互质的位置。 i 进⼀步的,可以找出每个 作为左端,可⾏的右端最⼤是多⼤(区间 min)。 2j 因此每次划分极⻓就可以了,令 f(i, j) 然后枚举答案的⼆进制下每⼀位即可。 。 求C++代码.
06-10
### 代码概述 该问一个基于数组划分的优化问,要求将给定区间划分为多个子区间,使得每个子区间的乘积等于其最小公倍数(LCM)。为此需要预处理每个元素的质因数集合,并找到每个位置右侧第一个与其不互质的位置。通过动态规划或双指针方法可以实现极长区间的划分。 以下是求解此问的C++代码实现: --- ### 代码解析 ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 5; int n, q; int a[MAXN]; vector<int> primes; vector<bool> is_prime(MAXN, true); vector<vector<int>> factor(MAXN); // 预处理所有小于等于10^5的质数 void sieve() { is_prime[0] = is_prime[1] = false; for (int i = 2; i < MAXN; ++i) { if (is_prime[i]) { primes.push_back(i); for (long long j = 1LL * i * i; j < MAXN; j += i) is_prime[j] = false; } } } // 计算a[i]的所有质因数 void factorize() { for (int i = 1; i <= 1e5; ++i) { int x = i; for (auto p : primes) { if (p * p > x) break; if (x % p == 0) { factor[i].push_back(p); while (x % p == 0) x /= p; } } if (x > 1) factor[i].push_back(x); } } // 预处理next[i]表示从i开始向右第一个与a[i]不互质的下标 int next_non_coprime_pos[MAXN]; void preprocess_next() { // 初始化next数组为n+1 fill(next_non_coprime_pos, next_non_coprime_pos + n + 1, n + 1); // 对于每个质因数p,维护last[p] vector<int> last(1e5 + 1, n + 1); for (int i = n - 1; i >= 0; --i) { for (auto p : factor[a[i + 1]]) { // 注意数组下标从1开始 next_non_coprime_pos[i] = min(next_non_coprime_pos[i], last[p]); } for (auto p : factor[a[i + 1]]) { last[p] = i + 1; // 更新last[p] } } } // 处理查询 int main() { ios::sync_with_stdio(false); cin.tie(0); sieve(); factorize(); cin >> n >> q; for (int i = 1; i <= n; ++i) cin >> a[i]; preprocess_next(); while (q--) { int l, r; cin >> l >> r; l -= 1; // 转换为0-based索引 int cnt = 0; while (l < r) { cnt++; l = next_non_coprime_pos[l]; } cout << cnt << "\n"; } return 0; } ``` --- ### 知识 1. **埃拉托色尼筛法** 用于快速生成小于等于某个上限的所有质数,复杂度为$O(n \log \log n)$。 2. **质因数分解** 使用已知的质数表对每个数字进行质因数分解,复杂度为$O(\sqrt{n})$。 3. **双指针算法** 通过预处理和动态更新最远可到达位置,实现高效区间划分,复杂度为$O(n)$。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值