CodeForces

来源:互联网 发布:python rsa 加密 编辑:程序博客网 时间:2024/06/03 17:53

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 46 14 25 48
output
3
input
123 1 2143435 4123 11 -5453 141245
output
0
input
123 1 2143435 454343 -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.


就是模拟啊,不过得把特殊的几个找出来,1、q=0。2、b1=0。3、q=1。4、q=-1,剩下的模拟就行,一开始忽略了-1这个情况,汗,我说为什么超时,对啦,还必须判断下b1 > l的情况,不然卡在第14组实例,这组实例应该是b > l 但是q=0或q=1或q=-1吧


ac代码:

#include <iostream>#include <cstdio>#include <cmath>#include <set>#include <queue>#include <algorithm>#define ll long longusing namespace std;const int maxn=1e5+10;ll b,q,l,m;set<ll> st;int main(){while(~scanf("%I64d%I64d%I64d%I64d",&b,&q,&l,&m)){st.clear();ll tmp;for(ll i=1;i<=m;i++){scanf("%I64d",&tmp);st.insert(tmp);} if(abs(b) > l){printf("0\n");continue ;}if(q==0){if(st.find(0) == st.end()){printf("inf\n");}else if(st.find(b)==st.end()){printf("1\n");}else printf("0\n");continue ; }if(b==0 || q==1){if(st.find(b)==st.end()){printf("inf\n");}else printf("0\n");continue ; }if(q==-1){if(st.find(b)==st.end() || st.find(-b)==st.end()){printf("inf\n");}else printf("0\n");continue ; }int cnt=0;while(abs(b) <= l){if(st.find(b) == st.end()){cnt++;}b=b*q;}printf("%d\n",cnt);}return 0;}