您的位置:首页 > 其它

Codeforces Round #407 (Div. 2) B. Masha and geometric depression

2017-03-30 10:45 316 查看
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 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.

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.

这道题我自己的解法是按着test点写的,实在是愚笨的方法,但最终是A了,令我意外的我的代码是运行速度最快,占用内存最少的,哈哈,瞎猫碰着死耗子了,还是献丑贴出来吧。这道题有一个证明一个整数是另一个整数的幂的函数,很好用。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll b,q,l,m;
set<ll> st;

bool IsPower(int number, int powerNumber) //number是否是powerNumber的幂数
{
if (powerNumber == 0)
{
return false;
}
if (powerNumber == 1)
{
return number == 1;
}
if (powerNumber == -1)
{
return (number == 1) || (number == -1);
}

int m = 0;
while (m == 0 && (abs(number) >= abs(powerNumber)))
{
m = number % powerNumber;
number /= powerNumber;
}

return (m==0) && (number == 1);
}

int main()
{
cin>>b>>q>>l>>m;
ll i,j;
ll temp;
ll num=0;
ll flag=0;
int liu;
for(i=0;i<m;i++)
{
scanf("%lld",&temp);
if(temp==0) //temp==0 说明只适合讨论b或者q是0的时候 ,有以下几种情况
//b!=0 q==0 b==0 q==0 b!=0 q==0 b==0 q!=0 这个时候这个flag就有大作用
{
flag=1;
continue;
}
if(temp==b) //这个判断主要是为了应付这个样例
/*123 0 2143435 4
5433 0 123 -645*/
{
st.insert(temp);
continue;
}
if(b!=0 && q!=0 && temp%b==0 && temp<=l) //b q都不能等于0,因为做分母,temp必须<=l
{
liu=temp/b;
if(IsPower(liu,q)==true)
{
st.insert(temp);
}
}
}
if(b==0)
{
if(flag==1) //如果有零
{
printf("0\n");
return 0;
}
else
{
printf("inf\n");
return 0;
}
}
else if(q==0 && abs(b)<=l) //abs(b)<=l 是为了保证b也能写在黑板上
{
if(flag==1) //看看禁选名单里有没有b
{
printf("%d\n",1-st.size());
return 0;
}
else
{
printf("inf\n");
return 0;
}
}
else if(q==0 && abs(b)>l)
{
printf("0\n");
return 0;
}
else if(q==1)
{
if(abs(b)>l) //如果b>l,那么肯定是0
{
printf("0\n");
return 0;
}
else if(st.size()==0)
{
printf("inf\n");
return 0;
}
else
{
printf("0\n");
return 0;
}
}
else if(q==-1)
{
if(abs(b)>l)
{
printf("0\n");
return 0;
}
else if(st.size()==2)
{
printf("0\n");
return 0;
}
else
{
printf("inf\n");
return 0;
}
}
else
{
ll tt=0;
if(abs(b)>l) //大于l,那么绝对值肯定超出范围,输出0
{
printf("0\n");
return 0;
}
tt=1;
while(abs(b*q)<=l)
{
tt++;
b*=q;
}
printf("%lld\n",tt-st.size());
}
return 0;
}

下边的是人家的代码,不得不说,又学习了一个算法技巧
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
map<int,int> mp;
int main(){
ll a,r,l,m;int ans=0;int v;
cin >> a >> r >> l >> m;
for(int i=0;i<m;i++){
scanf("%d",&v);
mp[v]=1;
}
for(int i=0;i<100;i++){
if(abs(a)>l)
break;
if(!mp[a])
ans++;
a*=r;

}
if(ans<50)
cout<<ans;
else
cout<<"inf";
}他的算法思想是这样的,先把禁选名单里标记一下,在后边的等比数列中一旦出现就不计这一项,这道题最关键的一个点是这个for循环,我们问最小的等比数列,就是递增最慢的(出去b 或者q等于0的情况)情况是b=1,q=2;我们就以这个极限为例,2^50肯定大于l,所有,你如果是个正常的等比数列,ANS是不会大于50的,所以,大于50的都是有无数个<l
的项的,输出inf,小于50说明是个正常的等比数列,很巧妙。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: