Pupu

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 913    Accepted Submission(s): 385

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
 
o(︶︿︶)o 唉,好不容易想到方法的,,,居然超时了,
 
思路:我们可以将此题用二进制的思想来解题。0代表不透明,1代表透明。
2层: (0 0)->(1 0)->(0 1)三天
3层:(0 0 0)->(1 0 0)-> (0 1 0)->(1 1 0)->(0 0 1) 五天
所以题意就转变为求2的n-1次方%n。
可是刚刚开始做的时候超时了,要对2的n-1次方这个步骤进行优化,就过了- -||
 
/*超时。。。。呜呜
#include <stdio.h>
int main()
{
__int64 n,m,i,j,a;
while(scanf("%I64d",&n),n)
{
m=n-1;
a=2;
for(i=1;i<m;)
{
if(i*2<=m)
{
a=a*a%n;
i=i*2;
}
else
{
a=a*2%n;
i++;
}
}
printf("%I64d\n",(a+1)%n);
}
return 0;
}*/

优化后的代码。。。

#include<stdio.h>
int main()
{
__int64 n,m,i,k,a,bit[];
while(scanf("%I64d",&n),n)
{
a=;
k=;
m=n-;
while(m)
{
bit[k++]=m%;//判断奇偶
m=m>>;//m/=2;
}
for(i=k-;i>=;i--)
{
a=a*a%n;
if(bit[i]==)
a=a*%n;
}
printf("%d\n",(a+)%n);
}
return ;
}

Pupu(hdu3003)数论的更多相关文章

  1. Codeforces Round #382 Div. 2【数论】

    C. Tennis Championship(递推,斐波那契) 题意:n个人比赛,淘汰制,要求进行比赛双方的胜场数之差小于等于1.问冠军最多能打多少场比赛.题解:因为n太大,感觉是个构造.写写小数据, ...

  2. NOIP2014 uoj20解方程 数论(同余)

    又是数论题 Q&A Q:你TM做数论上瘾了吗 A:没办法我数论太差了,得多练(shui)啊 题意 题目描述 已知多项式方程: a0+a1x+a2x^2+..+anx^n=0 求这个方程在[1, ...

  3. 数论学习笔记之解线性方程 a*x + b*y = gcd(a,b)

    ~>>_<<~ 咳咳!!!今天写此笔记,以防他日老年痴呆后不会解方程了!!! Begin ! ~1~, 首先呢,就看到了一个 gcd(a,b),这是什么鬼玩意呢?什么鬼玩意并不 ...

  4. hdu 1299 Diophantus of Alexandria (数论)

    Diophantus of Alexandria Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  5. 【BZOJ-4522】密钥破解 数论 + 模拟 ( Pollard_Rho分解 + Exgcd求逆元 + 快速幂 + 快速乘)

    4522: [Cqoi2016]密钥破解 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 290  Solved: 148[Submit][Status ...

  6. bzoj2219: 数论之神

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...

  7. hdu5072 Coprime (2014鞍山区域赛C题)(数论)

    http://acm.hdu.edu.cn/showproblem.php?pid=5072 题意:给出N个数,求有多少个三元组,满足三个数全部两两互质或全部两两不互质. 题解: http://dty ...

  8. ACM: POJ 1061 青蛙的约会 -数论专题-扩展欧几里德

    POJ 1061 青蛙的约会 Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & %llu  Descr ...

  9. 数论初步(费马小定理) - Happy 2004

    Description Consider a positive integer X,and let S be the sum of all positive integer divisors of 2 ...

随机推荐

  1. supervisor错误记录

    今天在使用supervisor配置多个程序时遇到了问题如下 FATAL     Exited too quickly (process log may have details) 然后就找到了日志文件 ...

  2. SQL注入之PHP-MySQL实现手工注入-数字型

    SQL注入,就是通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令.具体来说,它是利用现有应用程序,将(恶意的)SQL命令注入到后台数据库引擎 ...

  3. Codeforces 1058 D. Vasya and Triangle(分解因子)

    题目:http://codeforces.com/contest/1058/problem/D 题意:有一个大小为N*M的矩阵内,构造一个三角形,使面积为(n*m)/k.若存在输出三个顶点(整数). ...

  4. Fusioncharts的数字格式化

      1.     小数点位数格式化 <chart ... decimals='2' > Eg.数值12.432, 13.4 and 13,使用<chart ... decimals= ...

  5. 一文搞懂Java环境,轻松实现Hello World!

    在上篇文章中,我们介绍了Java自学大概的路线.然而纸上得来终觉浅,今天我们教大家写第一个java demo.(ps:什么是demo?Demo的中文含意为“示范",Demo源码可以理解为某种 ...

  6. linux防火墙(三)—— iptables语法之匹配条件

    一.iptables规则的匹配条件类型有三类 1.通用匹配:可直接使用,不依赖于其他条件或扩展,包括网络协议.IP地址.网络接口等条件 2.隐含匹配:要求以特定的协议匹配作为前提,包括端口.TCP标记 ...

  7. Postgres 的 Range 类型

    mysql 不支持 Range 类型 零.介绍 1. 适用场景: a.可以用于实现 是否满足薪资需求 的功能 b.可以用于实现 是否符合上线时间 的功能 一.定义 1.类型范围 Postgres Se ...

  8. python 数据库连接及操作

    Python DB-API使用流程: 引入API模块. 获取与数据库的连接. 执行SQL语句和存储过程. 关闭数据库连接. def mysql_dbtest(): config = { 'host': ...

  9. tkinter的grid布局中合并单元格

    rowspan  合并多行,比如:Label(root,text="table",width=10,height=2,),grid(row=0,column=0,rowspan=2 ...

  10. Vue2.5开发去哪儿网App 第五章笔记 下

    1. 多个元素或组件的过渡 多个元素的过渡: <style> .v-enter,.v-leace-to{ opacity: 0; } .v-enter-active,.v-leave-ac ...