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.
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.
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.
3 2 30 4 6 14 25 48
3
123 1 2143435 4 123 11 -5453 141245
0
123 1 2143435 4 54343 -13 6 124
inf
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.
题目链接:http://codeforces.com/contest/789/problem/B
看到数据瞬间就A了,少了一个else,愁死了,码力码力 码力!!!这场比赛两个题都是差一点。。。。
纯模拟就好,题目的意思是说给你一个b1,q,l,m,分别代表初始值,公比,绝对值上限,不可出现的数.
代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <cmath>
#define LL long long
using namespace std;
map<LL,int>mp;
int a[300000];
int main(){
LL b1,q,l,m;
scanf("%I64d%I64d%I64d%I64d",&b1,&q,&l,&m);
for(int i=0;i<m;i++){
LL x;
scanf("%I64d",&x);
mp[x]=1;
}
if(b1==0){
if(mp[0]==1){
printf("0\n");
}
else{
printf("inf\n");
}
return 0;
}
if(q==0){
if(abs(b1)>l){
printf("0\n");
}
else if(mp[0]==1&&mp[b1]==1){//少了这个else,码力的重要性
printf("0\n");
}
else if(mp[0]==1){
printf("1\n");
}
else{
printf("inf\n");
}
return 0;
}
if(q==1){
if(abs(b1)>l){
printf("0\n");
}
else if(mp[b1]==1){
printf("0\n");
}
else{
printf("inf\n");
}
return 0;
}
if(q==-1){
if(abs(b1)>l){
printf("0\n");
}
else if(mp[b1]==1&&mp[-b1]==1){
printf("0\n");
}
else{
printf("inf\n");
}
return 0;
}
int top=0;
for(;;){
if(abs(b1)>l)
break;
if(mp[b1]==1){
b1*=q;
continue;
}
if(top!=0&&b1==a[top-1]){
break;
}
a[top++]=b1;
b1*=q;
}
printf("%d\n",top);
return 0;
}