描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1008 监狱有连续编号为1...N的N个房间,每个房间关押一个犯人,有M种宗教,每个犯人可能信仰其中一种.如果 相邻房间的犯人的宗教相同,就可能发生越狱,求有多少种状态可能发生越狱. p.s.我真的没有企图概括的必要... 分析 所有情况是m^n,不可能发生越狱的情况是m*(m-1)^(n-1). 最后答案就是: m*(m^(n-1)-(m-1)^(n-1)).做个快速幂就好了. 注意: 1.…
Description 监狱有连续编号为1...N的N个房间,每个房间关押一个犯人,有M种宗教,每个犯人可能信仰其中一种.如果相邻房间的犯人的宗教相同,就可能发生越狱,求有多少种状态可能发生越狱 Input 输入两个整数M,N.1<=M<=10^8,1<=N<=10^12 Output 可能越狱的状态数,模100003取余 Sample Input 2 3 Sample Output 6 HINT 6种状态为(000)(001)(011)(100)(110)(111) Source…
链接:传送门 思路:简单矩阵快速幂,算完 A^k 后再求一遍主对角线上的和取个模 /************************************************************************* > File Name: hdu1575.cpp > Author: WArobot > Blog: http://www.cnblogs.com/WArobot/ > Created Time: 2017年05月02日 星期二 20时42分37秒 **…
[BZOJ1009] [HNOI2008] GT考试(KMP+dp+矩阵快速幂) 题面 阿申准备报名参加GT考试,准考证号为N位数X1X2-.Xn,他不希望准考证号上出现不吉利的数字.他的不吉利数学A1A2-Am有M位,不出现是指X1X2-Xn中没有恰好一段等于A1A2-Am. A1和X1可以为0 \(0 \leq X_i \leq 9,0\leq Ai\leq 9,m \leq 20,n \leq 10^9\) 分析 先考虑暴力的思路,设\(dp[i][j]\)表示前i位数与不吉利数字匹配了前…
题目传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1008 Description 监狱有连续编号为1...N的N个房间,每个房间关押一个犯人,有M种宗教,每个犯人可能信仰其中一种.如果相邻房间的犯人的宗教相同,就可能发生越狱,求有多少种状态可能发生越狱 Input 输入两个整数M,N.1<=M<=10^8,1<=N<=10^12 Output 可能越狱的状态数,模100003取余 Sample Input 2 3 Samp…
题目链接: C. Vanya and Label time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output While walking down the street Vanya saw a label "Hide&Seek". Because he is a programmer, he used & as a…
1008: [HNOI2008]越狱 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 12593  Solved: 5439[Submit][Status][Discuss] Description 监狱有连续编号为1...N的N个房间,每个房间关押一个犯人,有M种宗教,每个犯人可能信仰其中一种.如果相邻房间的犯人的宗教相同,就可能发生越狱,求有多少种状态可能发生越狱 Input 输入两个整数M,N.1<=M<=10^8,1<=N<=1…
题目链接:https://vjudge.net/problem/UVA-10870 题目意思: 给出a1,a2,a3,a4,a5………………ad,然后算下面这个递推式子,简单的矩阵快速幂,裸题,但是第一个次遇到了矩阵大小不确定的矩阵快速幂,而且在这道题里面第一次明白了如何构造矩阵.算是矩阵快速幂的学习的一个小里程碑吧. f(n) = a1 *f(n - 1) + a2 *f(n - 2) + a3 *f(n - 3) + … + ad* f(n - d),  n > d.求f(n) 代码: //…
Time Limit: 2000/1000 MS (Java/Others)   Memory Limit: 131072/131072 K (Java/Others) Problem Description soda has a set S with n integers {1,2,…,n}. A set is called key set if the sum of integers in the set is an even number. He wants to know how man…
In a galaxy far far away there is an ancient game played among the planets. The specialty of the gameis that there is no limitation on the number of players in each team, as long as there is a captain inthe team. (The game is totally strategic, so so…
这道题目第二次看的时候才彻底理解了是什么意思 把题目转化为数学模型分析后就是 有一个初始序列, 有一个进化率矩阵 求的是初始序列 与进化率矩阵进行 m 次运算后, 初始序列最后一位的答案 那么显然,可以对进化率矩阵进行快速幂计算 Example Let's assume that P(0, 1) = P(1, 2) = 1, and at the beginning of a sub-process, the populations of 0, 1, 2 are 40, 20 and 10 re…
题目中所给的方阵就是一个矩阵,而就是只要将题目所给矩阵不断进行相乘即可,本题中我采用的是直接重载运算符*,使矩阵每一个都进行运算,可以简化为只对对角线上的元素进行运算.最后所得结果就只需将最终的矩阵上的对角线上的数相加即可.快速幂即是将指数进行质因子分解,从而减少运算,比如15=7*2+1,而7=3*2+1,3=2*1+1,如此便只需3步即可求出15. 代码如下: #include<cstdio> #include<cstring> using namespace std; ; s…
传送门 f[i][j]f[i][j]f[i][j]表示从状态"匹配了前i位"转移到"匹配了前j位"的方案数. 这个东西单次是可以通过跳kmp的fail数组得到的. 考虑到每次都是一样的就可以用矩阵快速幂优化一波. 代码: #include<bits/stdc++.h> using namespace std; int n,m,mod,fail[21]; bool vis[21][10]; char s[21]; struct Matrix{ int va…
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 <algori…
https://www.lydsy.com/JudgeOnline/problem.php?id=1009 阿申准备报名参加GT考试,准考证号为N位数X1X2....Xn(0<=Xi<=9),他不希望准考证号上出现不吉利的数字.他的不吉利数学A1A2...Am(0<=Ai<=9)有M位,不出现是指X1X2...Xn中没有恰好一段等于A1A2...Am. A1和X1可以为0 在构造好的next图上跑矩阵快速幂即可 /**********************************…
题意:求第n个三角形内部的上三角形个数 对每个三角形分别维护上下三角形个数,记为\(dp[1][i],dp[2][i]\) 规律很明显是 \(dp[1][i+1]=3*dp[1][i]+dp[2][i]\) \(dp[2][i+1]=3*dp[2][i]+dp[1][i]\) 别忘了快速幂里也要long long,白送了个TLE /*H E A D*/ inline ll mod(ll a){return a%MOD;} struct Matrix{ ll mt[5][5],r,c; void…
题面 传送门 思路 首先,如果$n$和$m$没有那么大的话,有一个非常显然的dp做法: 设$dp[i][j]$表示长度为i的字符串,最后j个可以匹配模板串前j位的情况数 那么显然,答案就是$\sum_{i=0}^{m-1}dp[n][i]$了 转移过程则需要用一个辅助数组:令$g[i][j]$表示模板串的前缀$i$可以转移到前缀$j$的方法数(注意它可能可以转移到很多个串) 辅助数组的生成可以用next数组来推(模板串太短,其实暴力也是可以的) 那么$dp[i+1][k]=dp[i][j]*g[…
Description Assuming a finite – radius “ball” which is on an N dimension is cut with a “knife” of N-1 dimension. How many pieces will the “ball” be cut into most?However, it’s impossible to understand the following statement without any explanation.L…
[HNOI2008]越狱 Description 监狱有连续编号为1...N的N个房间,每个房间关押一个犯人,有M种宗教,每个犯人可能信仰其中一种.如果相邻房间的犯人的宗教相同,就可能发生越狱,求有多少种状态可能发生越狱 输入格式:输入两个整数M,N.1<=M<=10^8,1<=N<=10^12 输出格式:可能越狱的状态数,模100003取余 Solution 1.直接计算所有的越狱方案不方便,考虑使用容斥原理分别计算总方案数和不越狱方案数,相减即可: 2.总方案数为 m^n,因为…
A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2791    Accepted Submission(s): 1659 Problem Description Lele now is thinking about a simple function f(x). If x < 10 f(x) =…
题目链接 设f[i][j]为当前是第i位考号.现在匹配到第j位(已有j-1位和A[]匹配)的方案数 因为假如当前匹配j位,如果选择的下一位与A[j+1]不同,那么新的匹配位数是fail[j]而不是0,那么设由匹配j位转移到匹配k位的方案数为t[j][k] 那么 \(f[i][j] = ∑f[i-1][k]*t[k][j]\) 这个式子是线性的,于是可以先计算出t矩阵的n次幂,最后乘以初始矩阵 t矩阵枚举当前匹配多少位后,枚举下次选择的数即可,利用KMP计算现在匹配的位数 //824kb 24ms…
Description 阿 申准备报名参加GT考试,准考证号为N位数X1X2....Xn(0<=Xi<=9),他不希望准考证号上出现不吉利的数字.他的不吉利数学 A1A2...Am(0<=Ai<=9)有M位,不出现是指X1X2...Xn中没有恰好一段等于A1A2...Am. A1和X1可以为0 Input 第一行输入N,M,K.接下来一行输入M位的数. 100%数据N<=10^9,M<=20,K<=1000 40%数据N<=1000 10%数据N<=6…
题意:       求斐波那契后四位,n <= 1,000,000,000. 思路:        简单矩阵快速幂,好久没刷矩阵题了,先找个最简单的练练手,总结下矩阵推理过程,其实比较简单,关键是能把问题转换成矩阵的题目,也就是转换成简单加减地推式,下面说下怎么样根据递推式构造矩阵把,这个不难,我的习惯是在中间插矩阵,就是比如斐波那契 a[n] = a[n-1] + a[n-2]; 我的习惯是这样,首先要知道这个式子是有连续的两个项就可以推出第三个项 那么        a1 a2   0  1…
http://poj.org/problem?id=1995 简单的快速幂问题 要注意num每次加过以后也要取余,否则会出问题 #include<iostream> #include<cstdio> using namespace std; typedef long long ll; ll mod_pow(ll x,ll n,ll mod) { ll res=; ) { ) res=res*x%mod; x=x*x%mod; n>>=; } return res; }…
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1005 Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 131753    Accepted Submission(s): 31988 Problem Description A number sequence…
实现 pow(x, n) ,即计算 x 的 n 次幂函数. 示例 1: 输入: 2.00000, 10输出: 1024.00000 示例 2: 输入: 2.10000, 3输出: 9.26100 示例 3: 输入: 2.00000, -2输出: 0.25000 解释: 2^(-2) = 1/(2^2) = 1/4 = 0.25 说明: -100.0 < x < 100.0n 是 32 位有符号整数,其数值范围是 [−2^31, 2^31 − 1] . 显然,由于 $n$ 是一个整数,可以使用快…
题意: 最简单的快速幂.给你两个数n和m,求n^m的最后一位: 解题思路: 额,快速幂就很简单了,这里只要最后一位可以一对每次运算都%10: 代码: #include<cstdio> #include<cstring> #include<algorithm> #include <iostream> using namespace std; #define N 100000 long long n,m; long long pow_(long long n,l…
输入a和p.如果p不是素数,则若满足ap = a (mod p)输出yes,不满足或者p为素数输出no.最简单的快速幂,啥也不说了. #include<iostream> #include<cstdio> #include<cstring> #include<cmath> using namespace std; typedef long long ll; ll p,a; int whether(int p) { ; ;i*i<=p;i++) ) {…
Problem Description Given a positive integer N, you should output the most right digit of N^N. Input The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.Each…
给出3个正整数A B C,求A^B Mod C. 例如,3 5 8,3^5 Mod 8 = 3. Input 3个正整数A B C,中间用空格分隔.(1 <= A,B,C <= 10^9) Output 输出计算结果 Input示例 3 5 8 Output示例 3 一道比较简单的快速幂,曾经写过一篇博客,具体讲解请戳链接 #include<iostream> #include<algorithm> #include<string> #include<…