HDU-1757(矩阵快速幂)

2024-03-02 10:58
文章标签 快速 矩阵 hdu 1757

本文主要是介绍HDU-1757(矩阵快速幂),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Lele now is thinking about a simple function f(x). 

If x < 10 f(x) = x. 
If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10); 
And ai(0<=i<=9) can only be 0 or 1 . 

Now, I will give a0 ~ a9 and two positive integers k and m ,and could you help Lele to caculate f(k)%m. 
Input
The problem contains mutiple test cases.Please process to the end of file. 
In each case, there will be two lines. 
In the first line , there are two positive integers k and m. ( k<2*10^9 , m < 10^5 )
In the second line , there are ten integers represent a0 ~ a9. 
Output
For each case, output f(k) % m in one line.
Sample Input
10 9999
1 1 1 1 1 1 1 1 1 1
20 500
1 0 1 0 1 0 1 0 1 0
Sample Output
45
104
/*
题意:给两个数k和m,然后是10个由0和1组成的数,分别代表a0->a9,求f(k)%m
f(x)定义为:
If x < 10 ,f(x) = x.
If x >= 10 ,f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10);题解:
构建矩阵
| f(10)|       |a0 a1 a2 ...a8 a9|          |f(9)|
| f(9) |       | 1  0  0 ... 0  0|          |f(8)|
| .....|  =    |.. ... ... ... ..|     *    | .. |
| f(2) |       | 0  0  0 ... 0  0|          |f(1)|
| f(1) |       | 0  0  0 ... 1  0|          |f(0)|*/#include <stdio.h>
#include <string.h>
#include<iostream>
#include <algorithm>
#include<math.h>
#include<map>
using namespace std;struct node
{int maxtri[12][12];
};
node res, roi;
long long int k, m;void init()
{memset(res.maxtri, 0, sizeof(res.maxtri));for (int i = 0; i < 10; i++){res.maxtri[i][i] = 1;}for (int i = 1; i < 10; i++){for (int j = 0; j < 10; j++){if ((i - 1) == j){roi.maxtri[i][j] = 1;}elseroi.maxtri[i][j] = 0;}}
}node muti(node ss, node tt)
{node ans;memset(ans.maxtri, 0, sizeof(ans.maxtri));for (int i = 0; i < 10; i++){for (int j = 0; j < 10; j++){if (ss.maxtri[i][j] == 0)continue;for (int kk = 0; kk < 10; kk++){ans.maxtri[i][kk] = (ans.maxtri[i][kk] + (ss.maxtri[i][j] * tt.maxtri[j][kk]) % m) % m;}}}return ans;
}void muti_maxtri(int n)
{while (n){if (n % 2 != 0){res = muti(res, roi);}roi = muti(roi, roi);n /= 2;}}int main()
{while (cin >> k >> m){for (int i = 0; i < 10; i++){cin >> roi.maxtri[0][i];}init();if (k <= 9){cout << k%m << endl;continue;}else{muti_maxtri(k - 9);int sum = 0;for (int i = 0; i < 10; i++){sum += (res.maxtri[0][i] * (9 - i)) % m;sum %= m;}cout << sum << endl;}}return 0;
}




这篇关于HDU-1757(矩阵快速幂)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/765886

相关文章

电脑桌面文件删除了怎么找回来?别急,快速恢复攻略在此

在日常使用电脑的过程中,我们经常会遇到这样的情况:一不小心,桌面上的某个重要文件被删除了。这时,大多数人可能会感到惊慌失措,不知所措。 其实,不必过于担心,因为有很多方法可以帮助我们找回被删除的桌面文件。下面,就让我们一起来了解一下这些恢复桌面文件的方法吧。 一、使用撤销操作 如果我们刚刚删除了桌面上的文件,并且还没有进行其他操作,那么可以尝试使用撤销操作来恢复文件。在键盘上同时按下“C

usaco 1.3 Mixing Milk (结构体排序 qsort) and hdu 2020(sort)

到了这题学会了结构体排序 于是回去修改了 1.2 milking cows 的算法~ 结构体排序核心: 1.结构体定义 struct Milk{int price;int milks;}milk[5000]; 2.自定义的比较函数,若返回值为正,qsort 函数判定a>b ;为负,a<b;为0,a==b; int milkcmp(const void *va,c

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

hdu 2093 考试排名(sscanf)

模拟题。 直接从教程里拉解析。 因为表格里的数据格式不统一。有时候有"()",有时候又没有。而它也不会给我们提示。 这种情况下,就只能它它们统一看作字符串来处理了。现在就请出我们的主角sscanf()! sscanf 语法: #include int sscanf( const char *buffer, const char *format, ... ); 函数sscanf()和

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

hdu 1754 I Hate It(线段树,单点更新,区间最值)

题意是求一个线段中的最大数。 线段树的模板题,试用了一下交大的模板。效率有点略低。 代码: #include <stdio.h>#include <string.h>#define TREE_SIZE (1 << (20))//const int TREE_SIZE = 200000 + 10;int max(int a, int b){return a > b ? a :

hdu 1166 敌兵布阵(树状数组 or 线段树)

题意是求一个线段的和,在线段上可以进行加减的修改。 树状数组的模板题。 代码: #include <stdio.h>#include <string.h>const int maxn = 50000 + 1;int c[maxn];int n;int lowbit(int x){return x & -x;}void add(int x, int num){while

hdu 3790 (单源最短路dijkstra)

题意: 每条边都有长度d 和花费p,给你起点s 终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的。 解析: 考察对dijkstra的理解。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstrin

hdu 2489 (dfs枚举 + prim)

题意: 对于一棵顶点和边都有权值的树,使用下面的等式来计算Ratio 给定一个n 个顶点的完全图及它所有顶点和边的权值,找到一个该图含有m 个顶点的子图,并且让这个子图的Ratio 值在所有m 个顶点的树中最小。 解析: 因为数据量不大,先用dfs枚举搭配出m个子节点,算出点和,然后套个prim算出边和,每次比较大小即可。 dfs没有写好,A的老泪纵横。 错在把index在d

hdu 1102 uva 10397(最小生成树prim)

hdu 1102: 题意: 给一个邻接矩阵,给一些村庄间已经修的路,问最小生成树。 解析: 把已经修的路的权值改为0,套个prim()。 注意prim 最外层循坏为n-1。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstri