hdu 4990(数学,等比数列求和)

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

BestCoder Round #8

这个题就是需要用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 = 1;
    while(n){
        if(n&1) ans=ans*a%mod;
        a= a*a%mod;
        n>>=1;
    }
    return ans;
}
 LL cal(LL p,LL n){  ///这里是递归求解等比数列模板 1+p+p^2...+p^n
    if(n==0) return 1;
    if(n&1){///(1+p+p^2+....+p^(n/2))*(1+p^(n/2+1));
         return (1+pow_mod(p,n/2+1))*cal(p,n/2)%mod;
    }
    else { ///(1+p+p^2+....+p^(n/2-1))*(1+p^(n/2+1))+p^(n/2);
         return (pow_mod(p,n/2)+(1+pow_mod(p,n/2+1))*cal(p,n/2-1))%mod;
    }
}

int main()
{
    LL n;
    while(scanf("%lld%lld",&n,&mod)!=EOF)
    {
        if(n==1&&mod==1) {
            printf("0\n");
            continue;
        }
        LL k = (n+1)/2;
        LL ans = cal(4,k-1);
        if(n&1){
            printf("%lld\n",ans);
        }else {
            printf("%lld\n",ans*2%mod);
        }
    }
    return 0;
}
时间: 2024-08-02 21:27:11

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

hdu 4990 Reading comprehension(等比数列法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4990 思路:曾经有一个矩阵乘法的做法请戳这儿.. . . 開始我们把数都不模... 能够得到一个规律 n:1        ans:1      4^0                          n:2     ans:2         2*(4^0) 2                 5      4^0+4^1                        4           

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

for(i=1;i<=n;i++) { if(i&1)ans=(ans*2+1)%m; else ans=ans*2%m; } 给定n,m.让你用O(log(n))以下时间算出ans. 打表,推出 ans[i] = 2^(i-1) + f[i-2] 故 i奇数:ans[i] = 2^(i-1) + 2^(i-3) ... + 1; i偶数:ans[i] = 2^(i-1) + 2^(i-3) ... + 2; 故可以用等比数列求和公式. 公式涉及除法.我也没弄懂为啥不能用逆元,貌似说是啥逆元

HDU 4990 Reading comprehension (找规律+矩阵快速幂)

题目链接:HDU 4990 Reading comprehension 题目给的一个程序其实就是一个公式:当N=1时 f[n]=1,当n>1时,n为奇数f[n]=2*f[n-1]+1,n为偶数f[n]=2*f[n-1]. 先不取模,计算前十个找规律.得到一个递推公式:f[n]=2*f[n-2]+f[n-1]+1 然后快速幂解决之. 给出一个神奇的网站(找数列通项):http://oeis.org/ AC代码: #include<stdio.h> #include<string.h&

hoj3152-Dice 等比数列求和取模

http://acm.hit.edu.cn/hoj/problem/view?id=3152 Dice My Tags (Edit) Source : Time limit : 1 sec Memory limit : 128 M Submitted : 82, Accepted : 18 You have a dice with M faces, each face contains a distinct number. Your task is to calculate the expect

hdu 4961 数学杂题

http://acm.hdu.edu.cn/showproblem.php?pid=4961 先贴个O(nsqrtn)求1-n所有数的所有约数的代码: vector<int>divs[MAXN]; void caldivs() { for(int i=1;i<MAXN;i++) for(int j=i;j<MAXN;j+=i) divs[j].push_back(i); } 有了这个当时理下思路就可写了,但是重复数处理注意: 1.用一个数组vis[]  vis[i]=1表示i存在

[NOI2011] 兔农 矩阵乘法,矩阵的等比数列求和

#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; typedef long long ll; ll n,p; ll k; #define N 4000000 ll a[N],fr[N]; struct sq{ ll a[3][3]; sq(){memset(a,0,sizeof(a));} sq operator*(sq

等比数列求和公式

[等比数列求和公式] 首项a1,公比q a(n+1)=an*q=a1*q^(n Sn=a1+a2+..+an q*Sn=a2+a3+...+a(n+1) qSn-Sn=a(n+1)-a1 S=a1(q^n-1)/(q-1)

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

[Tjoi2016&Heoi2016]求和 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 679  Solved: 534[Submit][Status][Discuss] Description 在2016年,佳媛姐姐刚刚学习了第二类斯特林数,非常开心. 现在他想计算这样一个函数的值: S(i, j)表示第二类斯特林数,递推公式为: S(i, j) = j ∗ S(i − 1, j) + S(i − 1, j − 1), 1 <= j &l

B-Icebound and Sequence(等比数列求和取模)

题目传送门:B-Icebound and Sequence(19年河北省赛) 题目大意: 等比数列求和,结果取模 分析: 因为取模操作,直接运用等比数列求和公式无法做出,所以需要用到公式 求等比为k的等比数列之和S[n]..当n为偶数..S[n] = S[n/2] + pow(k,n/2) * S[n/2] n为奇数...S[n] = S[n/2] + pow(k,n/2) * S[n/2] + pow(k,n)等比数列第n个数的值 代码: #include<bits/stdc++.h> us