Pupu HDU - 3003 快速幂取模&思维&&打表

There is an island called PiLiPaLa.In the island there is a wild animal living in it, and you can call them PuPu. PuPu is a kind of special animal, infant PuPus play under the sunshine, and adult PuPus hunt near the seaside. They fell happy every day. 

But there is a question, when does an infant PuPu become an adult PuPu? 
Aha, we already said, PuPu is a special animal. There are several skins wraping PuPu's body, and PuPu's skins are special also, they have two states, clarity and opacity. The opacity skin will become clarity skin if it absorbs sunlight a whole day, and sunshine can pass through the clarity skin and shine the inside skin; The clarity skin will become opacity, if it absorbs sunlight a whole day, and opacity skin will keep sunshine out. 

when an infant PuPu was born, all of its skins were opacity, and since the day that all of a PuPu's skins has been changed from opacity to clarity, PuPu is an adult PuPu. 

For example, a PuPu who has only 3 skins will become an adult PuPu after it born 5 days(What a pity! The little guy will sustain the pressure from life only 5 days old) 

Now give you the number of skins belongs to a new-laid PuPu, tell me how many days later it will become an adult PuPu?

Input

There are many testcase, each testcase only contains one integer N, the number of skins, process until N equals 0

Output

Maybe an infant PuPu with 20 skins need a million days to become an adult PuPu, so you should output the result mod N

Sample Input

2
3
0

Sample Output

1
2

题意:这个题的题意蛮有意思的 就是有一种生物 它有很多层皮肤 而且成年的标志是每一层皮肤都变透明过(注意不是同时透明,即最内层透明就可以了)

然后这种生物每天都会晒太阳 晒一天太阳 最外层的不透明皮肤就会变透明(阳光会穿过外层的透明皮肤),同时这一天阳光穿过的透明的皮肤又会变成不透明的 ,然后问你最内层透明的需要晒多少天太阳

题解:解法有两种 1.暴力打表 看一下规律 就会发现ans=2^(n-1)+1;

2.推理一下 设ans[n]为第n层透明的天数total[n]为前n层同时透明的天数

假如四层皮肤

第一天       第二天    第三天    第四天    第五天   第六天   第七天   第八天    第九天

    +                  -            +              -            +            -            +            -               +

    +                 +             -              -            +           +            -             -               +

    +                 +            +             +            -             -            -             -               +

    +                 +            +             +            +           +            +            +               -

首先第n层透明 首先要求前一天前n-1层同时透明 所以ans[n]=total[n-1]+1;

又因为 第n层全部透明 是第n天透明而且前n-1天同时透明 又根据上面的表格total[n]=ans[n]+total[n-1]-1;

合并两个方程解得total[n]=total[n-1]*2; total[1]=1;所以total[n]=2^n;

ans[n]=2^(n-1)+1;

思路借鉴了这里

#include <bits/stdc++.h>
#define maxn 1000+5
using namespace std;
typedef long long int ll;
int quick_powmod(ll a,ll b,ll m){
    a%m;
    ll ans=1;
    while(b)
    {
        if(b&1)
            ans=(ans*a)%m;
        a=(a*a)%m;
        b>>=1;
    }
    return int(ans);
}
int main(){
    ll n;
    while(scanf("%lld",&n)&&n){
        ll ans=quick_powmod(2,n-1,n)+1;
        printf("%d\n",ans);
    }
    return 0;
}

 

全部评论

相关推荐

2025-12-28 16:32
重庆邮电大学 Java
程序员花海:1.技能放最后,来面试默认你都会,技能没啥用 2.实习写的看起来没啥含金量,多读读部门文档,包装下 接LLM这个没含金量 也不要用重构这种 不会给实习生做的 3.抽奖这个还是Demo项目,实际在公司里面要考虑策略,满减,触发点,触发规则 库存 之类的,不是这个项目这么简单 4.教育背景提前,格式为 教育背景 实习 项目 技能 自我评价
简历被挂麻了,求建议
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务