• CF992C Nastya and a Wardrobe 数学 第四道


    Nastya and a Wardrobe
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Nastya received a gift on New Year — a magic wardrobe. It is magic because in the end of each month the number of dresses in it doubles (i.e. the number of dresses becomes twice as large as it is in the beginning of the month).

    Unfortunately, right after the doubling the wardrobe eats one of the dresses (if any) with the 50% probability. It happens every month except the last one in the year.

    Nastya owns x dresses now, so she became interested in the expected number of dresses she will have in one year. Nastya lives in Byteland, so the year lasts for k + 1 months.

    Nastya is really busy, so she wants you to solve this problem. You are the programmer, after all. Also, you should find the answer modulo109 + 7, because it is easy to see that it is always integer.

    Input

    The only line contains two integers x and k (0 ≤ x, k ≤ 1018), where x is the initial number of dresses and k + 1 is the number of months in a year in Byteland.

    Output

    In the only line print a single integer — the expected number of dresses Nastya will own one year later modulo 109 + 7.

    Examples
    input
    Copy
    2 0
    output
    Copy
    4
    input
    Copy
    2 1
    output
    Copy
    7
    input
    Copy
    3 2
    output
    Copy
    21
    Note

    In the first example a year consists on only one month, so the wardrobe does not eat dresses at all.

    In the second example after the first month there are 3 dresses with 50% probability and 4 dresses with 50% probability. Thus, in the end of the year there are 6 dresses with 50% probability and 8 dresses with 50% probability. This way the answer for this test is(6 + 8) / 2 = 7.

     题意: 给你两个数x,k,k代表有k+1个月,x每个月可以增长一倍,增长后的下一个x会先少一再增长一倍,增长k+1个月后x的结果是每种情况的总和除以种数。题目要求的是增长k+1个月后的结果。

    我们根据第三个样例可以推出他的增长过程

        3

      5    6

    9  10  11  12

    |    |   |   |

    18  20  22  24

    从这个推导我们容易得出最后得到的每种情况构成一个等差数列,公差为2,首项为x*(2^(k+1))-2*(2^k - 1),末项为x*(2^(k+1)),个数为2^k

    最后根据等差数列求和的公式可以得到( (2*x-1)*2^k + 1 ) *(2^k),再除以2^k种数,我们可以求出最后的结果(2*x-1)*2^k + 1

    注意特判0的情况,0的时候答案是0,直接输出

    另外求2的次方的时候用快速幂,注意取余

    #include <map>
    #include <set>
    #include <cmath>
    #include <queue>
    #include <cstdio>
    #include <vector>
    #include <string>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #define debug(a) cout << #a << " " << a << endl
    using namespace std;
    const int maxn = 1e3 + 10;
    const int mod = 1e9 + 7;
    typedef long long ll;
    ll qow( ll a, ll b ) {
        ll ans = 1;
        a = a % mod;
        while(b) {
            if( b % 2 == 1 ) {
                ans = ( ans * a ) % mod;
            }
            a = ( a * a ) % mod;
            b /= 2;
        }
        return ans;
    }
    int main(){
        std::ios::sync_with_stdio(false);
        ll x, k;
        while( cin >> x >> k ) {
            if( x == 0 ) {
                cout << 0 << endl;
                continue;
            }
            x = x % mod; //如果开始x很大需先取余,否则在后面取余会出现偏差
            cout << ( ( ( 2 * x - 1 ) * qow( 2, k ) + mod ) % mod + 1 + mod ) % mod << endl;
        } 
        return 0;
    }
    彼时当年少,莫负好时光。
  • 相关阅读:
    【Kubernetes】Service 实现探索
    【Kubernetes】浏览器访问服务
    【Kubernetes】访问入口-Service
    oracle 分页查询
    本地cmd命令打war包、解压war包
    xp系统上安装和使用kettle
    VWware15安装windows XP纯净版虚拟机
    JDK8:HashMap源码解析:TreeNode类的balanceInsertion方法
    红黑树
    kettle JVM内存设置-
  • 原文地址:https://www.cnblogs.com/l609929321/p/9211035.html
Copyright © 2020-2023  润新知