Reading comprehension

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

Problem 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
 
Source
 
这个题就是需要用log(n)解决上面的程序问题。
然后我们找奇数项的关系。
f[2k+1] = 2*f[2k] + 1 (k>=1)
f[2k] = 2*f[2k-1]
代入可得 f[2k+1] = 4*f[2k-1]+1 => bi = 4*bi-1+1
bi = 4*bi-1+1
bi-1 = 4*bi-2+1
..
b3 = 4*b2 + 1
b2 = 4*b1 +1
可得bk = 1+4+4^2+....+4^k-1
k与n之间的映射是 k = (n+1)/2 然后带入模板算就OK。偶数的话乘2.
#include <cstdio>
#include<iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include<vector>
typedef long long LL; LL mod;
LL pow_mod(LL a,LL n){
LL ans = ;
while(n){
if(n&) ans=ans*a%mod;
a= a*a%mod;
n>>=;
}
return ans;
}
LL cal(LL p,LL n){ ///这里是递归求解等比数列模板 1+p+p^2...+p^n
if(n==) return ;
if(n&){///(1+p+p^2+....+p^(n/2))*(1+p^(n/2+1));
return (+pow_mod(p,n/+))*cal(p,n/)%mod;
}
else { ///(1+p+p^2+....+p^(n/2-1))*(1+p^(n/2+1))+p^(n/2);
return (pow_mod(p,n/)+(+pow_mod(p,n/+))*cal(p,n/-))%mod;
}
} int main()
{
LL n;
while(scanf("%lld%lld",&n,&mod)!=EOF)
{
if(n==&&mod==) {
printf("0\n");
continue;
}
LL k = (n+)/;
LL ans = cal(,k-);
if(n&){
printf("%lld\n",ans);
}else {
printf("%lld\n",ans*%mod);
}
}
return ;
}

hdu 4990(数学,等比数列求和)的更多相关文章

  1. hdu 4990 Reading comprehension(等比数列法)

    题目链接:pid=4990" style="color:rgb(255,153,0); text-decoration:none; font-family:Arial; line- ...

  2. Reading comprehension HDU - 4990 (矩阵快速幂 or 快速幂+等比数列)

    ;i<=n;i++) { )ans=(ans*+)%m; %m; } 给定n,m.让你用O(log(n))以下时间算出ans. 打表,推出 ans[i] = 2^(i-1) + f[i-2] 故 ...

  3. POJ 1845 (约数和+二分等比数列求和)

    题目链接: http://poj.org/problem?id=1845 题目大意:A^B的所有约数和,mod 9901. 解题思路: ①整数唯一分解定理: 一个整数A一定能被分成:A=(P1^K1) ...

  4. hoj3152-Dice 等比数列求和取模

    http://acm.hit.edu.cn/hoj/problem/view?id=3152 Dice My Tags (Edit) Source : Time limit : sec Memory ...

  5. luogu1397 [NOI2013]矩阵游戏 (等比数列求和)

    一个比较显然的等比数列求和,但有一点问题就是n和m巨大.. 考虑到他们是在幂次上出现,所以可以模上P-1(费马小定理) 但是a或c等于1的时候,不能用等比数列求和公式,这时候就要乘n和m,又要变成模P ...

  6. HDU 5984 数学期望

    对长为L的棒子随机取一点分割两部分,抛弃左边一部分,重复过程,直到长度小于d,问操作次数的期望. 区域赛的题,比较基础的概率论,我记得教材上有道很像的题,对1/len积分,$ln(L)-ln(d)+1 ...

  7. Codeforces 963A Alternating Sum(等比数列求和+逆元+快速幂)

    题目链接:http://codeforces.com/problemset/problem/963/A 题目大意:就是给了你n,a,b和一段长度为k的只有'+'和‘-’字符串,保证n+1被k整除,让你 ...

  8. bzoj 4555 [Tjoi2016&Heoi2016]求和 NTT 第二类斯特林数 等比数列求和优化

    [Tjoi2016&Heoi2016]求和 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 679  Solved: 534[Submit][S ...

  9. ZOJ-3774 Power of Fibonacci——等比数列求和&&等价替换

    题目 求 $\displaystyle \sum_{i=1}^n F_i^k$,($1 \leq n\leq 10^{18},1 \leq  k\leq 10^5$),答案对 $10^9+9$ 取模. ...

随机推荐

  1. Java课堂作业

  2. POJ 2586 贪心+枚举

    Y2K Accounting Bug Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15626   Accepted: 78 ...

  3. PAT Basic 1083

    1083 是否存在相等的差 给定 N 张卡片,正面分别写上 1.2.…….N,然后全部翻面,洗牌,在背面分别写上 1.2.…….N.将每张牌的正反两面数字相减(大减小),得到 N 个非负差值,其中是否 ...

  4. HDU2586 How far away ?

    一.描述 很久没写代码了,在之前一直在参与准备ASC比赛和美赛,现在又重新捡起来.目标是两个月后的邀请赛. 这题是树链拋分解决LCA问题的一个模板题. 首先介绍下树链拋分的基本思想. 对于任意一颗树, ...

  5. Android 简历+面试题 汇总

    1.教你写简历 1.1.你真的会写简历吗? 1.2.80%以上简历都是不合格的 1.3.推荐两个技术简历模板 1.4.关于程序员求职简历 1.5.程序员简历模板列表 2.面试题 2.1.国内一线互联网 ...

  6. sqoop安装和使用

    下载版本:sqoop-1.4.6.bin__hadoop-2.0.4-alpha.tar.gz 官网:http://mirror.bit.edu.cn/apache/sqoop/1.4.6/ jdbc ...

  7. Java装箱和拆箱

    https://www.cnblogs.com/dolphin0520/p/3780005.html http://mxdxm.iteye.com/blog/2028196 装箱过程是通过调用包装器的 ...

  8. 云容器和安全性仍然是困扰IT人士的头号问题

    [TechTarget中国原创] 容器和云安全仍然是IT领域中最热门的两个话题.下面就让我们来详细探讨一下吧. 云容器风靡一时是事出有因的.如Docker这样的容器能够提高应用的可移植性,并让企业用户 ...

  9. 非常全的API接口查询

    http://www.apix.cn/services/category/3 https://www.showapi.com/ https://www.juhe.cn/docs http://deve ...

  10. 【Set Matrix Zeros】cpp

    题目: Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. cl ...