hdu4990 Reading comprehension 矩阵快速幂
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;
}
矩阵快速幂
#include<stdio.h>
#include<string.h>
typedef long long ll;
int mod;
struct mat{
int r,c;
ll m[][];
void clear(){
for(int i=;i<=r;i++)memset(m[i],,sizeof(m[i]));
}
}; mat MatMul(mat m1,mat m2){
mat tmp;
tmp.r=m1.r;
tmp.c=m2.c;
int i,j,k;
for(i=;i<=tmp.r;i++){
for(j=;j<=tmp.c;j++){
ll t=;
for(k=;k<=m1.c;k++){
t=(t+(m1.m[i][k]*m2.m[k][j])%mod)%mod;
}
tmp.m[i][j]=t;
}
}
return tmp;
} mat MatQP(mat a,int n){
mat ans,tmp=a;
ans.r=ans.c=a.r;
memset(ans.m,,sizeof(ans.m));
for(int i=;i<=ans.r;i++){
ans.m[i][i]=;
}
while(n){
if(n&)ans=MatMul(ans,tmp);
n>>=;
tmp=MatMul(tmp,tmp);
}
return ans;
} int main(){
mat a;
a.r=;a.c=;
a.m[][]=;
a.m[][]=;
a.m[][]=;
mat t;
t.r=;
t.c=;
t.clear();
t.m[][]=;
t.m[][]=;
t.m[][]=;
t.m[][]=;
t.m[][]=;
int n;
while(scanf("%d%d",&n,&mod)!=EOF){
mat tmp=MatQP(t,n/);
tmp=MatMul(tmp,a);
printf("%lld\n",tmp.m[n%+][]);
}
return ;
}
hdu4990 Reading comprehension 矩阵快速幂的更多相关文章
- 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 ...
- hdu 4990 Reading comprehension 二分 + 快速幂
Description Read the program below carefully then answer the question. #pragma comment(linker, " ...
- HDU4990 Reading comprehension —— 递推、矩阵快速幂
题目链接:https://vjudge.net/problem/HDU-4990 Reading comprehension Time Limit: 2000/1000 MS (Java/Others ...
- HDU 4990 Reading comprehension 简单矩阵快速幂
Problem Description Read the program below carefully then answer the question.#pragma comment(linker ...
- 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] 故 ...
- HDU - 4990 Reading comprehension 【矩阵快速幂】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4990 题意 初始的ans = 0 给出 n, m for i in 1 -> n 如果 i 为奇 ...
- hdu4990矩阵快速幂
就是优化一段代码,用矩阵快速幂(刚开始想到了转移矩阵以为是错的) 在搜题解时发现了一个神奇的网站:http://oeis.org/ 用来找数列规律 的神器.... 规律就是an=an-1+2*an-2 ...
- 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)
题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ...
- 51nod 算法马拉松18 B 非010串 矩阵快速幂
非010串 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个01字符串满足不存在010这样的子串,那么称它为非010串. 求长度为n的非010串的个数.(对1e9+7取模) ...
随机推荐
- React中禁止chrome填充密码表单
当 input 的 type="password" 时,chrome浏览器会以 type="password" 为标识记住输入的用户名和密码, 如果chrome ...
- Mysql 实列结构-进程
一.MySQL后台进程简介 master thread与四大I/O线程:read thread.write thread.redo log thread.change buffer thread与 p ...
- Ant在Java项目中的使用(一眼就看会)
参考:http://www.cnblogs.com/zhengqiang/p/5557155.html Ant是跨平台的构建工具,它可以实现项目的自动构建和部署等功能.在本文中,主要让读者熟悉怎样将A ...
- unordered_map/unordered_set & unordered_multimap/unordered_multiset非关联容器
body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...
- bind配置文件
options{} - 整个bind使用的全局配置选项 bind监听的端口,数据文件存储位置,缓存的存储位置,权限加密的控制 logging{}- 服务日志选项 日志输出的位置,以及输出的级别 zon ...
- Centos7配置NFS
centos7配置nfs yum -y install nfs-utils rpcbind 设置服务开机启动: systemctl enable rpcbind systemctl enable nf ...
- java去除字符串的空格,换行符,水平制表符,回车
final private String stringTrimAll(final String input) { if (null == input) return ""; // ...
- mysql创建存储过程,定时任务,定时删除log
-- 创建存储过程 清除30天前的日志create procedure deleteLog()BEGINdelete from contract_vlog where create_time<D ...
- netty源码理解补充 之 DefaultChannelPipeline到底是个啥
- wait_activity
wait_activity(self, activity, timeout, interval=1): android特有的 返回的True 或 False :Agrs: - activity - 需 ...