B. Masha and geometric depression

54 篇文章 0 订阅

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.



唉QAQ差那么一点点就可以搞出来了,当时题意理解的有点偏差,一直wa在了14组。是q等于0的时候,b1的绝对值大于l的时候,当b1的绝对值大于l的时候就应该直接终端掉,不应该管后面的0的。

题意告诉你b1,q,l,m然后b1和q是一个等比序列的首项和公比,然后l是这个序列出现的数绝对值的最大值。m代表序列有m个数不能被计算在内。问你能写出的最多的序列的元素个数。如果有无限多个输出inf。


分析一下,首先就是无限多个出现的情况。

1.b1==0。此时的序列是无限个0,只要0不在m个数中就是inf,否则就是0。

2.q==0。此时的序列就是b1 +无限个0。首先就是判断一下b1的绝对值是不是大于l,是就输出0.

不是的话就看一看0是不是出现在m个数中,如果没有,就输出inf,否则看一下b1是不是出现在m个数中,如果没有输出1,否则输出0。

3.还有就是q的绝对值等于1的时候,可能出现无限个。

q==1数列就是无限个b1,只要b1不在m个数中就是inf,否则就是0。

q==-1数列就是无限个b1和-b1,看一下b1和-b1是不是出现在m个数中,如果都有就是0,否则就是inf。

4.剩下的情况就没有无限个出现了。

这时候直接从b1不断乘q得到序列的值判断有没有出现在m个数中然后累加就可以了。



我的就是少了第二种情况中开始b1的绝对值大于l的情况,这时候应该直接中断,一直理解成跳过了,QAQ

#include <bits/stdc++.h>

using namespace std;
const int MAXN=1e5+7;

long long b1,q,l,m;
map<long long,bool>vis;
int main()
{
    scanf("%I64d%I64d%I64d%I64d",&b1,&q,&l,&m);
    long long x;
    for(int i=0; i<m; ++i)
    {
        scanf("%I64d",&x);
        vis[x]=1;
    }

    if(!b1)
    {
        if(!vis[0])puts("inf");
        else puts("0");
    }
    else if(!q)
    {
        if(abs(b1)>l)puts("0");
        else
        {
            if(!vis[0])puts("inf");
            else
            {
                if(!vis[b1])puts("1");
                else puts("0");
            }
        }
    }
    else if(abs(b1)<=l&&abs(q)==1)
    {
        if(q==1)
        {
            if(!vis[b1])puts("inf");
            else puts("0");
        }
        else
        {
            if(vis[b1]&&vis[-b1])puts("0");
            else puts("inf");
        }
    }
    else
    {
        long long sum=0;
        long long t=b1;
        while(abs(t)<=l)
        {
            if(!vis[t])sum++;
            t*=q;
        }
        printf("%I64d\n",sum);
    }

    return 0;
}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值