HDU2842—Chinese Rings

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2842

题目意思:一把一个n连环的前n个拿下来,一个个n连环,要把第k个拿下来,需要把前n-2个拿下来,并留下第n-1个环,然后花一步把第n个拿下来,然后为了把第n-1个环拿下来,我们又需要把前n-2个放回去(逆过程),从而把这个问题转化成把前n-1个环拿下来。

思路:根据上面的分析,我们很容易的可以写出递推式子,f[n]=2*f[n-2]+f[n-1]+1,一个带常数项的矩阵快速幂,题目意思有点难懂,忽略了,要把前n-2个放回去,还是逆过程,一直没有搞得特别懂,感觉有点没道理。

代码:

//Author: xiaowuga
#include <bits/stdc++.h>
#define maxx INT_MAX
#define minn INT_MIN
#define inf 0x3f3f3f3f
#define n 3
#define MOD 200907
using namespace std;
typedef long long ll;
struct Matrix{
    ll mat[3][3];
    Matrix operator * (const Matrix & m) const{
        Matrix tmp;
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++){
                tmp.mat[i][j]=0;
                for(int k=0;k<n;k++){
                    tmp.mat[i][j]+=mat[i][k]*m.mat[k][j]%MOD;
                    tmp.mat[i][j]%=MOD;
                }
            }
        return tmp;
    }
};
Matrix POW(Matrix m,ll k){
    Matrix ans;
    memset(ans.mat,0,sizeof(ans.mat));
    for(int i=0;i<n;i++) ans.mat[i][i]=1;
    while(k){
        if(k&1) ans=ans*m;
        k/=2;
        m=m*m;
    }
    return ans;
}
int main() {
    ios::sync_with_stdio(false);cin.tie(0);
    ll num;
    while(cin>>num&&num){
        Matrix m;
        memset(m.mat,0,sizeof(m.mat));
        m.mat[0][0]= m.mat[0][2]= m.mat[1][0]= m.mat[2][2]=1;
        m.mat[0][1]=2;
        ll f[3]={2,1,1};
        Matrix ans;
        if(num<=2) cout<<(num==2?2:1)<<endl;
        else{
            ans=POW(m,num-2);
            ll sum=0;
            for(int i=0;i<n;i++){
                sum+=ans.mat[0][i]*f[i]%MOD;
            }
            cout<<sum%MOD<<endl;
        }
    }
    return 0;
}

时间: 2024-11-06 04:08:05

HDU2842—Chinese Rings的相关文章

HDU 2842 Chinese Rings (带常数矩阵+矩阵快速幂)

HDU 2842 Chinese Rings (带常数矩阵+矩阵快速幂) ACM 题目地址:HDU 2842 Chinese Rings 题意: 一种中国环,解开第k个环需要先解开前(k-2)个环,并留有第(k-1)环.问解开n环最少需要几步. 分析: 设f(n)表示解开n环. 1. 由于游戏规则,解开n环不能一下子把n-1全解开了,否则第n个就没法拿掉了. 2. 得先拿掉第n个:先完成f(n-2),然后再拿掉第n环. 3. 然后放回前(n-2),其实这也是f(n-2),因为是一个逆的过程. 4

HDU - 2842 Chinese Rings

Description Dumbear likes to play the Chinese Rings (Baguenaudier). It's a game played with nine rings on a bar. The rules of this game are very simple: At first, the nine rings are all on the bar. The first ring can be taken off or taken on with one

Chinese Rings (九连环+矩阵快速幂)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2842 题目: Problem Description Dumbear likes to play the Chinese Rings (Baguenaudier). It’s a game played with nine rings on a bar. The rules of this game are very simple: At first, the nine rings are all

HDU 2842 Chinese Rings(矩阵快速幂+递推)

题目地址:HDU 2842 这个游戏是一个九连环的游戏. 假设当前要卸下前n个环.由于要满足前n-2个都卸下,所以要先把前n-2个卸下,需要f(n-2)次.然后把第n个卸下需要1次,然后这时候要卸下第n-1个,然后此时前n-2个都已经被卸下了.这时候把前n-2个都卸下与都装上所需的次数是一样的,因为卸下与装上的规则是一样的.所以又需要f(n-2)次,这时候前n-1个都在上面,卸下前n-1个需要f(n-1)次. 所以,总共需要2*f(n-2)+f(n-1)+1次. 然后构造如下矩阵. 1,2,1

HDU 2842 Chinese Rings(矩阵高速功率+递归)

职务地址:HDU 2842 这个游戏是一个九连环的游戏. 如果当前要卸下前n个环.由于要满足前n-2个都卸下,所以要先把前n-2个卸下.须要f(n-2)次.然后把第n个卸下须要1次,然后这时候要卸下第n-1个.然后此时前n-2个都已经被卸下了.这时候把前n-2个都卸下与都装上所需的次数是一样的.由于卸下与装上的规则是一样的. 所以又须要f(n-2)次.这时候前n-1个都在上面,卸下前n-1个须要f(n-1)次. 所以.总共须要2*f(n-2)+f(n-1)+1次. 然后构造例如以下矩阵. 1,2

HDU - 2842 Chinese Rings 矩阵快速幂

题目大意:有n个环,刚开始都是on状态,可以随便改第一个的状态,其他的话,只有前面的k个off掉,第k+1个on,才可以操控第k+2个的状态 问要让n个都变成off状态,需要多少步 解题思路:要让第n个是off,只有先让前n-2个都off,也就是说需要f(n-2)步了.然后再把第n个变成off,需要1步. 之后要把剩下的n-1个都off掉的话,只有先把前面的n-2个都变成on,才能去操控第n-1个,也就是说剩下n-1个都是on的,现在的问题就转成了怎么把n-1都变成off,共需要f(n-1)步

hdu2842---Chinese Rings(矩阵)

Problem Description Dumbear likes to play the Chinese Rings (Baguenaudier). It's a game played with nine rings on a bar. The rules of this game are very simple: At first, the nine rings are all on the bar. The first ring can be taken off or taken on

HDU 专题分类

[背包问题] 2602 Bone Collector 1114 Piggy-Bank 1203 I NEED A OFFER! 1171 Big Event in HDU 1059 Dividing 2844 Coins 2191 悼念512汶川大地震遇难同胞--珍惜现在,感恩生活 2159 FATE 1561 The more, The Better 1011 Starship Troopers 2639 Bone Collector II 3033 I love sneakers! 2955

IEF could not decode Chinese character in IE history well

My friend is working on some case, and she looks not in the mood. I ask her what's going on. She wants me to look at the screenshot as below. That's why she is upset...IEF could not decode Chinese character in IE history well, so the filenames in Chi