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. js - ajax中的get和post说明

    转自:http://www.cnblogs.com/hateyoucode/archive/2009/12/09/1620050.html 一.谈Ajax的Get和Post的区别 Get方式:   用 ...

  2. Java基础之线程——派生自Thread类的子类(TryThread)

    控制台程序. 程序总是至少有一个线程,程序开始执行时就会创建这个线程.在普通的Java应用程序中,这个线程从mian()方法的开头启动. 要开始执行线程,可以调用Thread对象的start()方法. ...

  3. Request、Servlet及其子接口

    最近看tomcat源码,这类接口多的有点眩,整理出来看一下.(基于tomcat4) javax.servlet.ServletRequset接口,和org.apache.catalina.Reques ...

  4. 《30天自制操作系统》03_day_学习笔记

    harib00a: 添加的部分从P46开始,制作IPL准备开始从磁盘装载程序了 笔者讲解了软盘的驱动的构造,以及汇编语言读取软盘的方法 MOV AX,0x0820 MOV ES,AX ; 柱面0 ; ...

  5. Java SE series:2. enhance your java basis! [doc chm: jdk6api Chinese reference]

    1. javaee(Web) and Android 2. how to use eclipse and break point debuging in eclipse, as to java web ...

  6. android中的权限(转)

    Android权限系统非常庞大,我们在Android系统中做任何操作都需要首先获取Android系统权限,本文记录了所有的Android权限问题,整理一下分享给大家. 访问登记属性 android.p ...

  7. c#读取INI文件类

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;na ...

  8. 远程mysql服务器无法连接解决方案

    错误现象:Habon被拒绝. 远程服务器无法连接从两个方面看 1.是否能ping通远程服务器 windows下查看防火墙是否已关闭 linux下查看iptables等 2.数据库是否有开用户管理权限 ...

  9. 【SPFA】 最短路计数

    最短路计数 [问题描述]   给出一个N个顶点M条边的无向无权图,顶点编号为1-N.问从顶点1开始,到其他每个点的最短路有几条. [输入格式]   输入第一行包含2个正整数N,M,为图的顶点数与边数. ...

  10. NOIP201205Vigenère密码

                                   NOIP201205Vigenère密码 [问题描述]   16 世纪法国外交家Blaise de Vigenère设计了一种多表密码加密 ...