HDU 4990 Reading comprehension 简单矩阵快速幂
#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;
}
[Technical Specification]
1<=n, m <= 1000000000
3 100
5


- #include <stdio.h>
- #include <algorithm>
- #include <iostream>
- #include <string.h>
- #define ll __int64
- using namespace std;
- ll mod;
- struct matrix
- {
- ll x[][];
- void init()
- {
- for(int i = ; i < ; i++)
- for(int j = ; j < ; j++)
- x[i][j] = ;
- }
- };
- matrix mul(matrix a, matrix b)
- {
- matrix c;
- c.init();
- for( int i = ; i < ; i++)
- for(int j = ; j < ; j++)
- {
- for(int k = ; k < ; k++)
- {
- c.x[i][j] += a.x[i][k] * b.x[k][j];
- }
- c.x[i][j] %= mod;
- }
- return c;
- }
- matrix powe(matrix x, ll n)
- {
- matrix r;
- r.init();
- r.x[][] = r.x[][] = r.x[][] = ; //初始化
- while(n)
- {
- if(n & )
- r = mul(r , x);
- x = mul(x , x);
- n >>= ;
- }
- return r;
- }
- int main()
- {
- ll x, y, n, ans;
- //while(~scanf("%I64d%I64d", &n, &mod))
- while(cin >> n >> mod)
- {
- if(n == )
- printf("%I64d\n", %mod);
- else if(n == )
- printf("%I64d\n", %mod);
- else
- {
- matrix d;
- d.init();
- d.x[][] = ;
- d.x[][] = ;
- d.x[][] = ;
- d.x[][] = ;
- d.x[][] = ;
- d = powe(d, n - );
- ans = d.x[][] * + d.x[][] * + ; //如果使用手动乘,不知为何还要再判断
- matrix e;
- e.init();
- e.x[][] = ;
- e.x[][] = ;
- e.x[][] = ;
- d = mul(e , d);
- /*if( n % 2 ) //再判断
- ans-=2;
- else
- ans-=1;*/
- cout << d.x[][] % mod << endl;
- }
- }
- }
HDU 4990 Reading comprehension 简单矩阵快速幂的更多相关文章
- HDU - 4990 Reading comprehension 【矩阵快速幂】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4990 题意 初始的ans = 0 给出 n, m for i in 1 -> n 如果 i 为奇 ...
- HDU 4990 Reading comprehension(矩阵快速幂)题解
思路: 如图找到推导公式,然后一通乱搞就好了 要开long long,否则红橙作伴 代码: #include<set> #include<cstring> #include&l ...
- HDU 1575 Tr A( 简单矩阵快速幂 )
链接:传送门 思路:简单矩阵快速幂,算完 A^k 后再求一遍主对角线上的和取个模 /********************************************************** ...
- hdu 5667 BestCoder Round #80 矩阵快速幂
Sequence Accepts: 59 Submissions: 650 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
- hdu 1575 Tr A(矩阵快速幂,简单)
题目 和 LightOj 1096 - nth Term 类似的线构造一个符合题意的矩阵乘法模版,然后套快速幂的模版,具体的构造矩阵我就不作图了,看着代码也能理解吧 #include<stdi ...
- HDU 2604 Queuing( 递推关系 + 矩阵快速幂 )
链接:传送门 题意:一个队列是由字母 f 和 m 组成的,队列长度为 L,那么这个队列的排列数为 2^L 现在定义一个E-queue,即队列排列中是不含有 fmf or fff ,然后问长度为L的E- ...
- hdu 1005 Number Sequence(矩阵快速幂,找规律,模版更通用)
题目 第一次做是看了大牛的找规律结果,如下: //显然我看了答案,循环节点是48,但是为什么是48,据说是高手打表出来的 #include<stdio.h> int main() { ], ...
- hdu 4686 Arc of Dream(矩阵快速幂)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=4686 题意: 其中a0 = A0ai = ai-1*AX+AYb0 = B0bi = bi-1*BX+BY ...
- HDU 4686 Arc of Dream 矩阵快速幂,线性同余 难度:1
http://acm.hdu.edu.cn/showproblem.php?pid=4686 当看到n为小于64位整数的数字时,就应该有个感觉,acm范畴内这应该是道矩阵快速幂 Ai,Bi的递推式题目 ...
随机推荐
- ASP.NET MVC5 学习系列之视图
一.视图约定 当创建一个项目模版时,可以注意到,项目以一种非常具体的方式包含了一个结构化的Views目录.在每一个控制器的View文件夹中,每一个操作方法都有一个同名的视图文件与其对应.(约定大于配置 ...
- Traffic Steering for Service Function Chaining
Introduction 目前通过vlan标签来把流量引向对应的sfc 以前的sfc静态(SFs相邻组成SFC),有了sdn之后具有动态性.(SFs不需要彼此相邻.将流量动态地导向所需的SFs.) 流 ...
- 福大软工1816:Beta(1/7)
Beta 冲刺 (1/7) 队名:第三视角 组长博客链接 本次作业链接 团队部分 团队燃尽图 工作情况汇报 张扬(组长) 过去两天完成了哪些任务 文字/口头描述 答辩 组织会议 复习课本 展示GitH ...
- webService —— soap
package soupTest; import javax.jws.WebMethod; import javax.jws.WebService; import javax.xml.ws.Endpo ...
- J2EE体系
J2EE的概念 目前,Java 2平台有3个版本,它们是适用于小型设备和智能卡的Java 2平台Micro版(Java 2 Platform Micro Edition,J2ME).适用于桌面系统的J ...
- rabbitmqctl 的常用命令
# 查看服务器的状态 rabbitmqctl status # 查看环境变量 rabbitmqctl environment # 停止rabbitmq的应用 rabbitmqctl stop_ ...
- QJsonDocument实现Qt下JSON文档读写
版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:QJsonDocument实现Qt下JSON文档读写 本文地址:http://tech ...
- [历史百科]抗战时期兵团简介 From 百度知道
中央军委1948年11月1日和1949年1月15日两次关于统一全军组织和部队番号的训令,我军先后进行了整编.西北野战军改称第一野战军,司令员兼政治委员彭德怀,第一副司令员张宗逊,第二副司令员赵寿山,参 ...
- Oracle忘记密码如何重置
昨天安装Oracle11g R2的时候给scott用户设置密码,当时没有显示而且还只以输入一次,可能密码输入错误,结果今天用scott用户登录果然密码不对,还好sys和system用户都正常,就进去给 ...
- chrome调试selenium。其实我是无聊了
from selenium import webdriverdriver = webdriver.Chrome()driver.get("http://www.baidu.com" ...