题目链接:

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. ubuntu安装常用软件

    安装unzip sudo apt-get install unzip

  2. iOS绘图—— UIBezierPath 和 Core Graphics

    前言 iOS系统本身提供了两套绘图的框架,即UIBezierPath 和 Core Graphics.而前者所属UIKit,其实是对Core Graphics框架关于path的进一步封装,所以使用起来 ...

  3. 转: 浅析Fusion-IO和Intel SSD

    from: http://alanwu.blog.51cto.com/3652632/865235 标签:SSD 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否 ...

  4. makefile 与android.mk中加信息打印

    makefile里面加打印: [table]@echo ' zImage - Compressed kernel image' android.mk里面加信息打印: $(warning TEXT... ...

  5. java性能监控工具jstat-windows

    jstat Monitors Java Virtual Machine (JVM) statistics. This command is experimental and unsupported. ...

  6. Vue.js 使用cordova camera插件调取相机

    本文给出在vue.js里如何使用cordova的插件完成调取相机及图库,并完成图片上传的操作.具体的操作步骤如下 第一步:在cordova项目下安装cordova-plugin-camera插件 co ...

  7. 《UNIX-Shell编程24学时教程》读书笔记Chap1,2 Shell基础,脚本基础

    Chap1 Shell基础 知道该使用哪种命令是依赖于经验的.----惟手熟尔. 1.1 什么是命令 其实知道这些名词好像也没什么帮助,嘻嘻 1.2 什么是Shell 不同用户不同的提示符:不同的环境 ...

  8. substr使用注意

    substr使用时要判断起点和长度是否超过了串本身的长度,否则会抛异常

  9. EasyUI基础入门之Droppable(可投掷)

    怎么说呢Droppable这个单词究竟是什么意思,准确来说easyui作者究竟要表达什么意思,还是不大好拿捏的.只是没关系,没有必要纠结与这些细枝末节的东西,依据官网的demo效果,就简单的将之定义为 ...

  10. node JS 微信开发

    JS-SDK 要点 微信测试号; 扫码登录;无需认证(只是名称统一为微信测试号)http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/logi ...