ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Pupu(二分快速幂)

2021-11-17 19:01:57  阅读:137  来源: 互联网

标签:二分 PuPu ll Pupu skins adult skin become 快速


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

思路:

皮肤浑浊与透明的变换与二进制的变换很像。

按照题中描述,若有三张皮,则五天后变成熟。

假设用0表示浑浊,1表示透明

最初为:000

第一天:001

第二天:010

第三天:011

第四天:100

第五天:101

这时候只有最里层和最外层的皮肤变成了透明,但是题目明确表示当它只有三张皮肤时,五天后就会变成熟,因此我的理解是当它最外层和最内层皮肤同时变透明时,它就会变成熟。

所以如果它有n层皮肤,则需要2^(n-1)+1天 二进制表达为  100000...0001

然后根据二分快速幂可以很快得出该题的代码为:

#include<iostream>
using namespace std;
#define ll long long

ll func(ll a,ll b,ll c) {
	ll sum = 1;
	while (b) {
		if (b & 1)sum = sum * a % c;
		a = a * a % c;
		b >>= 1;
	}
    return sum;
}


int main() {
	ll n;
	while (cin >> n && n) {
		cout << func(2, n - 1, n) % n + 1 << endl;
	}


	return 0;
}

欢迎各位大佬指正!!!

标签:二分,PuPu,ll,Pupu,skins,adult,skin,become,快速
来源: https://blog.csdn.net/weixin_54664477/article/details/121384976

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有