CodeForces 789B (Masha and geometric depression)

B. Masha and geometric depression
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 b1qlm (-109 ≤ b1, q ≤ 1091 ≤ l ≤ 1091 ≤ 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.

Examples
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.

好题一枚,这个题就是一个数学题,但是呢需要考虑的细节有很多,否则就各种WA,

红到怀疑人生,其实大概就分为几种情况,当然分-1和其他的用容器也能过。

① b1如果大于L的话直接输出0,因为题目要求是b1必须小于L

②q==0 只要查询下b1在不在坏数里面和0在不在坏数里面

③q==1  只需要查询下b1在不在坏数里面

④q==-1 只需要查询-b1和b1在不在坏数里面

⑤b1==0 只需要查询下0在不在坏数里面

剩下的直接暴力查找即可

本题用了二分的lower_bound  注意下这个函数的返回值就行,返回的是地址

所以最好是减去首地址,然后查询是不是在数组内。

还有题目用long long 和__int64  都可以过,有的地方可能卡其中一个

#include<stdio.h>
#include<iostream> 
#include <algorithm>
#include<string.h>
#include<math.h>
#include<queue>
#include<set>
#define LL long long
#define INF 0x3f3f3f3f
#define s(n) scanf("%d",&n)
using namespace std; 
long long  temp[100500];
int main()
{
	__int64 b1,q,l,m;
	int w,l1;
	while(scanf("%lld%lld%lld%lld",&b1,&q,&l,&m)!=EOF)
	{
		for(int i=0;i<m;i++)
			scanf("%lld",&temp[i]);
		temp[m]=INF; 
		sort(temp,temp+m);
		if(fabs(b1)>l)
			printf("0\n");
		else if(b1==0)//所有的数都是0 如果有就输出0 没有就inf 
		{
			w=lower_bound(temp,temp+m,b1)-temp;
			if(temp[w]==b1)
				printf("0\n"); 
			else
				printf("inf\n"); 
		}
		else if(q==0)//递增 等于0 的话  看b1 和 0 
		{
			w=lower_bound(temp,temp+m,b1)-temp;
			l1=lower_bound(temp,temp+m,0)-temp;
			if(temp[l1]!=0)//里面没有0 
				printf("inf\n");//输出的都是0 
			else if(temp[w]==b1)//坏数中有0的前提下 还有 b1
				printf("0\n");
			else//坏数中没有b1 
				printf("1\n");
		}
		else if(q==-1)//b1不等于0  而且 q==-1 说明这两个数是互为相反数 
		{
			w=lower_bound(temp,temp+m,b1)-temp;
			l1=lower_bound(temp,temp+m,-b1)-temp;
			if(temp[w]==b1 && temp[l1]==-b1)//坏数中都存在 
				printf("0\n");
			else 
				printf("inf\n");
		} 
		else if(q==1)//说明都是b1这个数字
		{
			w=lower_bound(temp,temp+m,b1)-temp;
			if(temp[w]==b1)
				printf("0\n");
			else
				printf("inf\n");
		} 
		else//剩下的暴力 
		{
			int ans=0;
			for(LL i=b1;fabs(i)<=l;i=i*q) 
			{
				w=lower_bound(temp,temp+m,i)-temp;
				if(temp[w]!=i)//坏数中没有当前的数 
					ans++;
			} 
			printf("%d\n",ans); 
		}	
	}
	return 0;
} 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值