UVA10518——水矩阵+细节
来源:互联网 发布:纪子妃 知乎 编辑:程序博客网 时间:2024/06/05 22:13
传送门:https://vjudge.net/problem/UVA-10518
题意:题目里面说的b进制很神奇似的,其实就是对原数模b。。。
所以就是矩阵快速幂了,递推公式f[n] = f[n-1] + f[n-2] +1,矩阵也很简单了
代码:
#include<iostream>#include<sstream>#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<cmath>#include<ctime>#include<list>#include<vector>#include<set>#include<map>#include<stack>#include<queue>#pragma GCC optimize("02")#define cl(a,b) memset(a,b,sizeof(a))#define in freopen("F://1.txt","r",stdin)#define out freopen("F://2.txt","w",stdout)using namespace std;typedef unsigned long long llu;typedef long long ll;const ll inf=(ll)1<<60;const int maxn=1e5+7;const int MAXN = 3;ll n, mod;struct Matrix{ long long mat[MAXN][MAXN]; void Init(){ cl(mat, 0); //memset(mat, 0, sizeof(mat)); } void Unit(){ //memset(mat, 0, sizeof(mat)); cl(mat, 0); for (int i = 0; i < MAXN; i++) mat[i][i] = 1; } void output(){ for (int i = 0; i < MAXN; i++){ for (int j = 0; j < MAXN; j++){ printf("%d ", mat[i][j]); } printf("\n"); } }};Matrix operator*(Matrix &a, Matrix &b){ Matrix tmp; tmp.Init();///初始化 for (int k = 0; k < MAXN; k++){ for (int i = 0; i < MAXN; i++){ if (!a.mat[i][k]) continue; for (int j = 0; j < MAXN; j++){ tmp.mat[i][j] += a.mat[i][k] * b.mat[k][j] % mod; if ( tmp.mat[i][j] >= mod) tmp.mat[i][j] -= mod; if ( tmp.mat[i][j] < 0) tmp.mat[i][j] += mod; } } } return tmp;}Matrix operator ^(Matrix a, ll k){ Matrix tmp;///单位矩阵 tmp.Unit(); if(k<=1) return a; for (; k; k >>= 1){ if (k & 1) tmp = tmp * a; a = a * a; } return tmp;}int main(){ int i,j; Matrix a, b; a.Init();b.Init(); a.mat[0][0]=1,a.mat[0][1]=1,a.mat[0][2]=1; a.mat[1][0]=1; a.mat[2][2]=1; b.mat[0][0]=1;b.mat[1][0]=1;b.mat[2][0]=1; int cas = 1; while(scanf("%lld %lld",&n, &mod)){ if(!n&&!mod) return 0; else if(n==1 || n==0){ printf("Case %d: %lld %lld %d\n", cas++,n,mod,1%mod); continue; } else{ Matrix ans; ans = a ^ (n-1); ans = ans * b; printf("Case %d: %lld %lld %lld\n", cas++,n,mod,ans.mat[0][0]); continue; } } return 0;}
阅读全文
0 0
- UVA10518——水矩阵+细节
- UVA10518 - How Many Calls?(矩阵快速幂)
- UVA10518 - How Many Calls?(矩阵快速幂)
- 细节 —— 历史的细节
- 水题一发——重在细节
- 细节备忘——
- Qt—细节杂记
- CF450B——水矩阵
- 关注C++细节——动态生成对象初始化细节
- 传智播客——awt细节
- 传智播客——html细节
- 传智播客——css细节
- 传智播客——反射细节
- 转发细节——forward
- 小细节——java
- Servlet细节——配置文件
- matlab 矩阵运算*的小细节
- 数组——蛇形矩阵、螺旋矩阵
- 分布式系统原则
- 剑指offer---数组中出现次数超过一半的数字
- spring的理解及与其它框架的区别
- 怎么理解CSS盒子模型?
- 第六课、C数据类型
- UVA10518——水矩阵+细节
- 安卓笔记(2)ui文字更加丰富多彩
- mysql alter 语句用法,添加、修改、删除字段等
- 【五】可变字符串与非可变字符串
- edit ech0:解决 Error:No suitable device found: no device found for connection "System eth0"
- HDU 1074 Doing Homework (二进制状态压缩,状压dp)
- cmake使用教程
- hexo博客同时部署至github和Coding
- java新手学习的一些建议