CF789B Masha and geometric depression 题解 分类讨论

2024-02-04 04:44

本文主要是介绍CF789B Masha and geometric depression 题解 分类讨论,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Masha and geometric depression

传送门

Masha really loves algebra. On the last lesson, her strict teacher Dvastan gave she new exercise.

You are given geometric progression b b b defined by two integers b 1 b_{1} b1and q q q . Remind that a geometric progression is a sequence of integers b 1 , b 2 , b 3 , . . . b_{1},b_{2},b_{3},... b1,b2,b3,... , where for each i > 1 i>1 i>1 the respective term satisfies the condition b i = b i − 1 ⋅ q b_{i}=b_{i-1}·q bi=bi1q , where q q q is called the common ratio of the progression. Progressions in Uzhlyandia are unusual: both b 1 b_{1} b1 and q q q can equal 0 0 0 . Also, Dvastan gave Masha m m m “bad” integers a 1 , a 2 , . . . , a m a_{1},a_{2},...,a_{m} a1,a2,...,am , and an integer l l l .

Masha writes all progression terms one by one onto the board (including repetitive) while condition ∣ b i ∣ < = l |b_{i}|<=l bi<=l is satisfied ( ∣ x ∣ |x| x means absolute value of x x x ). There is an exception: if a term equals one of the “bad” integers, Masha skips it (doesn’t write onto the board) and moves forward to the next term.

But the lesson is going to end soon, so Masha has to calculate how many integers will be written on the board. In order not to get into depression, Masha asked you for help: help her calculate how many numbers she will write, or print “inf” in case she needs to write infinitely many integers.

Input

The first line of input contains four integers b 1 b_{1} b1 , q q q , l l l , m m m (- 1 0 9 < = b 1 , q < = 1 0 9 10^{9}<=b_{1},q<=10^{9} 109<=b1,q<=109 , 1 < = l < = 1 0 9 1<=l<=10^{9} 1<=l<=109 , 1 < = m < = 1 0 5 1<=m<=10^{5} 1<=m<=105 ) — the initial term and the common ratio of progression, absolute value of maximal number that can be written on the board and the number of “bad” integers, respectively.

The second line contains m m m distinct integers a 1 , a 2 , . . . , a m a_{1},a_{2},...,a_{m} a1,a2,...,am (- 1 0 9 < = a i < = 1 0 9 ) 10^{9}<=a_{i}<=10^{9}) 109<=ai<=109) — numbers that will never be written on the board.

Output

Print the only integer, meaning the number of progression terms that will be written on the board if it is finite, or “inf” (without quotes) otherwise.

Examples

input #1

3 2 30 4
6 14 25 48

output #1

3

input #2

123 1 2143435 4
123 11 -5453 141245

output #2

0

input #3

123 1 2143435 4
54343 -13 6 124

output #3

inf

Note

In the first sample case, Masha will write integers 3 , 12 , 24 3,12,24 3,12,24 . Progression term 6 6 6 will be skipped because it is a “bad” integer. Terms bigger than 24 24 24 won’t be written because they exceed l l l by absolute value.

In the second case, Masha won’t write any number because all terms are equal 123 123 123 and this is a “bad” integer.

In the third case, Masha will write infinitely integers 123 123 123 .

题目翻译

玛莎非常喜欢代数。最后一节课,严厉的老师德瓦斯坦给她布置了新的练习。

给你由两个整数 b 1 b_1 b1 q q q 定义的几何级数 b b b 。请注意,几何级数是整数 b 1 , b 2 , b 3 , … b_1, b_2, b_3,\dots b1,b2,b3, 的序列,其中每个 i > 1 i > 1 i> 1 的相应项都满足条件 b i = b i − 1 ⋅ q b_i = b_{i - 1}·q bi=bi 1q,其中 q q q 称为级数的公共比。Uzhlyandia 中的级数是不寻常的: b 1 b_1 b1 q q q 都是不寻常的。可以等于 0 0 0。此外,德瓦斯坦还给了玛莎 m m m。坏 “整数” a 1 , a 2 , . . . , a m a_1, a_2, ..., a_m a1,a2, ...,am 和一个整数 l l l

在满足条件 ∣ b i ∣ ≤ l |b_i| ≤ l bi∣ l ( ∣ x ∣ |x| x 表示 x x x 的绝对值)的情况下,玛莎将所有级数项逐一写在黑板上(包括重复项)。但有一个例外:如果一个项等于一个 "坏"整数,Masha 会跳过它(不写在黑板上),转到下一个项。

但是这节课马上就要结束了,所以玛莎必须计算出黑板上会写下多少个整数。为了避免情绪低落,玛莎向你求助:帮她计算要写多少个数字,或者打印"inf",以防她需要写无限多的整数。

输入格式

第一行输入包含四个整数 b 1 b_1 b1 , q q q , l l l , m m m ( − 1 0 9 ≤ b 1 , q ≤ 1 0 9 - 10^9 ≤ b_1, q ≤ 10^9 109b1,q 109 , 1 ≤ l ≤ 1 0 9 1 ≤ l ≤ 10^9 1 l 109 , 1 ≤ m ≤ 1 0 5 1 ≤ m ≤ 10^5 1 m 105 ) --分别是初始项和级数的共同比、可写在黑板上的最大数的绝对值以及 "坏 "整数的个数。
第二行包含 m m m 个不同的整数 a 1 , a 2 , . . . , a m a_1, a_2, ..., a_m a1,a2, ...,am − 1 0 9 ≤ a i ≤ 1 0 9 - 10^9 ≤ a_i ≤ 10^9 109ai 109) --永远不会写在黑板上的数字。

输出格式

打印唯一的整数,即如果是有限的,则在黑板上写出的递进项的个数,否则打印"inf"(不带引号)。

提示

在第一个示例中,Masha 将写入整数 3 , 12 , 24 3, 12, 24 3, 12, 24 。递增项 6 6 6 将被跳过,因为它是一个 "坏 "整数。大于 24 24 24 的项不会被写入,因为它们的绝对值超过了 l l l
在第二种情况下,Masha 不会写入任何数字,因为所有项都等于 123 123 123 ,这是一个 "坏 "整数。

在第三种情况下,玛莎会写出无限大的整数 123 123 123

以上来自 C o d e F o r c e s ,翻译:崩溃了一次的 D e e p L 。 以上来自CodeForces,翻译:崩溃了一次的DeepL。 以上来自CodeForces,翻译:崩溃了一次的DeepL

解题思路

前言

你知道 C o d e F o r c e s 有一个功能叫题解吗? \color{white}你知道CodeForces有一个功能叫题解吗? 你知道CodeForces有一个功能叫题解吗?
你知道我为什么空了一行吗?

正文

分类讨论:

  1. ∣ b 1 ∣ > l |b1| > l b1∣ >l :
    • 答案为 0 0 0
  2. b 1 = 0 b_1 = 0 b1= 0
    • 如果数组 a a a 中存在 0 0 0,则答案为 0 0 0
    • 否则为 inf
  3. q = 1 q = 1 q= 1
    • 如果数组 a a a 中有 b 1 b_1 b1 ,则答案为 0 0 0
    • 否则为 inf
  4. q = − 1 q =  - 1 q=   1
    • 如果数组 a a a 中同时存在 b 1 b_1 b1 和   − b 1 - b_1 b1 ,则答案为 0 0 0
    • 否则为 inf
  5. q = 0 q = 0 q= 0
    • 如果数组 a a a 中不存在 0 0 0,则答案为 inf
    • 否则
      • 如果 a a a 中存在 b 1 b_1 b1 ,则答案为 0 0 0
      • 否则答案为 1 1 1
  6. 在所有其他情况下,我们可以简单地遍历级数 b b b 中的所有项,只要它们的绝对值不超过 l l l 。显然,下一个元素的绝对值至少是上一个元素绝对值的 2 2 2 倍。这就是为什么我们最多需要检查 log ⁡ l \log l logl 项。 l l l 递推项。

解的复杂度为 O ( M ⋅ log ⁡ L ) O(M·\log L) O(MlogL) O ( M ⋅ log ⁡ M + log ⁡ L ⋅ log ⁡ M ) O(M·\log M + \log L·\log M) O(MlogM+logLlogM) (<-你知道为什么吗)。

AC Code

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define I 1.0
#define II 2.0
const int Maxn = 10 + 5;
int n, t;
double f[Maxn][Maxn];
int ans;
inline void init();
inline void solve();
inline void work() {init();solve();
}
signed main() {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);work();return 0;
}
inline void init() {cin >> n >> t;f[0][0] = t;
}
inline void solve() {for (int i = 0; i < n - 1; i++)for (int j = 0; j <= i; j++)if (f[i][j] >= I) {f[i + 1][j] += (f[i][j] - I) / II;f[i + 1][j + 1] += (f[i][j] - I) / II;f[i][j] = I;}for (int i = 0; i < n; i++)for (int j = 0; j <= i; j++)if (f[i][j] >= 1.0)ans++;cout << ans << endl;
}

无奖竞猜:此题洛谷评分?
答案: 普及 + / 提高 \textcolor{green}{普及+/提高} 普及+/提高。就问你6不6。

这篇关于CF789B Masha and geometric depression 题解 分类讨论的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/676383

相关文章

MySQL中闪回功能的方案讨论及实现

《MySQL中闪回功能的方案讨论及实现》Oracle有一个闪回(flashback)功能,能够用户恢复误操作的数据,这篇文章主要来和大家讨论一下MySQL中支持闪回功能的方案,有需要的可以了解下... 目录1、 闪回的目标2、 无米无炊一3、 无米无炊二4、 演示5、小结oracle有一个闪回(flashb

C#使用DeepSeek API实现自然语言处理,文本分类和情感分析

《C#使用DeepSeekAPI实现自然语言处理,文本分类和情感分析》在C#中使用DeepSeekAPI可以实现多种功能,例如自然语言处理、文本分类、情感分析等,本文主要为大家介绍了具体实现步骤,... 目录准备工作文本生成文本分类问答系统代码生成翻译功能文本摘要文本校对图像描述生成总结在C#中使用Deep

基于人工智能的图像分类系统

目录 引言项目背景环境准备 硬件要求软件安装与配置系统设计 系统架构关键技术代码示例 数据预处理模型训练模型预测应用场景结论 1. 引言 图像分类是计算机视觉中的一个重要任务,目标是自动识别图像中的对象类别。通过卷积神经网络(CNN)等深度学习技术,我们可以构建高效的图像分类系统,广泛应用于自动驾驶、医疗影像诊断、监控分析等领域。本文将介绍如何构建一个基于人工智能的图像分类系统,包括环境

认识、理解、分类——acm之搜索

普通搜索方法有两种:1、广度优先搜索;2、深度优先搜索; 更多搜索方法: 3、双向广度优先搜索; 4、启发式搜索(包括A*算法等); 搜索通常会用到的知识点:状态压缩(位压缩,利用hash思想压缩)。

C++ | Leetcode C++题解之第393题UTF-8编码验证

题目: 题解: class Solution {public:static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num &

C语言 | Leetcode C语言题解之第393题UTF-8编码验证

题目: 题解: static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num & MASK1) == 0) {return

C - Word Ladder题解

C - Word Ladder 题解 解题思路: 先输入两个字符串S 和t 然后在S和T中寻找有多少个字符不同的个数(也就是需要变换多少次) 开始替换时: tips: 字符串下标以0开始 我们定义两个变量a和b,用于记录当前遍历到的字符 首先是判断:如果这时a已经==b了,那么就跳过,不用管; 如果a大于b的话:那么我们就让s中的第i项替换成b,接着就直接输出S就行了。 这样

【秋招笔试】9.07米哈游秋招改编题-三语言题解

🍭 大家好这里是 春秋招笔试突围,一起备战大厂笔试 💻 ACM金牌团队🏅️ | 多次AK大厂笔试 | 大厂实习经历 ✨ 本系列打算持续跟新 春秋招笔试题 👏 感谢大家的订阅➕ 和 喜欢💗 和 手里的小花花🌸 ✨ 笔试合集传送们 -> 🧷春秋招笔试合集 🍒 本专栏已收集 100+ 套笔试题,笔试真题 会在第一时间跟新 🍄 题面描述等均已改编,如果和你笔试题看到的题面描述

LeetCode 第414场周赛个人题解

目录 Q1. 将日期转换为二进制表示 原题链接 思路分析 AC代码 Q2. 范围内整数的最大得分 原题链接 思路分析 AC代码 Q3. 到达数组末尾的最大得分 原题链接 思路分析 AC代码 Q4. 吃掉所有兵需要的最多移动次数 原题链接 思路分析 AC代码 Q1. 将日期转换为二进制表示 原题链接 Q1. 将日期转换为二进制表示 思路分析

用Pytho解决分类问题_DBSCAN聚类算法模板

一:DBSCAN聚类算法的介绍 DBSCAN(Density-Based Spatial Clustering of Applications with Noise)是一种基于密度的聚类算法,DBSCAN算法的核心思想是将具有足够高密度的区域划分为簇,并能够在具有噪声的空间数据库中发现任意形状的簇。 DBSCAN算法的主要特点包括: 1. 基于密度的聚类:DBSCAN算法通过识别被低密