HDU 3411 Snail Alice

Snail Alice

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 759    Accepted Submission(s): 193


Problem Description
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.
 

Input
Input 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
 

Output
For 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
 

题意:题意比较明显,要求f[n],其中  q=x1^y1+z1.  n=2^y2+z2.  会告诉 x1,y1,z1,y2,z2 以及P的值。观察一下就会发现f(n)实际上是一个等比数列前n项和,n分了奇数偶数,当n是奇数时,就是前n项和,当n是偶数时,是前n项和的相反数。于是现在就该矩阵快速幂上场了。
构造矩阵(1,1)|-q,-q|^(n-1)  答案就是 最后的 f[0][1]+f[1][1] 
                        |0,1  |
看一看n的大小,直接算似乎不太科学,可以注意到n-1=2^y2-1+z2。在进行快速幂时可以发现,进行的是移位运算,对于指数某一位是1的,就让答案和底数相乘一次,因此模拟y2位的矩阵快速幂,然后再进行z2次快速幂即可。例如2^10, 10的二进制表示 1010 
n=2,m=10,b=1;

1、(1&0==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;


因此 对于求q^(2^n) 只需要求n次模拟快速幂在乘上q 即可。
伪代码 
b=1
for(i=0;i<n;i++)
{
     b=b*q;
     q=q*q;   
}
ans=b*q;

#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
typedef __int64 ll;
using namespace std;

struct matrix
{
    ll f[2][2];
};

ll mod;

ll fun1(ll m,ll k)
{
    ll b=1;
    while(k)
    {
        if(k&1)
            b=(b*m)%mod;
        m=(m*m)%mod;
        k>>=1;
    }
    return b;
}

matrix mul(matrix a,matrix b)
{
    matrix c;
    memset(c.f,0,sizeof c.f);

    for(int i=0;i<2;i++)
        for(int j=0;j<2;j++)
        {
            for(int k=0;k<2;k++)
           c.f[i][j]=(c.f[i][j]+a.f[i][k]*b.f[k][j]);

           c.f[i][j]%=mod;
        }

    return c;
}

int main()
{
    int x1,y1,z1,y2,z2;
    ll q;

    while(~scanf("%d %d %d",&x1,&y1,&z1))
    {
        if(x1==-1 && y1==-1 && z1==-1) break;

        scanf("%d %d %I64d",&y2,&z2,&mod);

        q=(fun1(x1,y1)+z1)%mod;

        matrix s,b,a;
        s.f[0][0]=-q;
        s.f[0][1]=-q;
        s.f[1][0]=0;
        s.f[1][1]=1;

        b.f[0][0]=1;//单位阵
        b.f[0][1]=0;
        b.f[1][0]=0;
        b.f[1][1]=1;

        a=s;

        while(z2)
        {
            if(z2&1)
                b=mul(a,b);
            a=mul(a,a);
            z2>>=1;
        }

        for(int i=0;i<y2;i++)
        {
            b=mul(s,b);
            s=mul(s,s);
        }

        ll ans;
        ans=(b.f[0][1]+b.f[1][1])%mod;

        if(ans<0)
            ans=-ans;
        printf("%I64d\n",ans);

    }
    return 0;
}

/*
3 0 1
0 3
70546

3 0 1
0 4
999999

*/










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值