zoukankan      html  css  js  c++  java
  • Div--2, 992 C. Nastya and a Wardrobe(彻底理解矩阵快速幂)

    C. 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 modulo 109 + 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.

    思路:题意很简单,求均值,我们看一下样例三

    可以发现一个规律第一个数与最后一个数的和刚好为均值,最后一个数可以用快速幂

    求得,而第一个数可以用矩阵快速幂利用a[i] = 2*a[i-1] - 1;求得

    AC code:

    #include <bits/stdc++.h>
    
    using namespace std;
    
    #define mod 1000000007
    
    typedef long long ll;
    
    struct matrix1{
    	ll s[3][3];
    } q;
    
    struct matrix2 {
    	ll s[3];
    } ans;
    
    ll ksm(ll a,ll b,ll c)
    {
    	c %= mod;
    	while(b)
    	{
    		if(b&1) c = c*a%mod;
    		b >>= 1;
    		a = a*a%mod;
    	}
    	return c;
    }
    
    matrix2 mulmatrix(matrix2 a,matrix1 b)
    {
    	matrix2 res;
    	memset(res.s,0,sizeof(res.s));
    	for (int i = 0;i<3;i++){
    		ll x = 0;
    		for(int j = 0;j<3;j++){
    			x = (x + b.s[i][j]%mod * a.s[j]%mod) % mod;
    		}
    		res.s[i] = x % mod;
    	}
    	return res;
    }
    
    matrix1 mulmatrix2(matrix1 a,matrix1 b)
    {
    	matrix1 res;
    	memset(res.s,0,sizeof(res.s));
    	for (int i = 0;i<3;i++){
    		for (int j = 0;j<3;j++){
    			ll x = 0;
    			for(int k = 0;k<3;k++){
    				x = (x + a.s[i][k]%mod * b.s[k][j]%mod)%mod;
    			}
    			res.s[i][j] = x % mod;
    		}
    	}
    	return res;
    }
    
    matrix2 ksmmatrix(matrix1 a,matrix2 b,ll c)
    {
    	while(c)
    	{
    		if(c&1) b = mulmatrix(b,a);
    		c >>= 1;
    		a = mulmatrix2(a,a);
    	}
    	return b;
    }
    
    int main()
    {
    	ios_base::sync_with_stdio(false); cin.tie(0) ; cout.tie(0);
    	ll x,y;
    	cin>>x>>y;
    	if(x == 0) {
    		cout<< 0 <<endl;
    		return 0;
    	}
    	if(y == 0){
    		cout<< x*2%mod <<endl;
    		return 0;
    	}
    	ll res1 = ksm(2,y,x);
    	x %= mod;
    	ans.s[0] = x;
    	ans.s[2] = -1;
    	q.s[0][0] = 2; q.s[0][2] = 1; q.s[2][2] = 1;
    	ans = ksmmatrix(q,ans,y);
    	cout<< ((res1%mod + ans.s[0]%mod)%mod + mod)%mod <<endl;
    	return 0;
    }
    
  • 相关阅读:
    修改MFC标题栏上的图标
    【转】子窗口刷新父窗口的问题
    水晶报表添加引用
    【转】MetadataType的使用,MVC的Model层数据验证
    poj 1556 The Doors 线段相交判断+最短路
    poj 1269 Intersecting Lines 求直线交点 判断直线平行共线
    string 函数操作
    poj 1066 Treasure Hunt 线段相交判断
    poj 1410 Intersection 线段相交判断
    poj 3347 Kadj Squares 扩大数据化整数
  • 原文地址:https://www.cnblogs.com/Nlifea/p/11745987.html
Copyright © 2011-2022 走看看