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
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?
3
0
2
某生物成年的标志是身上的所有皮肤都从不透明变成过透明至少一次,不是同时变成透明才算。(= =!)
也就是求最里一层皮肤变成透明的天数(最里一层皮肤要变成透明,必须外面其他所有的皮肤都同时透明才行)。
一开始没理解这里,总是不明白样例。
理解后,就可以推导了。
所以,请分清【前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
下面是采用二分来求取高次幂
//计算(2^(n-1)+1)%n
//快速幂
#include<cstdio>
#include<cstring>
using namespace std;
#define ll long long
ll multi(ll a,ll n,ll m)//¼ÆËãa^n%m
{
ll ans = ;
while(n>){
if(n&) ans = (ans*a)%m;
a = (a*a)%m;
n>>=;
}
return ans;
}
int main()
{
ll n;
while(~scanf("%lld",&n)&&n)
{
printf("%lld\n",(multi((ll),n-,n)+)%n);
}
return ;
}
hdu_3003Pupu(快速幂)的更多相关文章
- 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)
题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ...
- 51nod 算法马拉松18 B 非010串 矩阵快速幂
非010串 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个01字符串满足不存在010这样的子串,那么称它为非010串. 求长度为n的非010串的个数.(对1e9+7取模) ...
- hdu 4704 Sum (整数和分解+快速幂+费马小定理降幂)
题意: 给n(1<n<),求(s1+s2+s3+...+sn)mod(1e9+7).其中si表示n由i个数相加而成的种数,如n=4,则s1=1,s2=3. ...
- Codeforces632E Thief in a Shop(NTT + 快速幂)
题目 Source http://codeforces.com/contest/632/problem/E Description A thief made his way to a shop. As ...
- GDUFE-OJ 1203x的y次方的最后三位数 快速幂
嘿嘿今天学了快速幂也~~ Problem Description: 求x的y次方的最后三位数 . Input: 一个两位数x和一个两位数y. Output: 输出x的y次方的后三位数. Sample ...
- 51nod 1113 矩阵快速幂
题目链接:51nod 1113 矩阵快速幂 模板题,学习下. #include<cstdio> #include<cmath> #include<cstring> ...
- 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】
还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...
- HDU5950(矩阵快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 题意:f(n) = f(n-1) + 2*f(n-2) + n^4,f(1) = a , f(2 ...
- 51nod 1126 矩阵快速幂 水
有一个序列是这样定义的:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. 给出A,B和N,求f(n)的值. Input 输 ...
随机推荐
- ios单独的页面支持横竖屏的状态调整,HTML5加载下(更新2)
单独的页面支持横竖屏的状态调整,HTML5加载下 工程中设置只支持竖屏状态,在加载HTML5的界面可以是横竖屏的,在不对工程其他界面/设置做调整的同时,可以这样去 #import "View ...
- 安装cocoa pods时出现Operation not permitted - /usr/bin/xcodeproj的问题
安装cocoa pods时, 在命令行中输入: 安装:sudo gem install cocoapods报Operation not permitted - /usr/bin/xcodeproj这个 ...
- Python学习日记:day8-------文件操作
文件操作 1,文件路径:d:\xxxx.txt 绝对路径:从根目录到最后 相对路径:当前目录下的文件 2,编码方式:utf-8 3,操作方式:只读,只写,追加,读写,写读...... ...
- Say Hello to ConstraintLayout
ConstraintLayout介绍 ConstraintLayout让你可以在很平的view结构(没有多层布局嵌套)中构建一个复杂的布局结构. 有点像RelativeLayout, 所有的view都 ...
- mvc中signalr实现一对一的聊天
Asp.net MVC中实现即时通讯聊天的功能.前几天刚写了一片基础入门的教程,今天就来实现一下使用signaIr实现一对一的聊天的功能,对于这种场景也是即时通讯最基本功能.好吧废话不多说.先来看一下 ...
- eslint常见规则总结
google: eslint+rules es6: impot When you import the module's default, don't use brace {} 意思是,当你使用默认的 ...
- spring的注解使用
1.注解测试在xml里面配置<context:component-scan base-package="cn.ql"></component-scan>co ...
- 使用ListView控件展示数据
属性名称 说明items 指定显示那种视图View 指定显示那种视图largelmagelist 大图标图像的imagelist控件SmallLmagelist 小图标图像的imag ...
- ABP架构学习系列二:ABP中配置的注册和初始化
一.手工搭建平台 1.创建项目 创建MVC5项目,手动引入Abp.Abp.Web.Abp.Web.Mvc.Abp.Web.Api 使用nuget添加Newtonsoft.Json.Castle.Cor ...
- openldap 编译报错MozNSS not found
openldap 编译报错 1)报错 MozNSS not found - please specify the location to the NSPR and NSS header files i ...