POJ 1091 跳蚤
生活随笔
收集整理的這篇文章主要介紹了
POJ 1091 跳蚤
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
跳蚤
Time Limit: 1000ms Memory Limit: 10000KB This problem will be judged on?PKU. Original ID:?109164-bit integer IO format:?%lld????? Java class name:?Main Z城市居住著很多只跳蚤。在Z城市周六生活頻道有一個娛樂節目。一只跳蚤將被請上一個高空鋼絲的正中央。鋼絲很長,可以看作是無限長。節目主持人會給該跳蚤發一張卡片。卡片上寫有N+1個自然數。其中最后一個是M,而前N個數都不超過M,卡片上允許有相同的數字。跳蚤每次可以從卡片上任意選擇一個自然數S,然后向左,或向右跳S個單位長度。而他最終的任務是跳到距離他左邊一個單位長度的地方,并撿起位于那里的禮物。?
比如當N=2,M=18時,持有卡片(10, 15, 18)的跳蚤,就可以完成任務:他可以先向左跳10個單位長度,然后再連向左跳3次,每次15個單位長度,最后再向右連跳3次,每次18個單位長度。而持有卡片(12, 15, 18)的跳蚤,則怎么也不可能跳到距他左邊一個單位長度的地方。?
當確定N和M后,顯然一共有M^N張不同的卡片。現在的問題是,在這所有的卡片中,有多少張可以完成任務。?
Input
兩個整數N和M(N <= 15 , M <= 100000000)。Output
可以完成任務的卡片數。Sample Input
2 4Sample Output
12Hint
這12張卡片分別是:?(1, 1, 4), (1, 2, 4), (1, 3, 4), (1, 4, 4), (2, 1, 4), (2, 3, 4),?
(3, 1, 4), (3, 2, 4), (3, 3, 4), (3, 4, 4), (4, 1, 4), (4, 3, 4)?
Source
HNOI 2001 解題:容斥原理,先找出n個與m不互質的,然后減去就是了 有擴展歐幾里得的影子 1 #include <iostream> 2 using namespace std; 3 typedef long long LL; 4 const int maxn = 50; 5 LL p[maxn],n,m,ret,tot; 6 LL quickPow(LL base,LL index){ 7 LL ret = 1; 8 while(index){ 9 if(index&1) ret *= base; 10 index >>= 1; 11 base *= base; 12 } 13 return ret; 14 } 15 void init(LL x){ 16 tot = 0; 17 for(int i = 2; i*i <= x; ++i){ 18 if(x%i == 0){ 19 p[tot++] = i; 20 while(x%i == 0) x /= i; 21 } 22 } 23 if(x > 1) p[tot++] = x; 24 } 25 int main(){ 26 ios::sync_with_stdio(false); 27 while(cin>>n>>m){ 28 init(m); 29 for(int i = 1; i < (1<<tot); ++i){ 30 int cnt = 0; 31 LL tmp = 1; 32 for(int j = 0; j < tot; ++j){ 33 if((i>>j)&1){ 34 cnt++; 35 tmp *= p[j]; 36 } 37 } 38 if(cnt&1) ret += quickPow(m/tmp,n); 39 else ret -= quickPow(m/tmp,n); 40 } 41 cout<<(quickPow(m,n) - ret)<<endl; 42 } 43 return 0; 44 } View Code?
轉載于:https://www.cnblogs.com/crackpotisback/p/4850708.html
總結
以上是生活随笔為你收集整理的POJ 1091 跳蚤的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: dp_Pku1887
- 下一篇: 新手学逆向,调试abexcm1过程