hdu 4990 Reading comprehension 二分 + 快速幂

Description

Read the program below carefully then answer the question. 
#pragma comment(linker, "/STACK:1024000000,1024000000") 
#include <cstdio> 
#include<iostream> 
#include <cstring> 
#include <cmath> 
#include <algorithm> 
#include<vector>

const int MAX=100000*2; 
const int INF=1e9;

int main() 

  int n,m,ans,i; 
  while(scanf("%d%d",&n,&m)!=EOF) 
  { 
    ans=0; 
    for(i=1;i<=n;i++) 
    { 
      if(i&1)ans=(ans*2+1)%m; 
      else ans=ans*2%m; 
    } 
    printf("%d\n",ans); 
  } 
  return 0; 
}

 

Input

Multi test cases,each line will contain two integers n and m. Process to end of file. 
[Technical Specification]
1<=n, m <= 1000000000
 

Output

For each case,output an integer,represents the output of above program.
 

Sample Input

1 10
3 100
 

Sample Output

1
5
思路:若n为奇数, ans(n) = 2^(n-1) + 2^(n-3) +...2^0
   若n为偶数, ans(n) = 2^(n-1) + 2^(n-3) +...2^1
 
由于 2^1+2^2+2^3+2^4 = (2^1+2^2) + 2^2*(2^1+2^2)  故只需要算一半就好了, 那么就可以递归处理,注意,若为奇数项,把最后一项算出来加上去就好了
n为奇数时的递归边界是:2^0 = 1;
n为偶数时的递归边界是:2^1 = 2;
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <map>
#include <set>
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1|1
using namespace std;
typedef long long LL;
const int N = 100005;
LL n, m, nn, mm;
LL pow_mod(LL b)
{
LL res = 1, a = 2;
while(b) {
if(b & 1) res = res * a % m;
a = (a % m) * a % m;
b >>= 1;
}
return res;
}
LL sum1(LL k)
{
if(k == 1) return 1;
LL tmp, now;
if(k & 1) tmp = pow_mod(k - 1);
else tmp = pow_mod(k); now = sum1(k >> 1) % m;
LL res = (now % m + (now * tmp) % m) % m;
if(k & 1) res = res + pow_mod(k * 2 - 2) % m;
return res; }
LL sum2(LL k)
{
if(k == 1) return 2;
LL tmp, now;
if(k & 1) tmp = pow_mod(k - 1);
else tmp = pow_mod(k); now = sum2(k >> 1) % m;
LL res = (now % m + (now * tmp) % m) % m;
if(k & 1) res = res + pow_mod(k * 2 - 1) % m;
return res;
}
int main()
{
while(~scanf("%lld%lld", &n, &m))
{
LL ans = 0;
if(n & 1) {
nn = (n + 1) >> 1;
ans = sum1(nn);
printf("%lld\n", ans % m);
}
else {
mm = (n) >> 1;
ans = sum2(mm);
printf("%lld\n", ans % m);
}
}
}

  

 
 
 
 
 
 
上一篇:SIP压力测试——奇林软件kylinPET


下一篇:mac 终端启动 jboss 停留在(Starting) 不能启动joss