题目链接:https://vjudge.net/problem/HDU-4990

Reading comprehension

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2329    Accepted Submission(s): 954

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
 
Recommend
heyang

题解:

当n为奇数时,f[n] = 2*f[n-1]+1,f[n-1] = 2*f[n-2],所以:f[n] = f[n-1] + 2*f[n-2] + 1;

当n为偶数时,f[n] = 2*f[n-1],f[n-1] = 2*f[n-2] + 1,所以:f[n] = f[n-1] + 2*f[n-2] + 1;

综上:f[n] = f[n-1] + 2*f[n-2] + 1,构造矩阵:

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
//const int MOD = 10000007;
const int MAXN = 1e6+; LL MOD;
const int Size = ;
struct MA
{
LL mat[Size][Size];
void init()
{
for(int i = ; i<Size; i++)
for(int j = ; j<Size; j++)
mat[i][j] = (i==j);
}
}; MA mul(MA x, MA y)
{
MA ret;
memset(ret.mat, , sizeof(ret.mat));
for(int i = ; i<Size; i++)
for(int j = ; j<Size; j++)
for(int k = ; k<Size; k++)
ret.mat[i][j] += (1LL*x.mat[i][k]*y.mat[k][j])%MOD, ret.mat[i][j] %= MOD;
return ret;
} MA qpow(MA x, LL y)
{
MA s;
s.init();
while(y)
{
if(y&) s = mul(s, x);
x = mul(x, x);
y >>= ;
}
return s;
} MA tmp = {
, , ,
, , ,
, ,
}; int main()
{
LL n, m;
while(scanf("%lld%lld",&n,&m)!=EOF)
{
MOD = m;
if(n<=)
{
printf("%lld\n", n%MOD);
continue;
} MA s = tmp;
s = qpow(s, n-); LL ans = ((2LL*s.mat[][]%MOD + s.mat[][])%MOD+s.mat[][])%MOD;
printf("%lld\n", ans);
}
}

最新文章

  1. 关于javascript的运动教程
  2. 网络编程2--毕向东java基础教程视频学习笔记
  3. JDK中的设计模式
  4. 【vijos】P1514天才的记忆
  5. Set和Map
  6. gc 日志格式
  7. 在SpringMVC框架下建立Web项目时web.xml到底该写些什么呢?
  8. 从头开始-06.C语言中预处理指令
  9. HTML之学习笔记(九)表单
  10. spring mvc controller间跳转 重定向
  11. Oracle体系结构及备份(十七)——bg-others
  12. IndexAction.java (Java之负基础实战)
  13. 12_Android中HttpClient的应用,doGet,doPost,doHttpClientGet,doHttpClient请求,另外借助第三方框架实现网络连接的应用,
  14. Python基础:编码规范(4)
  15. python-17
  16. C++ #和##运算符
  17. Linux 第十天
  18. Global Mapper如何加载在线地图
  19. 基于zookeeper的activemq的主从集群配置
  20. floor函数

热门文章

  1. CDOJ_24 八球胜负
  2. jQuery使用on()绑定动态生成元素的事件无效
  3. Android Studio 删除项目
  4. 调用tf.softmax_cross_entropy_with_logits函数出错解决
  5. 一天时间用OpenFire打造自己的IM聊天工具
  6. 第六讲_图像分割Image Segmentation
  7. log4net报错Could not load type &#39;System.Security.Claims.ClaimsIdentity&#39;
  8. js:深入继承
  9. php-fpm.conf配置说明(重点要改动和优化的地方)
  10. 并行程序设计---cuda memory