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}{普及+/提高} 普及+/提高。就问你66

  • 25
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值