hdu_3003Pupu(快速幂)

题目链接: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): 1747    Accepted Submission(s): 688

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

题意:(转)

某生物成年的标志是身上的所有皮肤都从不透明变成过透明至少一次,不是同时变成透明才算。(= =!)

也就是求最里一层皮肤变成透明的天数(最里一层皮肤要变成透明,必须外面其他所有的皮肤都同时透明才行)。

一开始没理解这里,总是不明白样例。

理解后,就可以推导了。

所以,请分清【前n层皮肤同时为透明】和【第n层皮肤变为透明(即前n层皮肤都变透明"过”)】

接下来用total[n]、ans[n}分别表示【前n层皮肤同时为透明的天数】和【第n层皮肤变为透明的天数】

(ans[n]即为题中所求)

(举一个有4层皮肤的pupu为例,+表示不透明,  -表示透明)

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

+                  -            +              -            +            -            +            -               +

+                 +             -              -            +           +            -             -               +

+                 +            +             +            -             -            -             -               +

+                 +            +             +            +           +            +            +               -

有ans[1]=total[1]=2

ans[2]=3  total[2]=4

ans[3]=5 total[3]=8

ans[4]=9

可以看到要使第n层皮肤变透明,必须要前n-1层同时为透明,然后第二天第n层皮肤就会变为透明,同时前n-1层皮肤变成不透明

即有ans[n]=total[n-1]+1----------------------------------------1

而要使前n层皮肤同时为透明,则必须第n层皮肤变成透明,然后前n-1层同时为透明,由于第n层皮肤变成透明的那一天,也就是前n-1层皮肤同时为透明的过程的第一天

即有total[n]=ans[n]+total[n-1]-1---------------------------------------2

由1式代入2式可得total[n]=total[n-1]*2,又total[1]=2

所以total[n]=2^n

所以ans[n]=total[n-1]+1=2^(n-1)+1

下面是采用二分来求取高次幂

 1 //计算(2^(n-1)+1)%n
 2 //快速幂
 3 #include<cstdio>
 4 #include<cstring>
 5 using namespace std;
 6 #define ll long long
 7 ll multi(ll a,ll n,ll m)//¼ÆËãa^n%m
 8 {
 9     ll ans = 1;
10     while(n>0){
11         if(n&1) ans = (ans*a)%m;
12         a = (a*a)%m;
13         n>>=1;
14     }
15     return ans;
16 }
17 int main()
18 {
19     ll n;
20     while(~scanf("%lld",&n)&&n)
21     {
22         printf("%lld\n",(multi((ll)2,n-1,n)+1)%n);
23     }
24     return 0;
25 }
时间: 2024-11-07 05:58:57

hdu_3003Pupu(快速幂)的相关文章

矩阵快速幂刷题系列

来源自http://blog.csdn.net/chenguolinblog/article/details/10309423 hdu 1575 Tr A Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5587    Accepted Submission(s): 4200 Problem Description A为一个方阵,则Tr

快速幂取模(POJ 1995)

http://poj.org/problem?id=1995 以这道题来分析一下快速幂取模 a^b%c(这就是著名的RSA公钥的加密方法),当a,b很大时,直接求解这个问题不太可能 利用公式a*b%c=((a%c)*b)%c 每一步都进行这种处理,这就解决了a^b可能太大存不下的问题,但这个算法的时间复杂度依然没有得到优化 由此可以用快速幂算法优化: http://www.cnblogs.com/qlky/p/5020402.html 再结合取模公式: (a + b) % p = (a % p

HDU 1757 A Simple Math Problem (矩阵快速幂)

[题目链接]:click here~~ [题目大意]: If x < 10 f(x) = x. If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + -- + a9 * f(x-10); 问f(k)%m的值. [思路]:矩阵快速幂,具体思路看代码吧,注意一些细节. 代码: #include<bits/stdc++.h> using namespace std; typedef long long LL; const

快速幂及快速幂取模

快速幂顾名思义,就是快速算某个数的多少次幂.其时间复杂度为 O(log?N), 与朴素的O(N)相比效率有了极大的提高.——bybaidu 快速幂可以用位运算这个强大的工具实现. 代码: 1 int pow(int a,int b) 2 { 3 int ans=1; 4 while(b!=0) 5 { 6 if(b&1) 7 ans*=a; 8 a*=a; 9 b>>=1; 10 } 11 return ans; 12 } 快速幂取模需要记住一个定理:积的取模等于取模积的取模:算法是蒙

Codeforces Round #291 (Div. 2) E - Darth Vader and Tree (DP+矩阵快速幂)

这题想了好长时间,果断没思路..于是搜了一下题解.一看题解上的"快速幂"这俩字,不对..这仨字..犹如醍醐灌顶啊...因为x的范围是10^9,所以当时想的时候果断把dp递推这一方法抛弃了.我怎么就没想到矩阵快速幂呢.......还是太弱了..sad..100*100*100*log(10^9)的复杂度刚刚好. 于是,想到了矩阵快速幂后,一切就变得简单了.就可以把距离<=x的所有距离的点数都通过DP推出来,然后一个快速幂就解决了. 首先DP递推式很容易想到.递推代码如下: for(

NYOJ127 星际之门(一)(最小生成数的个数+快速幂)

题目描述: http://acm.nyist.net/JudgeOnline/problem.php?pid=127 可以证明,修建N-1条虫洞就可以把这N个星系连结起来. 现在,问题来了,皇帝想知道有多少种修建方案可以把这N个星系用N-1条虫洞连结起来? 输入 第一行输入一个整数T,表示测试数据的组数(T<=100) 每组测试数据只有一行,该行只有一个整数N,表示有N个星系.(2<=N<=1000000) 输出 对于每组测试数据输出一个整数,表示满足题意的修建的方案的个数.输出结果可能

POJ 3233 - Matrix Power Series ( 矩阵快速幂 + 二分)

POJ 3233 - Matrix Power Series ( 矩阵快速幂 + 二分) #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long LL; #define MAX_SIZE 30 #define CLR( a, b ) memset( a, b, sizeof(a) ) int MOD = 0; int n, k; st

HDU 4990 Reading comprehension(找规律+矩阵快速幂)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4990 Problem Description Read the program below carefully then answer the question. #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include<iostream> #include

hdu 6198(矩阵快速幂)

number number number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 175    Accepted Submission(s): 119 暴力发现当4 12 33 88 232 和斐波那契数列对比  答案为 第2*k+3个数减1 直接用矩阵快速幂求的F[2*k+3]  然后减1 A=1,B=0; 然后矩阵快速幂2*k