• 【codeforces 789B】Masha and geometric depression


    【题目链接】:http://codeforces.com/contest/789/problem/B

    【题意】

    让你一个一个地写出等比数列的每一项
    (注意是一个一个地写出);
    有m个数字不能写;
    且数字大于l的不写(大于l了就停止不再继续写)

    【题解】

    特判b1=0,q=0,q=1,q=-1的情况就好;
    有个很坑的地方就是,如果b1>l了,那么b2..bn就不能再计算了,因为在b1处就已经停止了;->在q=0的时候要先判断b1是不是大于l,是大于l就不用再考虑后面的0了.
    具体的看代码吧.

    【完整代码】

    #include <bits/stdc++.h>
    using namespace std;
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define LL long long
    #define rep1(i,a,b) for (int i = a;i <= b;i++)
    #define rep2(i,a,b) for (int i = a;i >= b;i--)
    #define mp make_pair
    #define pb push_back
    #define fi first
    #define se second
    #define rei(x) scanf("%d",&x)
    #define rel(x) scanf("%lld",&x)
    #define ref(x) scanf("%lf",&x)
    
    typedef pair<int, int> pii;
    typedef pair<LL, LL> pll;
    
    const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
    const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
    const double pi = acos(-1.0);
    const int N = 110;
    
    LL b1, q, l, m, ans = 0;
    map <int, int> dic;
    
    void in()
    {
        rel(b1), rel(q), rel(l), rel(m);
        rep1(i, 1, m)
        {
            int x;
            rei(x);
            dic[x] = 1;
        }
    }
    
    void out()
    {
        exit(0);
    }
    
    void special()
    {
        if (b1 == 0)
        {
            if (dic[0])
                puts("0");
            else
                puts("inf");
            out();
        }
        if (q == 0)//b1!=0
        {
            int ju1 = dic[b1], ju2 = dic[0];
            if (abs(b1) > l)//如果第一个数字就是大于l的,后面的0不能算
            {
                puts("0");
                out();
            }
            if (ju2 == 0)
            {
                puts("inf");
            }
            else//不能写0
            {
                if (abs(b1) <= l)//第一个数字在l范围内
                {
                    if (ju1 == 0)
                        puts("1");
                    else
                        puts("0");
                }
                else//不在l范围内,第一个数字也不能写
                {
                    puts("0");
                }
            }
            out();
        }
        if (q == 1)
        {
            //b1已经确定不会为0了
            if (abs(b1) <= l)
            {
                if (dic[b1])
                {
                    puts("0");
                }
                else
                    puts("inf");
            }
            else//不在l范围内,直接不能输出
                puts("0");
            out();
        }
        if (q == -1)
        {
            //b1 和 -b1交替出现
            //b1确定不会为0了
            if (abs(b1) <= l)
            {
                if (dic[b1] && dic[-b1])
                {
                    puts("0");
                }
                else
                    puts("inf");
            }
            else
                puts("0");
            out();
        }
    }
    
    void get_ans()
    {
        LL temp = b1;
    
        while (abs(temp) <= l)
        {
            if (!dic[temp])
            {
                ans++;
            }
            temp = temp*q;
        }
    }
    
    void o()
    {
        cout << ans << endl;
    }
    
    int main()
    {
        //freopen("F:\rush.txt", "r", stdin);
        in();//checked
        special();//checked
        get_ans();//checked
        o();
        //printf("
    %.2lf sec 
    ", (double)clock() / CLOCKS_PER_SEC);
        return 0;
    }
  • 相关阅读:
    88、使用tensorboard进行可视化学习,查看具体使用时间,训练轮数,使用内存大小
    88、展示Tensorflow计算图上每个节点的基本信息以及运行时消耗的时间和空间
    关于实时监听输入框的值变化
    再谈javascript函数节流
    HTML5离线缓存Manifest
    javascript判断浏览器支持CSS3属性
    关于移动web开发过程中的”点透“问题
    跨域解决方案之HTML5 postMessage
    最精简的金额格式化
    Grunt usemin前端自动化打包流程
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626515.html
Copyright © 2020-2023  润新知