ZOJ 3690 & HDU 3658 (矩阵高速幂+公式递推)
ZOJ 3690
题意:
有n个人和m个数和一个k,如今每一个人能够选择一个数。假设相邻的两个人选择同样的数。那么这个数要大于k
求选择方案数。
思路:
打表推了非常久的公式都没推出来什么可行解,好不easy有了想法结果WA到天荒地老也无法AC。。
于是学习了下正规的做法,恍然大悟。
这道题应该用递推 + 矩阵高速幂。
我们设F(n) = 有n个人,第n个人选择的数大于k的方案数;
G(n) = 有n个人。第n个人选择的数小于等于k的方案数;
那么递推关系式即是:
F(1)=m−k,G(1)=k
F(n)=(m−k)∗F(n−1)+(m−k)∗G(n−1)
G(n)=k∗(n−1)+(k−1)∗G(n−1)
ans=F(n)+G(n)
变换矩阵例如以下:
代码君:
/*
* @author FreeWifi_novicer
* language : C++/C
*/
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<queue>
using namespace std;
#define clr( x , y ) memset(x,y,sizeof(x))
#define cls( x ) memset(x,0,sizeof(x))
#define mp make_pair
#define pb push_back
typedef long long lint;
typedef long long ll;
typedef long long LL;
const lint mod = 1e9 + 7;
const int maxn = 4;
lint n,m,k;
struct Matrix{
int n , m ;
lint a[maxn][maxn];
Matrix( int n , int m ){
this->n = n ;
this->m = m ;
cls(a);
}
Matrix operator * ( const Matrix &tmp ){
Matrix res( n , tmp.m );
for( int i = 0 ; i < n ; i++ )
for( int j = 0 ; j < tmp.m ; j++ )
for( int k = 0 ; k < m ; k++ )
res.a[i][j] = ( res.a[i][j] + ( a[i][k] * tmp.a[k][j] ) % mod ) % mod;
return res;
}
};
void Matrix_print( Matrix x ){
for( int i = 0 ; i < x.n ; i++ ){
for( int j = 0 ; j < x.m ; j++){
cout << x.a[i][j] << ' ';
}
cout << endl;
}
cout << endl;
}
Matrix fast_pow( Matrix x , lint n ){
Matrix res( x.n , x.m );
for( int i = 0 ; i < x.n ; i++ ) res.a[i][i] = 1;
while( n ){
if( n & 1 )
res = res * x;
x = x * x;
n >>= 1;
}
return res;
}
void solve(){
Matrix base( 2 , 1 );
Matrix fun( 2 , 2 );
fun.a[0][0] = m - k ;
fun.a[0][1] = m - k ;
fun.a[1][0] = k ;
fun.a[1][1] = k - 1 ;
base.a[0][0] = m - k ;
base.a[1][0] = k ;
fun = fast_pow( fun , n - 1);
base = fun * base ;
cout << (base.a[1][0] + base.a[0][0]) % mod << endl;
}
int main(){
// freopen("input.txt","r",stdin);
while(cin >> n >> m >> k){
solve();
}
return 0;
}
pid=3658">HDU 3658
题意:
在52个英文字母里面选择m个字母组成一个字符串。
满足下面两个条件:
一、相邻的两个字符的ASCLL码的绝对值小于等于32(比方说X与x的码值差为32)。
二、至少要有一对的字符的绝对值为32。
思路:
分两步:先求出满足条件一的方案数。再减去相邻的两个字符的ASCLL码的绝对值**小于**32的方案数即可。
第一步:
我们设Fc(n) = 有n个字符。第n个字符为c的满足条件一方案数;
那么
FA(n)=FA(n−1)+FB(n−1)+FC(n−1)+...+FZ(n−1)+Fa(n−1)
FB(n)=FA(n−1)+FB(n−1)+FC(n−1)+...+FZ(n−1)+Fa(n−1)+Fb(n−1)
FC(n)=FA(n−1)+FB(n−1)+FC(n−1)+...+FZ(n−1)+Fa(n−1)+Fb(n−1)+Fc(n−1)
…
FZ(n)=FA(n−1)+FB(n−1)+FC(n−1)+...+FZ(n−1)+Fa(n−1)+Fb(n−1)+Fc(n−1)+...+Fz(n−1)
Fa(n)=FA(n−1)+FB(n−1)+FC(n−1)+...+FZ(n−1)+Fa(n−1)+Fb(n−1)+Fc(n−1)+...+Fz(n−1)
Fb(n)=FB(n−1)+FC(n−1)+...+FZ(n−1)+Fa(n−1)+Fb(n−1)+Fc(n−1)+...+Fz(n−1)
Fc(n)=FC(n−1)+...+FZ(n−1)+Fa(n−1)+Fb(n−1)+Fc(n−1)+...+Fz(n−1)
…
Fz(n)=FZ(n−1)+Fa(n−1)+Fb(n−1)+Fc(n−1)+...+Fz(n−1)
然后建立矩阵变换高速幂搞下即可。
。这个矩阵太庞大我就不画了。。
第二步:
我们设Gc(n) = 有n个字符,第n个字符为c的相邻的两个字符的ASCLL码的绝对值**小于**32的方案数。
那么
GA(n)=GA(n−1)+GB(n−1)+GC(n−1)+...+GZ(n−1)
GB(n)=GA(n−1)+GB(n−1)+GC(n−1)+...+GZ(n−1)+Ga(n−1)
GC(n)=GA(n−1)+GB(n−1)+GC(n−1)+...+GZ(n−1)+Ga(n−1)+Gb(n−1)
…
GZ(n)=GA(n−1)+GB(n−1)+GC(n−1)+...+GZ(n−1)+Ga(n−1)+Gb(n−1)+Gc(n−1)+...+Gy(n−1)
Ga(n)=GB(n−1)+GC(n−1)+...+GZ(n−1)+Ga(n−1)+Gb(n−1)+Gc(n−1)+...+Gz(n−1)
Gb(n)=GC(n−1)+...+GZ(n−1)+Ga(n−1)+Gb(n−1)+Gc(n−1)+...+Gz(n−1)
…
Gz(n)=Ga(n−1)+Gb(n−1)+Gc(n−1)+...+Gz(n−1)
建立变换矩阵,而后高速幂。
ans=∑i<=zi=AFi(n)−∑i<=zi=AGi(n)
代码君:
/*
* @author FreeWifi_novicer
* language : C++/C
*/
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<queue>
using namespace std;
#define clr( x , y ) memset(x,y,sizeof(x))
#define cls( x ) memset(x,0,sizeof(x))
#define mp make_pair
#define pb push_back
typedef long long lint;
typedef long long ll;
typedef long long LL;
const int maxn = 54 ;
const lint mod = 1e9 + 7;
lint n ;
struct Matrix{
int n , m ;
lint a[maxn][maxn];
Matrix( int n , int m ){
this->n = n ;
this->m = m ;
cls(a);
}
Matrix operator * ( const Matrix &tmp ){
Matrix res( n , tmp.m );
for( int i = 0 ; i < n ; i++ )
for( int j = 0 ; j < tmp.m ; j++ )
for( int k = 0 ; k < m ; k++ )
res.a[i][j] = ( res.a[i][j] + ( a[i][k] * tmp.a[k][j] ) % mod ) % mod;
return res;
}
};
Matrix fast_pow( Matrix x , lint n ){
Matrix res( x.n , x.m );
for( int i = 0 ; i < x.m ; i++ ) res.a[i][i] = 1 ;
while( n ){
if( n & 1 )
res = res * x;
x = x * x ;
n >>= 1;
}
return res;
}
void solve(){
if( n == 2 ){
cout << 52 << endl;
return;
}
Matrix base( 52 , 1 );
Matrix base1( 52 , 1 );
Matrix base2( 52 , 1 );
Matrix fun( 52 , 52 );
for( int i = 0 ; i < 52 ; i++ ) base.a[i][0] = 1;
for( int i = 0 ; i < 26 ; i++ ){
for( int j = 0 ; j < 27 + i ; j++ )
fun.a[i][j] = 1;
}
for( int i = 26 ; i < 52 ; i++ ){
for( int j = 51 ; j >= i - 26 ; j-- )
fun.a[i][j] = 1;
}
fun = fast_pow( fun , n - 1 );
base1 = fun * base ;
lint sum1 = 0;
cls(fun.a);
for( int i = 0 ; i < 52 ; i++ ) sum1 = ( sum1 + base1.a[i][0] ) % mod;
for( int i = 0 ; i < 26 ; i++ ){
for( int j = 0 ; j < 26 + i; j++ )
fun.a[i][j] = 1;
}
for( int i = 26 ; i < 52 ; i++ ){
for( int j = 51 ; j >= i - 25 ; j-- )
fun.a[i][j] = 1;
}
fun = fast_pow( fun , n - 1 );
base2 = fun * base ;
lint sum2 = 0;
for( int i = 0 ; i < 52 ; i++ ) sum2 = ( sum2 + base2.a[i][0] ) % mod;
lint ans = ( sum1 - sum2 + mod ) % mod ;
cout << ans << endl;
}
int main(){
//freopen("output.txt","w+",stdout);
int t ; cin >> t;
while( t-- ){
cin >> n ;
solve();
}
return 0;
}
ZOJ 3690 & HDU 3658 (矩阵高速幂+公式递推)的更多相关文章
- HDU 1757 矩阵快速幂加速递推
题意: 已知: 当x<10时:f(x)=x 否则:f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + --+ a9 * f(x-10); 求:f(x ...
- HDU - 2604 矩阵快速幂 字符串递推 两种解法
记dp[i]为长度i且符合题意的方案数,dp[n]就是解 符合方案的是不含fmf和fff子串的字符串 考虑如何从前面几项递推出后面第i项 (★表示存在生成的非法方案)←其实没啥用处 i=1时 m③ f ...
- HDU 5950 - Recursive sequence - [矩阵快速幂加速递推][2016ACM/ICPC亚洲区沈阳站 Problem C]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 Farmer John likes to play mathematics games with ...
- CH 3401 - 石头游戏 - [矩阵快速幂加速递推]
题目链接:传送门 描述石头游戏在一个 $n$ 行 $m$ 列 ($1 \le n,m \le 8$) 的网格上进行,每个格子对应一种操作序列,操作序列至多有 $10$ 种,分别用 $0 \sim 9$ ...
- hdu 2842(矩阵高速幂+递推)
题意:一个中国环的游戏,规则是一个木棒上有n个环.第一个环是能够任意放上或拆下的,剩下的环x假设想放上或拆下必须前一个环x-1是放上的且前x-2个环所有是拆下的,问n个环最少多少次操作能够所有拆掉. ...
- CH3401 石头游戏(矩阵快速幂加速递推)
题目链接:传送门 题目: 石头游戏 0x30「数学知识」例题 描述 石头游戏在一个 n 行 m 列 (≤n,m≤) 的网格上进行,每个格子对应一种操作序列,操作序列至多有10种,分别用0~9这10个数 ...
- HDU5950 Recursive sequence (矩阵快速幂加速递推) (2016ACM/ICPC亚洲赛区沈阳站 Problem C)
题目链接:传送门 题目: Recursive sequence Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total ...
- BZOJ4547 Hdu5171 小奇的集合 【矩阵快速幂优化递推】
BZOJ4547 Hdu5171 小奇的集合 Description 有一个大小为n的可重集S,小奇每次操作可以加入一个数a+b(a,b均属于S),求k次操作后它可获得的S的和的最大值.(数据保证这个 ...
- [bzoj1009](HNOI2008)GT考试 (kmp+矩阵快速幂加速递推)
Description 阿 申准备报名参加GT考试,准考证号为N位数X1X2....Xn(0<=Xi<=9),他不希望准考证号上出现不吉利的数字.他的不吉利数学 A1A2...Am(0&l ...
随机推荐
- MongoDB Shell (mongo)
https://docs.mongodb.com/getting-started/shell/client/ The mongo shell is an interactive JavaScript ...
- Laravel-路由组和中间件
Laravel-路由组和中间件 标签(空格分隔): php 定义路由组 Route::group(['prefix'=>'Anime'], function(){ Rout::match(['g ...
- Scala基础简述
* Scala基础简述 本文章作为Scala快速学习的教程,前提环境是:我假设在此之前,你已经学会了Java编程语言,并且我们以随学随用为目标(在此不会深度挖掘探讨Scala更高级层次的知识).其中语 ...
- checkbox和文字不再同一水平线上
为了演示效果,我故意将文字变为12px,将复选框变大,看到的效果就是下面的那样 然后,我们通过给复选框添加vertical-align:middle:让文字和复选框达到同一水平线的效果
- rails 开发随手记 9
ruby 根据名称确定类Object.const_get 一个简单的应用,在header中的,个人信息链应该链接到对应的用户类型的页面上. <%= link_to "个人信息" ...
- linq replace with single call to FirstOrDefault 解决使用resharper产生的警告
使用resharper时对linq使用的FirstOrDefault 一直产生一个警告, 解决办法: 参考The Linq FirstOrDefault() Method and Null Resul ...
- [笔记-图论]Dijkstra
用于求正权有向图 上的 单源最短路 优化后时间复杂度O(mlogn) 模板 // Dijkstra // to get the minumum distance with no negtive way ...
- POJ 3539 Elevator(同余类BFS)
题意 有一部电梯,最初停在1层. 电梯有4个按键,上升a,b,c层,回到一层. 求从一层出发.能到达1~h的哪些楼层. (h<=1018,a,b,c<=105) 题解 这种h能大的图论,一 ...
- java 爬虫在 netbeans 里执行和单独执行结果不一样
在做内容測试的时候.发现我的爬虫(前些文章略有提及)在 netbeans 里面可以成功爬取网页内容,而单独执行时,给定一个 url,爬到的网页却与在浏览器里面打开 url 的网页全然不一样,这是一个非 ...
- Lesson 2 Building your first web page: Part 3
Time to build your first HTML page by hand I could go on with more theory and send half of you to sl ...