题目链接:

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+;
typedef long long ll;
int n,m;
ll fastpow(int x,int y)
{
int temp=x;
ll mod=*(ll)y;
ll ans=,base=;
while(x)
{
if(x&)ans=(ans*base%mod);
base=base*base%mod;
x=(x>>);
}
if(temp%==)return (ans-%mod+mod)%mod;
else return (ans-%mod+mod)%mod;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
printf("%lld\n",fastpow(n+,m)/);
} return ;
}
 

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

  1. HDU - 4990 Reading comprehension 【矩阵快速幂】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4990 题意 初始的ans = 0 给出 n, m for i in 1 -> n 如果 i 为奇 ...

  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. HDU 4990 Reading comprehension 简单矩阵快速幂

    Problem Description Read the program below carefully then answer the question.#pragma comment(linker ...

  4. 51Nod 1013 3的幂的和 快速幂 | 乘法逆元 | 递归求和公式

    1.乘法逆元 直接使用等比数列求和公式,注意使用乘法逆元 ---严谨,失细节毁所有 #include "bits/stdc++.h" using namespace std; #d ...

  5. HDU 4990 Reading comprehension

    快速幂 #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #i ...

  6. hdu 4990 Reading comprehension 二分 + 快速幂

    Description Read the program below carefully then answer the question. #pragma comment(linker, " ...

  7. HDU 4990 Reading comprehension(矩阵快速幂)题解

    思路: 如图找到推导公式,然后一通乱搞就好了 要开long long,否则红橙作伴 代码: #include<set> #include<cstring> #include&l ...

  8. HDU 4990 Reading comprehension 矩阵快速幂

    题意: 给出一个序列, \(f_n=\left\{\begin{matrix} 2f_{n-1}+1, n \, mod \, 2=1\\ 2f_{n-1}, n \, mod \, 2=0 \end ...

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

    A Boring Question Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

随机推荐

  1. Toolbar的使用.md

    1.什么是Toolbar Toolbar是在Android5.0时出现的一个新控件,其目的用于取代Actionbar,它与Actionbar最大的差别就是Toolbar使用更加灵活.自由,而且Tool ...

  2. vue2.0 + vux (一)Header 组件

    1.main.js import Vue from 'vue' import FastClick from 'fastclick' import VueRouter from 'vue-router' ...

  3. Allegro基本操作——PCB布线

    转:http://blog.sina.com.cn/s/blog_1538bc9470102vyyq.html http://www.elecfans.com/article/80/110/2010/ ...

  4. 轻松搞定RabbitMQ(五)——路由选择

    转自 http://blog.csdn.net/xiaoxian8023/article/details/48733249 翻译地址:http://www.rabbitmq.com/tutorials ...

  5. HMM MEMM CRF 差别 联系

    声明:本文主要是基于网上的材料做了文字编辑,原创部分甚少.參考资料见最后. 隐马尔可夫模型(Hidden Markov Model.HMM),最大熵马尔可夫模型(Maximum Entropy Mar ...

  6. Django知识梳理

    请求周期: url > 路由 > 函数或类 > 返回字符串或模板语言 Form 表单提交: 先处理模板语言再讲HTML发出去 提交 > url > 函数或类中的方法  — ...

  7. canvas 橡皮擦效果制作

    擦除一定数量后全部消失的有用 imageData 方法的 我把代码贴在最下面 <!DOCTYPE html> <html> <head> <meta char ...

  8. java IO 框架图

  9. A20 Android 编译

    cd lichee ./build.sh -p sun7i_android -b wing-sc3075gs cd ../android4.2 . build/envsetup.sh lunch wi ...

  10. 使用 lstat 函数获取文件信息

    前言 在之前的文章中,描述过如何用 fcntl 函数改变文件的状态标记.但,文件还有很多信息,如文件类型,权限设置,设备编号,访问时间等等.如果要获取这些信息,则使用函数 lstat 可以轻松达到这个 ...