hdu3411 Snail Alice

Snail Alice

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


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
 

Source
2010 National Programming Invitational Contest Host by ZSTU

话说,这题做了一天了,表示这个效率真糟糕……
好吧,切入主题,这题反正说白了就是求f(n),然后q=x1^y1+z1,n=2^y2+z2。所以q和n都很大。
其实推完式子很简单:F(n)=(q-1)F(n-1)+(q-2)F(n-2)。
具体过程如下:

之后,因为n是2^y2+z2,相当大,因此可以先减去1,得到2^y2-1+z2。根据矩阵快速幂,对于指数的二进制是1的,需要将底数和答案乘一次,如果二进制是0,则不需要。因此观察2^y2-1,所有位上都是1,因此只需要模拟一遍y2-1位的矩阵快速幂,最后加上z2即可。
下午超时超到疯掉,后来发现,把一些不必要的longlong改成int就可以过了。
代码如下
#include <stdio.h>

typedef struct
{
    long long M[2][2];
}Matrix;

Matrix a,b,c,aa;
long long p;

Matrix Mul(Matrix x,Matrix y)
{
    int i,j,k;
    for (i=0;i<2;i++)
    {
        for (j=0;j<2;j++)
        {
            if (y.M[0][j]==0 && y.M[1][j]==0) continue;
            c.M[i][j]=0;
            for (k=0;k<2;k++)
            {
                if (x.M[i][k]==0 || y.M[k][j]==0) continue;
                c.M[i][j]=c.M[i][j]+x.M[i][k]*y.M[k][j];
            }
            c.M[i][j]%=p;
        }
    }
    return c;
}

long long Fpow(int a,int n)
{
    long long ret=1;
    long long A=a;
    while(n)
    {
        if (n & 1)
        {
            ret=(ret*A)%p;
        }
        A=(A*A)%p;
        n>>=1;
    }
    return ret;
}

int main()
{
    int i,j,x1,y1,z1,y2,z2;
    long long q;
    while(1)
    {
        scanf("%d%d%d",&x1,&y1,&z1);
        if (x1==-1 && y1==-1 && z1==-1) break;
        scanf("%d%d",&y2,&z2);
        scanf("%lld",&p);
        q=(Fpow(x1,y1)+z1)%p;
        a.M[0][0]=(q-1+p)%p;
        a.M[0][1]=q;
        a.M[1][0]=1;
        a.M[1][1]=0;
        b.M[0][0]=1;
        b.M[1][0]=0;
        aa=a;
        while(z2)
        {
            if (z2 & 1)
            {
                b=Mul(aa,b);
            }
            aa=Mul(aa,aa);
            z2>>=1;
        }
        for (i=0;i<y2;i++)
        {
            b=Mul(a,b);
            a=Mul(a,a);
        }
        printf("%lld\n",b.M[0][0]);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了小程序应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值