Pupu

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1445 Accepted Submission(s): 557

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

题意:PuPu有n层皮肤,每层皮肤都有2中状态:透明和不透明,每层皮肤如果能被太阳照射到,则被太阳照射一天后都会变换状态。PuPu在出生的时候,所有的皮肤都为不透明的,直到每一层的皮肤都有过变为透明状态的时候,PuPu也就长大了。问有n层皮肤的PuPu几天后能长大。

思路:假设当第n层要变透明是状态用二进制表示为1000000,此时的天数为二进制所表示的数2^(n-1),当变透明是天数为2^(n-1)+1;

#include <set>
#include <map>
#include <list>
#include <stack>
#include <cmath>
#include <vector>
#include <queue>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define PI cos(-1.0)
#define RR freopen("input.txt","r",stdin) using namespace std; typedef long long LL; const int MAX = 1e5; LL n; LL power(LL m)
{
LL ans=1;
LL p=2;
while(m)
{
if(m%2)
{
ans=(ans*p)%n;
}
m/=2;
p=(p*p)%n;
}
return ans;
} int main()
{
while(scanf("%I64d",&n)&&n)
{
LL ans=power(n-1)%n;
printf("%I64d\n",ans+1); }
return 0;
}

Pupu(快速幂取模)的更多相关文章

  1. 【转】C语言快速幂取模算法小结

    (转自:http://www.jb51.net/article/54947.htm) 本文实例汇总了C语言实现的快速幂取模算法,是比较常见的算法.分享给大家供大家参考之用.具体如下: 首先,所谓的快速 ...

  2. HDU 1061 Rightmost Digit --- 快速幂取模

    HDU 1061 题目大意:给定数字n(1<=n<=1,000,000,000),求n^n%10的结果 解题思路:首先n可以很大,直接累积n^n再求模肯定是不可取的, 因为会超出数据范围, ...

  3. UVa 11582 (快速幂取模) Colossal Fibonacci Numbers!

    题意: 斐波那契数列f(0) = 0, f(1) = 1, f(n+2) = f(n+1) + f(n) (n ≥ 0) 输入a.b.n,求f(ab)%n 分析: 构造一个新数列F(i) = f(i) ...

  4. POJ3641-Pseudoprime numbers(快速幂取模)

    题目大意 判断一个数是否是伪素数 题解 赤果果的快速幂取模.... 代码: #include<iostream> #include<cmath> using namespace ...

  5. 九度OJ 1085 求root(N, k) -- 二分求幂及快速幂取模

    题目地址:http://ac.jobdu.com/problem.php?pid=1085 题目描述: N<k时,root(N,k) = N,否则,root(N,k) = root(N',k). ...

  6. HDU--杭电--4506--小明系列故事——师兄帮帮忙--快速幂取模

    小明系列故事——师兄帮帮忙 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) To ...

  7. CodeForces Round #191 (327C) - Magic Five 等比数列求和的快速幂取模

    很久以前做过此类问题..就因为太久了..这题想了很久想不出..卡在推出等比的求和公式,有除法运算,无法快速幂取模... 看到了 http://blog.csdn.net/yangshuolll/art ...

  8. HDU1013,1163 ,2035九余数定理 快速幂取模

    1.HDU1013求一个positive integer的digital root,即不停的求数位和,直到数位和为一位数即为数根. 一开始,以为integer嘛,指整型就行吧= =(too young ...

  9. Powmod快速幂取模

    快速幂取模算法详解 1.大数模幂运算的缺陷: 快速幂取模算法的引入是从大数的小数取模的朴素算法的局限性所提出的,在朴素的方法中我们计算一个数比如5^1003%31是非常消耗我们的计算资源的,在整个计算 ...

随机推荐

  1. 利用JAVA计算TFIDF和Cosine相似度-学习版本

    写在前面的话,既然是学习版本,那么就不是一个好用的工程实现版本,整套代码全部使用List进行匹配效率可想而知. [原文转自]:http://computergodzilla.blogspot.com/ ...

  2. codevs 1206 保留两位小数

    http://codevs.cn/problem/1206/ 1206 保留两位小数  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 青铜 Bronze 题解  查看运行结果 ...

  3. [原创]java WEB学习笔记70:Struts2 学习之路-- struts2拦截器源码分析,运行流程

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  4. Java基础(55):Exception类详解(转)

    Java中的异常 Exception java.lang.Exception类是Java中所有异常的直接或间接父类.即Exception类是所有异常的根类. 比如程序: public class Ex ...

  5. Java之Structs框架初探

    今天是小白第一次自己的接触Struts框架,因为网上的资料都是从Structs2开始,跟Structs1完全不同,因此,小白直接跳过1学习版本2的搭建,废话不多说,直接开始. 首先要搭建框架,就肯定要 ...

  6. 0422 数学口袋精灵app

    首先要部署这个app项目就是第一步: 一.前提下载并安装JDK 在线图解:手把手教你安装JDK      http://www.lvtao.net/server/windows-setup-jdk.h ...

  7. YbRapidSolution.Mvc判断不同用户登录不同页面

    AccountController.cs using System; using System.Collections.Generic; using System.Linq; using System ...

  8. linux系统-代码行数计算

    find macc-cometd -type f -name "*.java" -print0 | xargs -0 wc -l

  9. springmvcの神总结のreadme

    ********李守宏springmvc******** 3.== --\springmvc一个controller实现多个方法 ----\继承MultiActionController ----\配 ...

  10. 广告点击率 CTR预估中GBDT与LR融合方案

    http://www.cbdio.com/BigData/2015-08/27/content_3750170.htm 1.背景 CTR预估,广告点击率(Click-Through Rate Pred ...