hdu 3003-Pupu-快速幂取模

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=3003

Pupu

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


 

Problem Description

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

 

 

Source

2009 Multi-University Training Contest 11 - Host by HRBEU

 

 

Recommend

gaojie   |   We have carefully selected several similar problems for you:  3001 3004 3002 3007 3006 

 

 题意:

一种动物n层皮,开始时全部不透明,透明的皮阳光照射一天后变成不透明,不透明的变成透明,成年的标志是所有都变成透明过一次,不是全部变成透明。问最少多少天成年。

注:开始第一天没有是全部不透明的,没有照射。

结果对n取模

思路:

n--天数  规律      结果

1---2    2^0+1      0

2---3    2^1+1       1

3---5    2^2+1        2

4---9    2^3+1       1

规律就是(2^{n-1}+1)\%n

注意:一入long long 深似海

This is the code

#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define pppp cout<<endl;
#define EPS 1e-8
#define LL long long
#define ULL unsigned long long     //1844674407370955161
#define INT_INF 0x3f3f3f3f      //1061109567
#define LL_INF 0x3f3f3f3f3f3f3f3f //4557430888798830399
// ios::sync_with_stdio(false);
// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了。
const int dr[]={0, 0, -1, 1, -1, -1, 1, 1};
const int dc[]={-1, 1, 0, 0, -1, 1, -1, 1};
inline int read()//输入外挂
{
    int ret=0, flag=0;
    char ch;
    if((ch=getchar())=='-')
        flag=1;
    else if(ch>='0'&&ch<='9')
        ret = ch - '0';
    while((ch=getchar())>='0'&&ch<='9')
        ret=ret*10+(ch-'0');
    return flag ? -ret : ret;
}
LL quick_mod(LL x,LL n,LL mod)
{
    LL ret=1;
    while(n)
    {
        if(n&1)
            ret=ret*x%mod;
        x=x*x%mod;
        n>>=1;
    }
    return ret;
}
int main()
{
    //freopen("D:\\chnegxubianji\\inORout\\in.txt", "r", stdin);
    //freopen("D:\\chnegxubianji\\inORout\\out.txt", "w", stdout);
    LL n;
    while(scanf("%lld",&n)&&n)
    {
        printf("%lld\n",(quick_mod(2,n-1,n)+1)%n);
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值