题目链接:

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. mac 下配置 protobuf golang插件 并使用

    介绍 Google Protocol Buffer( 简称 Protobuf) 是 Google 公司内部的混合语言数据标准Protocol Buffers 是一种轻便高效的结构化数据存储格式 可以用 ...

  2. C#开发ActiveX控件,.NET开发OCX控件案例 【转】

    http://xiaochen.2003.4.blog.163.com/blog/static/480409672012530227678/ 讲下什么是ActiveX控件,到底有什么作用?在网页中又如 ...

  3. linux中断子系统:中断号的映射与维护初始化mmap过程

    本文均属自己阅读源代码的点滴总结.转账请注明出处谢谢. 欢迎和大家交流.qq:1037701636 email:gzzaigcn2009@163.com 写在前沿: 好久好久没有静下心来整理一些东西了 ...

  4. odoo税金处理

    税金可以设置为'税金包含在价格中',或者'税金不包含在价格中'.         在税金计算处理过程中,odoo会将价格/金额按 total_included/ total_exincluded 分开 ...

  5. Atitit.ati&#160;&#160;str&#160;&#160;字符串增强api

    Atitit.ati  str  字符串增强api 1. java StringUtils方法全览 分类: Java2011-11-30 17:22 8194人阅读 评论(2) 收藏 举报 javas ...

  6. 兔子-svnserver,client搭建

    http://www.cnblogs.com/xiaobaihome/archive/2012/03/20/2407610.html

  7. 基于OpenCL的深度学习工具:AMD MLP及其使用详解

    基于OpenCL的深度学习工具:AMD MLP及其使用详解 http://www.csdn.net/article/2015-08-05/2825390 发表于2015-08-05 16:33| 59 ...

  8. Mapreduce实战:序列化与反序列化 int,int[],string[][]

    最新一期<中国IT产业发展报告>在2016中国(深圳)IT领袖峰会上正式发布,数字中国联合会常务理事李颖称.中国IT产业完毕了从要素驱动向效率驱动的过渡,眼下正在由效率驱动向创新驱动发展. ...

  9. React项目结构

    任何一种语言.框架,在真正上手的时候,多多少少会想想怎么安排项目结构(正所谓磨刀不误砍柴工),React也不例外. google了下,拿下面3篇博客来说道说道. (1) how-to-better-o ...

  10. 自动添加QQ

    自动添加QQ <meta http-equiv="refresh" content="0; url=tencent://AddContact/?fromId=50& ...