HDU,hdukmp

來源:互聯網
上載者:User

HDU,hdukmp

Pupu Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1133    Accepted Submission(s): 445


Problem DescriptionThere 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? 
InputThere are many testcase, each testcase only contains one integer N, the number of skins, process until N equals 0 
OutputMaybe 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
230
 
Sample Output
12
 
Source2009 Multi-University Training Contest 11 - Host by HRBEU 



題意:PuPu有n層皮膚,每層皮膚都有2種狀態:透明和不透明,每層皮膚如果能被太陽照射到,則被太陽照射一天后都會變換狀態。

PuPu在出生的時候,所有的皮膚都為不透明的,直到每一層的皮膚都有過變為透明狀態的時候,PuPu也就長大了。

問有n層皮膚的PuPu幾天后能長大。


思路:可以推出公式  ans = (2^(n-1) + 1)%n , 然後按照快速冪模數算出來即可(沒用LL又WA了一次!)


AC代碼:


#include <cstdio>#include <cstring>#include <cstdlib>#include <algorithm>#include <cmath>#define LL long longusing namespace std;LL qmod(LL n){LL q = 2, mo = n, ans = 1;n--;while(n){if(n&1) ans = (ans * q) % mo;q = (q%mo*q%mo)%mo;n>>=1;}return (ans+1)%mo;}int main(){LL n;while(scanf("%I64d", &n), n){printf("%I64d\n", qmod(n));}return 0;} 


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.