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

http://codeforces.com/problemset/problem/992/C

 

题意:

给你两个数x,k,k代表有k+1个月,x每个月可以增长一倍,增长后的下一个月开始时x有50%几率减1,增长k+1个月后结果是每种情况的总和除以种数。

题目要求的是增长k+1个月后的结果。

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

    3

  6    5

12  11  10  9

|      |     |   |

24    22   20  18

从这个推导我们容易得出最后得到的每种情况构成一个等差数列,公差为2

结果是:(2*n-1)*2k+1

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

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

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <string>
 5 #include <math.h>
 6 #include <algorithm>
 7 #include <vector>
 8 #include <stack>
 9 #include <queue>
10 #include <set>
11 #include <map>
12 #include <sstream>
13 const int INF=0x3f3f3f3f;
14 typedef long long LL;
15 const int mod=1e9+7;
16 //const double PI=acos(-1);
17 #define Bug cout<<"---------------------"<<endl
18 const int maxm=1e6+10;
19 const int maxn=1e5+10;
20 using namespace std;
21 
22 LL POW(LL a,LL b )
23 {
24     LL ans = 1;
25     a = a % mod;
26     while(b) 
27     {
28         if( b % 2 == 1 )
29             ans = ( ans * a ) % mod;
30         a = ( a * a ) % mod;
31         b /= 2;
32     }
33     return ans;
34 }
35 
36 int main()
37 {
38     LL n,k;
39     scanf("%lld %lld",&n,&k);
40     LL ans=0;
41     if(n!=0)
42     {
43         n=n%mod;
44         ans=(((2*n-1)*POW(2,k)+mod)%mod+1+mod)%mod;
45     }
46     printf("%d\n",ans);
47     return 0;
48 } 

猜你喜欢

转载自www.cnblogs.com/jiamian/p/11776382.html