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. 基于JaCoCo的Android测试覆盖率统计(二)

    > 本文章是我上一篇文章的升级版本,详见地址:https://www.cnblogs.com/xiaoluosun/p/7234606.html ## 为什么要做这个?1. 辛辛苦苦写了几百条测 ...

  2. CentOS7.6源码编译安装PHP 7.3.8

    安装步骤 PHP官网下载链接:https://www.php.net/downloads.php 1. 使用wget命令下载源码安装包 wget https://www.php.net/distrib ...

  3. 阿里P8Java大牛仅用46张图让你弄懂JVM的体系结构与GC调优。

    本PPT从JVM体系结构概述.GC算法.Hotspot内存管理.Hotspot垃圾回收器.调优和监控工具六大方面进行讲述.图文并茂不生枯燥. 此PPT长达46页,全部展示篇幅过长,本文优先分享前十六页 ...

  4. 把Jar包加入windows系统服务

    之前在服务器上不一个Java服务时候,总是开着一堆黑框框,非常不雅,重点是极其容易误关,所以把可执行Jar文件加入Windows系统服务,看起来是个非常不错的选择!(实际上也确实是非常不错的选择) ! ...

  5. SQL Labs刷题补坑记录(less1-less30)

    补坑加1,这几天快速刷一下sqllabs 来巩固下sql注入基础吧,也算是把很久以前没刷的过一遍,do it! 第一部分: LESS1: 直接报错,有回显的注入, http://localhost/s ...

  6. MySQL InnoDB Cluster介绍

    目录 一.MySQL InnoDB Cluster介绍 二.环境准备 三.将MGR节点加入MySQL Cluster 四.问题汇总 五.性能测试 六.个人总结 一.MySQL InnoDB Clust ...

  7. Java相关|Code Review Checklist(Server)

    安全 所有入参均经过校验,包括验证参数数据类型.范围.长度,尽可能采用白名单形式验证所有的输入.对于非法请求,记录WARN log.参考Input Validation Cheat Sheet:前后端 ...

  8. Python 參考網站

    Python 3 Readiness : http://py3readiness.org/ Python Speed Center : https://speed.python.org/ Python ...

  9. React预备知识点

    1.react中的状态提升 react的状态提升就是用户对子组件操作,子组件不改变自己的状态,而是通过自己的props把操作改变的数据传递给父组件,改变父组件的状态,从而改变受父组件控制的所有子组件的 ...

  10. 搭建本地的idea激活服务器

    前言        博主用的是idea这个IDE,因为最近idea官方打击第三方激活服务有些严重,所以我的idea经常处于今天可以用,到了明天就不能用的状态,所以,从idea激活的网站找到了本地的id ...