codeforces Masha and geometric depression(set模拟,等比数列)

神坑题,地址:http://codeforces.com/problemset/problem/789/B


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 integersb1, 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" integersa1, 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.

【解析】:本来一直在想特判一下,q=0,q=1,q=-1,和b1=0的情况,写的越多越乱,还是wrong。

干脆直接不判了,就判了一下q=-1,竟然过了。

方法是暴力往set里加数,一旦加入的数已经出现过了,说明陷入了循环。如果循环的这个数没有在坏数里,那一定是inf

其实,除了q=-1的时候,是b1,-b1,b1,-b1这样两个数字循环,其余情况最多只会循环一个数字!

所以只要出现已经出现过的数字,那么这个数字一定是循环。

最后判断下这个数字是不是坏数

【代码】:

#include <stdio.h>
#include <math.h>  
#include <iostream>  
#include <set>  
using namespace std;
typedef long long ll;
ll a[120000];
ll b1,q,l,m;
set<ll> s;
int main()
{
	while(cin>>b1>>q>>l>>m)
	{
		s.clear();
		int v[4]={0};
		for(int i=0;i<m;i++)
		{
			scanf("%lld",&a[i]);
			if(a[i]==b1)v[1]=1;
			if(a[i]==-b1)v[2]=1;
		}
		if(q==-1){//循环 
			if(v[1]&&v[2]||abs(b1)>l)puts("0");
			else puts("inf");
			continue;
		}
		int flag=0x7fffffff;
		for(ll p=b1;abs(p)<=l;p*=q)
		{
			if(s.count(p)){
				flag=p;
				break;
			}
			s.insert(p);
		}
		for(int i=0;i<m;i++)
		{
			if(s.count(a[i]))
				s.erase(a[i]);
		}
		if(s.count(flag))puts("inf");
		else
		printf("%d\n",s.size());
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雪的期许

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值