hdu-4990 Reading comprehension(快速幂+乘法逆元)

题目链接:

Reading comprehension

Time Limit: 2000/1000 MS (Java/Others)   

 Memory Limit: 32768/32768 K (Java/Others)

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

题意:

给n和m,问如果按给的程序执行,最后得结果是多少;

思路:

把给的程序编程通项公式可以发现:

当n为奇数时ans=(2^(n+1)-1)/3%m;

当n为偶数时ans=(2^(n+1)-2)/3%m;

这个可以用二项式公式得到;2^0+2^1+2^2+...+2^(n-1)=2^n-1;然后啦啦啦啦就出来了;

可是这个取模有除法诶,除法的我不会怎么办,所以就去看了求乘法逆元怎么求,然后没看懂,但我看懂了这个式子:

ans=a/b%m  <==>ans=a%(b*m)/b;

证明如下:

a/b=km+x;

a=kbm+bx;

a%(b*m)=bx;

a%(b*m)/b=x;

a/b%m=x=a%(b*m)/b;

前提是a能整除b,即b|a;

然后一个快速幂就搞出结果啦啦啦;

AC代码:

/*Accepted    4990    0MS    1568K    518 B    G++    2014300227*/
#include <bits/stdc++.h>
using namespace std;
const int N=3e5+4;
typedef long long ll;
int n,m;
ll fastpow(int x,int y)
{
    int temp=x;
    ll mod=3*(ll)y;
    ll ans=1,base=2;
    while(x)
    {
        if(x&1)ans=(ans*base%mod);
        base=base*base%mod;
        x=(x>>1);
    }
    if(temp%2==0)return (ans-1%mod+mod)%mod;
    else return (ans-2%mod+mod)%mod;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        printf("%lld\n",fastpow(n+1,m)/3);
    }

    return 0;
}
时间: 2024-10-18 20:14:15

hdu-4990 Reading comprehension(快速幂+乘法逆元)的相关文章

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&

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4990 Problem Description Read the program below carefully then answer the question. #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include<iostream> #include

HDU 4990 Reading comprehension (矩阵快速幂)

题意:给一个数列a[i]=2a[i-1](如果i是偶数) a[i]=2a[i-1]+1(如果i是奇数):求a[n]%m (1<=n, m <= 1000000000) 思路:明显用矩阵快速幂,可以推出通项:a[n]=2*a[n-2]+a[n-1]+1 当然并不需要动脑...直接当成偶数处理就好,是奇数的话单独再递推一项就好.也就是a[i]=4a[i-2]+2 //4990 0MS 1620K 1196 B C++ #include<cstdio> #include<iostr

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(BestCoder Round #8)

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 <algor

hdu 4990 Reading comprehension (矩阵快速幂)

题意:读程序,找规律 思路:我们把程序输出发现序列为1,2,5,10,21,42,85,170,递推式f(n)=2*f(n-2)+f(n-1)+1 代码: #include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #define ll long long using namespace std; const int N=3,M=3,P=3; ll mod; stru

[矩阵快速幂] hdu 4990 Reading comprehension

题意: 初始值为零,后面奇数项成二加一,偶数项乘二. 思路: 其实区别就在于这个加一. 就是构造一个-1每次相成,然后1-1+1就ok了. 就是 |  -1   1   0  | | -1  1  0 | * |   0   1   1  |  =  | 1  0  1 | |   0   0   2  | 依次类推就好了. 代码: #include"cstdlib" #include"cstdio" #include"cstring" #inc

HDU 5793 A Boring Question (找规律 : 快速幂+乘法逆元)

A Boring Question Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 865    Accepted Submission(s): 534 Problem Description There are an equation.∑0≤k1,k2,?km≤n∏1?j<m(kj+1kj)%1000000007=?We define

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