HDU3411 Snail Alice 遞推+矩陣快速冪

題意:給定公式求和(題面基本是廢話)

首先求公式的通式,然後進行矩陣快速冪

這題就是求f(n),然後q=x1^y1+z1,n=2^y2+z2。所以q和n都很大。

因爲q=x1^y1+z1,n-1=2^y2-1+z2,q和n可以非常大,爆long long,所以要用到快速冪的思想變形

在進行快速冪時可以發現,進行的是移位運算,對於指數某一位是1的,就讓答案和底數相乘一次
例如2^10, 10的二進制表示 1010 ,n=2,m=10,b=1;

1、(0&1==0)  m=m>>1=101,n=n*n=4
2、(1&1==1)   b=b*n=4,m=m>>1=10,n=n*n=16 
3、(0&1==0)  m=m>>1=1,n=n*n=16*16  
4、(1&1==1)  b=b*n=4*16*16 m=m>>1=0 n=n*n; break;

因此 對於求b^(2^n-1) 只需要求n次模擬快速冪即可。
僞代碼 
a=1
for(i=0;i<n;i++)
{
     a=a*b;
     b=b*b;   
}
根據快速冪模擬y2次循環的矩陣快速冪,然後再進行z2次矩陣快速冪即可
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=2;
int mod;

struct matrix
{
    ll mat[N][N];
    matrix operator *(const matrix &b)
    {
        matrix tp;
        memset(tp.mat,0,sizeof(tp.mat));
        for(int i=0;i<N;++i)
            for(int j=0;j<N;++j)
            for(int k=0;k<N;++k)
        {
            tp.mat[i][j]+=mat[i][k]*b.mat[k][j];
            tp.mat[i][j]%=mod;
        }
        return tp;
    }
};

ll power(ll x,ll n)
{
    if(n==0) return 1%mod;
    ll tp=power(x,n>>1);
    tp=tp*tp%mod;
    if(n&1) tp=tp*x%mod;
    return tp;
}

int main()
{
    int x1,y1,z1,y2,z2;
    ll n,q;
    while(scanf("%d%d%d",&x1,&y1,&z1)&&((x1!=-1)||(y1!=-1)||(z1!=-1)))
    {
        scanf("%d%d%d",&y2,&z2,&mod);
        q=(power(x1,y1)+z1)%mod;
        matrix base={q-1,q,1,0},a={1,0,0,1};
        matrix b=base;
        for(int i=0;i<y2;++i)
        {
            a=a*b;
            b=b*b;
        }
        while(z2)
        {
            if(z2&1) a=a*base;
            base=base*base;
            z2>>=1;
        }
        printf("%lld\n",a.mat[0][0]);
    }
}


題目:
Snail Alice is a snail indulged in math. One day, when she was walking on the grass, suddenly an accident happened. Snail Alice fell into a bottomless hole, which was deep enough that she kept falling for a very long time. In the end, she caught the lateral wall of the hole and stop falling down. She named the place where she stopped “lucky place” immediately. 

Snail Alice decided to climb up along the wall from the “lucky place”. The first day she climbed up q0 (q is a positive constant integer) metres, but at night when she fell asleep, she fell down q1 metres. She was shocked when she woke up, and she decided to make an extra effort. The second day she finally climbed up q2 metres. To her surprise, she fell down faster because of the tiredness. She fell down q3 metres at night. The longer she climbed up the longer she fell down. But finally, she still climbed out of the hole and slept on the ground. 

Lying on the grass safe, she was curious about a question. How many metres was the “lucky place” down under the ground? She remembered that the sum of the times of her climbing up and falling down is n(of course, n is odd), so the distance between the ground and the “lucky place” must be 1-q+q2-q3+...+(-1)n-1qn-1. Snail Alice simplified that long formula and get a beautiful result: (qn+1)/(q+1). But as a math professor, she wouldn’t stop. She came up with a good problem to test her students. Here is the problem: 

A function f(n), n is a positive integer, and 

Given q and n, please calculate the value of f(n).Please note that q and n could be huge.
InputInput consists of multiple test cases, and ends with a line of “-1 -1 -1”. 
For each test case: 
The first line contains three integers x1, y1 and z1, representing q. q=x1^y1+z1. 
The second line contains two integers: y2 and z2, representing n. n=2^y2+z2. 
The third line contains a single integer P, meaning that what you really should output is the formula’s value mod P. 
Note: 0<=x1,y1,z1,y2,z2<=50000, z1>0, 0<P<100000000 OutputFor each test case, print one line containing an integer which equals to the formula’s value mod P. Sample Input
2 1 3
0 0
32551
3 0 5
0 2	
70546
-1 -1 -1
Sample Output
1
31




發佈了45 篇原創文章 · 獲贊 3 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章