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 输 ...
随机推荐
- 如何用VSCode愉快的写Python
在学习Python的过程中,一直没有找到比较趁手的第三方编辑器,用的最多的还是Python自带的编辑器.由于本人用惯了宇宙第一IDE(Visual Studio),所以当Visual Studio C ...
- ABP前端使用阿里云angular2 UI框架NG-ZORRO分享
一.前言 前段时间写博客分享和介绍了阿里云的UI框架NG-ZORRO(博客请查看:http://www.cnblogs.com/donaldtdz/p/7892960.html),结合近段时间对.Ne ...
- [数据结构]C语言栈的实现
有始有终,所以我准备把各种数据结构都讲一次,栈也分顺序存储和链式储存,这里我们选择链式存储来讲,顺序存储没有难度(链式其实也是) 作为数据结构中最简单的栈,这里不会说太多,首先考虑一下下面的model ...
- Equilibrium point
Given an array A your task is to tell at which position the equilibrium first occurs in the array. E ...
- 发布 Google Chrome插件教程
换个视角,世界不一样.嘘~~~ 如果你会使用js的话,那么你就可以自己动手写一个chrome插件,而且非常容易.google是一个全球化的平台,想想自己的程序被世界人民所使用,是不是很激动? 注册开发 ...
- go generate 生成代码
今后一段时间要研究下go generate,在官网博客上看了Rob Pike写的generating code,花了一些时间翻译了下.有几个句子翻译的是否正确有待考量,欢迎指正. 生成代码 通用计算的 ...
- jmeter中一次运行多条sql语句
操作比较简单,主要就分两步: 第一步:在JDBC Connection Configuration中设置,主要见下图标注部分增加:?allowMultiQueries=true 第二步:在JDBC R ...
- Webpack 2 视频教程 013 - 自动分离 CSS 到独立文件
原文发表于我的技术博客 这是我免费发布的高质量超清「Webpack 2 视频教程」. Webpack 作为目前前端开发必备的框架,Webpack 发布了 2.0 版本,此视频就是基于 2.0 的版本讲 ...
- Ubuntu16.04下编译安装OpenCV3.4.0(C++ & python)
Ubuntu16.04下编译安装OpenCV3.4.0(C++ & python) 前提是已经安装了python2,python3 1)安装各种依赖库 sudo apt-get update ...
- centos 系统日志
Linux系统日志主要有三类:连接时间日志.进程统计日志和错误日志 连接时间日志 连接时间日志由多个程序执行,把记录写入到/var/og/wtmp和/var/run/utmp.ogin等程序更新wtm ...