• 「CF630C」Lucky Numbers


    更好的阅读体验

    Portal

    Portal1: Codeforces

    Portal2: Luogu

    Description

    The numbers of all offices in the new building of the Tax Office of IT City will have lucky numbers.

    Lucky number is a number that consists of digits (7) and (8) only. Find the maximum number of offices in the new building of the Tax Office given that a door-plate can hold a number not longer than (n) digits.

    Input

    The only line of input contains one integer (n (1 le n le 55)) — the maximum length of a number that a door-plate can hold.

    Output

    Output one integer — the maximum number of offices, than can have unique lucky numbers not longer than (n) digits.

    Sample Input

    2
    

    Sample Output

    6
    

    Solution

    题目要我们构造(1 sim n)位由(7, 8)的数的个数。我们先来找一找规律:

    位数为(1)时:有(7, 8),共(2 imes 2 ^ 0 = 2)种;

    位数为(2)时:有(77, 78, 87, 88),共(2 imes 2 ^ 1 = 4)种;

    位数为(3)时:有(777, 778, 787, 788, 877, 878, 887, 888)(2 imes 2 ^ 2 = 8)种;

    (cdots cdots)

    所以,位数是(n)的总个数是(2 imes 2 ^ {n - 1})

    那么位数为(1 sim n)的总个数为

    [egin{aligned} sum^{n}_{i = 1}{2 imes 2 ^ {i - 1}} & = 2 imes sum^{n}_{i = 1}{2 ^ {i - 1}} \\ & = 2 imes (2 ^ {n} - 2) \\ & = 2 ^ {n + 1} - 2end{aligned} ]

    于是就解决了。

    Code

    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    
    using namespace std;
    
    typedef long long LL;
    LL n;
    inline LL power(LL x, LL y) {//求x的y次方
        LL ret = 1;
        for (LL i = 1; i <= y; i++)
            ret *= x;
        return ret;
    }
    int main() {
        scanf("%lld", &n);
        printf("%lld
    ", power(2, n + 1) - 2);//推出来的公式
        return 0;
    }
    
  • 相关阅读:
    Python即时网络爬虫项目启动说明
    Spring Cloud Consul入门
    Selenium用法示例
    云中漫步,做个公众号方便生活、取悦自己
    PhantomJS用法示例
    Python3环境安装PySpider爬虫框架过程
    Python3环境安装Scrapy爬虫框架过程及常见错误
    爬虫扒下 bilibili 视频信息
    Sql优化(一) Merge Join vs. Hash Join vs. Nested Loop
    email.py
  • 原文地址:https://www.cnblogs.com/shenxiaohuang/p/11574546.html
Copyright © 2020-2023  润新知