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 + ;
const int mod = 1e9 + ;
typedef long long ll;
ll qow( ll a, ll b ) {
ll ans = ;
a = a % mod;
while(b) {
if( b % == ) {
ans = ( ans * a ) % mod;
}
a = ( a * a ) % mod;
b /= ;
}
return ans;
}
int main(){
std::ios::sync_with_stdio(false);
ll x, k;
while( cin >> x >> k ) {
if( x == ) {
cout << << endl;
continue;
}
x = x % mod; //如果开始x很大需先取余,否则在后面取余会出现偏差
cout << ( ( ( * x - ) * qow( , k ) + mod ) % mod + + mod ) % mod << endl;
}
return ;
}

CF992C Nastya and a Wardrobe 数学 第四道的更多相关文章

  1. CF992C Nastya and a Wardrobe

    我是题面 题意很清晰,这种题,我们当然还是有两种方法来做啦 方法一:找规律 读完题我们来看样例,通过样例一已我们大概可以看出,答案或许是\(n*2^{k+1}\) 肯定不能这么简单对吧,那就来看样例二 ...

  2. CF思维联系– CodeForces -CodeForces - 992C Nastya and a Wardrobe(欧拉降幂+快速幂)

    Nastya received a gift on New Year - a magic wardrobe. It is magic because in the end of each month ...

  3. Codeforces 992C Nastya and a Wardrobe (思维)

    <题目链接> 题目大意: 你开始有X个裙子 你有K+1次增长机会 前K次会100%的增长一倍 但是增长后有50%的机会会减少一个 给你X,K(0<=X,K<=1e18), 问你 ...

  4. Nastya and a Wardrobe CodeForces - 992C(规律)

    写一下二叉树  推一下公式就出来了, 注意取模时的输出形式 #include <bits/stdc++.h> #define mem(a, b) memset(a, b, sizeof(a ...

  5. CodeForces 992C Nastya and a Wardrobe(规律、快速幂)

    http://codeforces.com/problemset/problem/992/C 题意: 给你两个数x,k,k代表有k+1个月,x每个月可以增长一倍,增长后的下一个月开始时x有50%几率减 ...

  6. Codeforces Round #489 (Div. 2) B、C

    B. Nastya Studies Informatics time limit per test 1 second memory limit per test 256 megabytes input ...

  7. ACM思维题训练 Section A

    题目地址: 选题为入门的Codeforce div2/div1的C题和D题. 题解: A:CF思维联系–CodeForces -214C (拓扑排序+思维+贪心) B:CF–思维练习-- CodeFo ...

  8. Codeforces Round #489 (Div. 2)

    A. Nastya and an Array time limit per test 1 second memory limit per test 256 megabytes input standa ...

  9. [Codeforces]Codeforces Round #489 (Div. 2)

    Nastya and an Array 输出有几种不同的数字 #pragma comment(linker, "/STACK:102400000,102400000") #ifnd ...

随机推荐

  1. Android平台使用Ceres Solver

    在Android平台上使用Ceres求解器,官方教程不明确,且编译过程遇到了很多问题. 环境 Ubuntu 18.04 源代码 https://github.com/Great-Keith/ceres ...

  2. SpringBoot:如何优雅地处理全局异常?

    之前用springboot的时候,只知道捕获异常使用try{}catch,一个接口一个try{}catch,这也是大多数开发人员异常处理的常用方式,虽然屡试不爽,但会造成一个问题,就是一个Contro ...

  3. SVN服务器更改ip地址客户端怎么设置

    SVN 服务器 IP 地址修改后,客户端对服务器的连接可以采用以下的方法重定位: 1. 如果客户端工具是TortoiseSVN,直接在工作副本上右键,选择TortoiseSVN->relocat ...

  4. java中File IO流的笔记

    1.File文件的属性和操作 boolean exists( )  判断文件或目录是否存在boolean isFile( )  判断是否是文件boolean isDirectory( ) 判断是否是目 ...

  5. 28岁,转行学 IT 靠谱吗?

    前几天在知乎上,刷到这么一个问题 鉴于有不少人看了我的blog给我私信一些职业规划相关的问题,讨论很多的就是担心自己年龄是否还适合转行. 于是决定静心下来码了一篇回答, 同时搬到博客园来供大家消遣.. ...

  6. 【POJ - 3255】Roadblocks(次短路 Dijkstra算法)

    Roadblocks 直接翻译了 Descriptions Bessie搬到了一个新的农场,有时候他会回去看他的老朋友.但是他不想很快的回去,他喜欢欣赏沿途的风景,所以他会选择次短路,因为她知道一定有 ...

  7. python(自用手册)导图

  8. Kaggle比赛(一)Titanic: Machine Learning from Disaster

    泰坦尼克号幸存预测是本小白接触的第一个Kaggle入门比赛,主要参考了以下两篇教程: https://www.cnblogs.com/star-zhao/p/9801196.html https:// ...

  9. Python day01 课堂笔记

    今天是第一天学习Python课程,主要从计算机基础,Python的历史,环境 ,变量,常量,注释,用户交互,基础数据类型 ,简单的if条件语句和while循环语句这几个来学习,重点的掌握内容是pyth ...

  10. CSS布局:元素水平居中

    CSS布局之元素水平居中 本文将依次介绍在不同条件下实现水平居中多种方法 一.使用 text-align: center : 适用于块级元素内部的行内元素水平居中(也适用于图片的水平居中) 此方法对i ...