Masha and geometric depression (Codeforces-789B)

题目链接:

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 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

题目大意:

一个等比数列(首项和公比已知),从第一项开始,如果有一项不满足(|bi|<=k),那么等比数列结束(到尽头了)。然后有m个数字,这m个数字是“bad”的数字。题目要求算出这个等比数列中有几个不是“bad”数字,并输出个数。

解题思路:

这道题考察逻辑思维,需要分好多种情况(1:b1==0   2:|b1|>L  3:q==-1   4:q==0  5:q==0)这只是我个人的分类方法,不代表标准答案。想象能力跟不上的话,就在纸上写写画画,不要漏掉任何一个细节。具体怎么分情况,请看代码。

代码:

#include<iostream>
#include<map>
#include<cmath>
using namespace std;
int main()
{
    long long int b1,q,l,s,m;
    while(cin>>b1>>q>>l>>m)
    {
        int sum=0;
        map<int,int>M;
        for(int i=0;i<=m-1;i++)
        {
            cin>>s;
            M[s]++;   //是bad数字的话,把bad数字对应的map值等于1,起到标记的作用
        }
        if(abs(b1)>l)   //说明这个等比数列为空的,直接输出0即可
        {
            cout<<0<<endl;
            continue;
        }
        if(q==0)   //如果公比等于0
        {
            if(b1==0)   //首项=0并且公比=0
            {
                if(M[0]==0)   //并且0不是bad数字,那么说明无限个0都满足题意
                    cout<<"inf"<<endl;
                if(M[0]!=0)   //如果0是bad数字,那么这个等不数列没有一项符合题意
                    cout<<0<<endl;
            }
            else   //首项不等于0
            {
                if(abs(b1)<=l)   //首项!=0 且 首项的绝对值满足<=L
                {
                    if(M[b1]==0)   //首项不是bad数字
                    {
                        if(M[0]==0)   //0不是bad数字,那么数列有无限个0满足题意
                            cout<<"inf"<<endl;
                        else   //0是bad数字,那么只有首项符合题意
                            cout<<1<<endl;
                    }
                    else
                    {
                        if(M[0]==0)   //首项是bad数字且0不是bad数字,那么数列有无限个0满足题意
                            cout<<"inf"<<endl;
                        else   //首项是bad数字,0是bad数字,那么序列没有一个满足题意
                            cout<<0<<endl;
                    }
                }
                else   //首项不满足绝对值<=L,说明数列为0项,更不用说是不是bad数字了,因为就没有数字,直接输出0
                    cout<<0<<endl;
            }
            continue;
        }
        if(q==1)
        {
            if(M[b1]==0)   //公比为1,说明这个数列的每一项都等于b1,且b1不是bad数字,那么无限的项满足题意
                cout<<"inf"<<endl;
            if(M[b1]==1)   //公比为1,说明这个数列的每一项都等于b1,且b1是bad数字,那么都不满足题意
                cout<<0<<endl;
            continue;
        }
        if(q==-1)   //公比为-1,那么这个数列只有b1,-b1,只用判断b1,-b1是不是bad十字即可
        {
            if(M[b1]==0||M[-b1]==0)
                cout<<"inf"<<endl;
            if(M[b1]==1&&M[-b1]==1)
                cout<<0<<endl;
            continue;
        }
        if(b1==0)   //首项为0,说明每一项都是0,只用判断0是不是bad数字即可
        {
            if(M[0]==0)
                cout<<"inf"<<endl;
            else
                cout<<0<<endl;
            continue;
        }
        long long int bn=b1;
        for(int i=0;;i++)
        {
            if(abs(bn)>l)   //判断bn是否满足题意,不满足的话说明数列已经结束(这一项包括后面的所有项都不满足题意了
                break;
            if(M[bn]==0)   //如果该数字不是bad数字,sum++
                sum++;
            bn=bn*q;   //计算下一项
        }
        cout<<sum<<endl;
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值