codeforces 911C (找规律)

本文探讨了一道关于圣诞树装饰灯的算法题,通过分析不同装饰灯的开关周期,寻找一种策略使得在任意时刻至少有一盏灯亮起。文章提出了一种简洁的解决方案,并通过代码实现验证了其有效性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述

Mishka is decorating the Christmas tree. He has got three garlands, and all of them will be put on the tree. After that Mishka will switch these garlands on.

When a garland is switched on, it periodically changes its state — sometimes it is lit, sometimes not. Formally, if i-th garland is switched on during x-th second, then it is lit only during seconds x, x + ki, x + 2ki, x + 3ki and so on.

Mishka wants to switch on the garlands in such a way that during each second after switching the garlands on there would be at least one lit garland. Formally, Mishka wants to choose three integers x1, x2 and x3 (not necessarily distinct) so that he will switch on the first garland during x1-th second, the second one — during x2-th second, and the third one — during x3-th second, respectively, and during each second starting from max(x1, x2, x3) at least one garland will be lit.

Help Mishka by telling him if it is possible to do this!

思路

通过找规律发现,只有一下几种情况时才成立

1 X X
2 2 X
3 3 3
4 4 2

直接判断即可

代码

#include<bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<long,long> PLL;
typedef pair<char,char> PCC;
typedef long long LL;
const int N=5005;
const int M=150;
const int INF=0x3f3f3f3f;
const int MOD=998244353;
int cnt[N];
void solve(){
    int k1,k2,k3;cin>>k1>>k2>>k3;
    cnt[k1]++;
    cnt[k2]++;
    cnt[k3]++;
    if(cnt[1]>=1) cout<<"YES"<<endl;
    else if(cnt[2]>=2) cout<<"YES"<<endl;
    else if(cnt[3]>=3) cout<<"YES"<<endl;
    else if(cnt[4]==2 && cnt[2]==1) cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
}
int main(){
    IOS;
    solve();
    return 0;
}

### 关于 Floor 和 Ceil 函数在编程中的实现 Floor 和 Ceil 是两个常见的数学函数,在许多算法竞赛平台(如 Codeforces)上经常被用于解决涉及整数除法、取模运算等问题。以下是它们的定义以及如何在程序设计中实现这些功能。 #### 定义 - **Floor Function**: 对实数向下取整,返回不大于该数值的最大整数[^1]。 - **Ceil Function**: 对实数向上取整,返回不小于该数值的最小整数[^2]。 #### 实现方法 大多数现代编程语言都提供了内置库来支持 floor 和 ceil 的计算: ##### 使用标准库 在 C++ 中可以利用 `<cmath>` 库中的 `floor` 和 `ceil` 方法完成相应操作: ```cpp #include <iostream> #include <cmath> int main() { double num = 3.7; std::cout << "Floor value: " << static_cast<int>(std::floor(num)) << "\n"; // 输出 3 std::cout << "Ceil value: " << static_cast<int>(std::ceil(num)) << "\n"; // 输出 4 } ``` 然而需要注意的是,当处理负数时,这两个函数的行为可能不符合直觉。例如,`floor(-2.5)` 返回 `-3` 而不是 `-2`,因为它是严格意义上的“下限”。 如果目标是在没有浮点误差的情况下仅针对正整数执行类似的逻辑,则可以通过简单的算术表达式手动模拟这两种行为而无需调用任何外部库函数: - 手动实现 Floor: 当 a / b 结果为非零余数时保持原样;否则减去一个小量使得最终结果总是趋向更小的方向移动直到达到下一个较低的整数位置为止。 - 手动实现 Ceil: 如果存在剩余部分则增加到下一更高的整数值上去;如果没有多余的部分,那么它本身已经是天花板高度了. 具体代码如下所示: ```python def manual_floor(a, b): return (a // b) def manual_ceil(a, b): if a % b == 0: return a // b else: return (a // b) + 1 ``` 上述 Python 版本展示了如何通过基本运算符构建自己的版本而不依赖额外模块的帮助[^3]. ### 示例应用案例分析 考虑到实际应用场景下的复杂度需求,这里选取几个典型例子加以说明其用途所在之处及其重要性何在? 比如 CF 圆形 #701(Division Two)-Problem C(Floor And Mod), 这道题目要求我们到满足特定条件的一系列数字组合方案数目统计工作当中需要用到大量的地板除法规律推导过程才能得出正确结论出来. 同样还有像CF Problem D(编号1469)-Ceiling Division Operations [^2], 此处探讨的就是怎样快速有效地把初始状态转变成为期望结束形态所经历过的最少次数动作规划策略研究方向上面去了.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值