CodeForces - 789B 模拟

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

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

Masha writes all progression terms one by one onto the board (including repetitive) while condition |bi| ≤ l is satisfied (|x| means absolute value of 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 b1, q, l, m (-109 ≤ b1, q ≤ 109, 1 ≤ l ≤ 109, 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 distinct integers a1, a2, ..., am (-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.

Example
Input
3 2 30 4
6 14 25 48
Output
3
Input
123 1 2143435 4
123 11 -5453 141245
Output
0
Input
123 1 2143435 4
54343 -13 6 124
Output
inf
Note

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

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

In the third case, Masha will write infinitely integers 123

题意:

  给你一个等差数列的首项b1和公比q,还有一个上界l,但是b1和q都可以为0。又给出一个序列a,然后挨个计算出这个等差数列bi,如果bi出现在序列a中,那么就不写出来,最后问你写出来的数有几个。前提是所有的bi的绝对值都必须不超过上界l。

思路:读懂题意,好好模拟蛮简单的,但是特别容易错,看代码讨论结果;

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define max_n 100010
using namespace std;
typedef long long ll;

ll a[max_n];

int main()
{
	ll b,q,l,m;
	scanf("%lld %lld %lld %lld",&b,&q,&l,&m);
	for(int i=0;i<m;i++)
		scanf("%lld",&a[i]);
	sort(a,a+m);
	if(abs(b)>l)
	{
		printf("0\n");
		return 0;
	}
	if(q==0)
	{
		bool t1=binary_search(a,a+m,b);
		bool t2=binary_search(a,a+m,0);
		if(!t1 && t2 && abs(b)<=l)
			printf("1\n");
		else if(!t2)
			printf("inf\n");
		else
			printf("0\n"); 
		return 0;
	}
	if(b==0)
	{
		bool t1=binary_search(a,a+m,0);
		if(!t1)
			printf("inf\n");
		else
			printf("0\n");
		return 0;
	}
	if(q==1)
	{
		bool t1=binary_search(a,a+m,b);
		if(!t1)
			printf("inf\n");
		else
			printf("0\n");
		return 0;
	}
	if(q==-1)
	{
		bool t1=binary_search(a,a+m,b);
		bool t2=binary_search(a,a+m,-b);
		if(!t1 || !t2)
			printf("inf\n");
		else printf("0\n");
		return 0;
	}
	ll ans=b,sum=0;
	for(int i=1;abs(ans)<=l;i++)
	{
		bool t1=binary_search(a,a+m,ans);
		if(!t1)
			sum++;
		ans*=q;
	}
	printf("%lld\n",sum);
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值