CF992C Nastya and a Wardrobe 数学 第四道
1 second
256 megabytes
standard input
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.
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.
In the only line print a single integer — the expected number of dresses Nastya will own one year later modulo 109 + 7.
2 0
4
2 1
7
3 2
21
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 数学 第四道的更多相关文章
- CF992C Nastya and a Wardrobe
我是题面 题意很清晰,这种题,我们当然还是有两种方法来做啦 方法一:找规律 读完题我们来看样例,通过样例一已我们大概可以看出,答案或许是\(n*2^{k+1}\) 肯定不能这么简单对吧,那就来看样例二 ...
- 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 ...
- Codeforces 992C Nastya and a Wardrobe (思维)
<题目链接> 题目大意: 你开始有X个裙子 你有K+1次增长机会 前K次会100%的增长一倍 但是增长后有50%的机会会减少一个 给你X,K(0<=X,K<=1e18), 问你 ...
- Nastya and a Wardrobe CodeForces - 992C(规律)
写一下二叉树 推一下公式就出来了, 注意取模时的输出形式 #include <bits/stdc++.h> #define mem(a, b) memset(a, b, sizeof(a ...
- CodeForces 992C Nastya and a Wardrobe(规律、快速幂)
http://codeforces.com/problemset/problem/992/C 题意: 给你两个数x,k,k代表有k+1个月,x每个月可以增长一倍,增长后的下一个月开始时x有50%几率减 ...
- Codeforces Round #489 (Div. 2) B、C
B. Nastya Studies Informatics time limit per test 1 second memory limit per test 256 megabytes input ...
- ACM思维题训练 Section A
题目地址: 选题为入门的Codeforce div2/div1的C题和D题. 题解: A:CF思维联系–CodeForces -214C (拓扑排序+思维+贪心) B:CF–思维练习-- CodeFo ...
- Codeforces Round #489 (Div. 2)
A. Nastya and an Array time limit per test 1 second memory limit per test 256 megabytes input standa ...
- [Codeforces]Codeforces Round #489 (Div. 2)
Nastya and an Array 输出有几种不同的数字 #pragma comment(linker, "/STACK:102400000,102400000") #ifnd ...
随机推荐
- linux字符设备驱动中内核如何调用驱动入口函数 一点记录
/* 内核如何调用驱动入口函数 ? *//* 答: 使用module_init()函数,module_init()函数定义一个结构体,这个结构体里面有一个函数指针,指向first_drv_init() ...
- zmnXAglTcg
#include <map>#include <cmath>#include <stack>#include <queue>#include <l ...
- Linux基础文件查找
一.文件查找 (一).命令文件 [root@linux ~]# chich ls //从PATH环境变量 [root@linux ~]# chereis vim [root@linux ~]# ech ...
- 90后iOS开发者的出路,如何规划30岁前的自己(程序员必修课)
前言: 最近发生了一些和我们没有直接关系但是有间接关系的事情.比如华为“清洗”高龄基层员工,比如游戏公司2号员工拿不到股份而离职.先不说事实到底如何,起码很多码农是心有戚戚焉. 最近一年多也发生了一些 ...
- 佳木斯集训Day5
今天是ACM赛制...本来可以400的,结果毒瘤T2模拟硬生生卡掉了我90分 T1是个大水题,找规律,5分钟AC没啥压力 #include <bits/stdc++.h> #define ...
- JavaScript数据结构——树的实现
在计算机科学中,树是一种十分重要的数据结构.树被描述为一种分层数据抽象模型,常用来描述数据间的层级关系和组织结构.树也是一种非顺序的数据结构.下图展示了树的定义: 在介绍如何用JavaScript实现 ...
- HTML5 Device Access (设备访问)
camera api (含图片预览) 参考地址 主要为利用input type=file, accept="image/*" 进行处理 图片预览方式(两种) const file ...
- JavaWeb——Filter过滤器
1.Filter的目的 Filter用于在Servlet之前检测和修改请求和响应,它可以拒绝.重定向或转发请求.常见的有这几种: 日志过滤器 使用过滤器记录请求,提供请求日志记录,还可以添加追踪信息用 ...
- 1.1Django简介和虚拟环境配置
MVC 大部分开发语言中都有MVC框架 MVC框架的核心思想是:解耦 降低各功能模块之间的耦合性,方便变更,更容易重构代码,最大程度上实现代码的重用 m表示model,主要用于对数据库层的封装 v表示 ...
- eclipse项目导入idea jdk版本不一致😵
在导入eclipse项目到idea过程中遇到 Imported project refers to unkonwn jdks JavaSE-1.8 解决方法: file --> Project ...