zoukankan      html  css  js  c++  java
  • ZOJ 3841 Cards

    Cards

    Time Limit: 2 Seconds      Memory Limit: 65536 KB

    EdwardGy has a poker (52 cards excluding jokers). One day, he saw some cards was lined up on the table. He wanted to know how many ways he can place all of the rest cards in a row with a lower lexicographic order than the line of the cards which were already on the table.

    The lexicographic order of the cards is A < 2 < 3 < 4 < 5 < 6 < 7 < 8 < 9 < 10 < J < Q < K, and the colors should be ignored. If the cards already on the table is

    AA22334455667788991010JJKKK

    EdwardGy have only five ways:

    AA22334455667788991010JJQQQQK
    AA22334455667788991010JJQQQKQ
    AA22334455667788991010JJQQKQQ
    AA22334455667788991010JJQKQQQ
    AA22334455667788991010JJKQQQQ

    Input

    There are multiple test cases. Each test case has only one line, a valid string indicating the cards on the table.

    Output

    For each test cases, output the remainder of the number of ways modulo by 109+7.

    Sample Input

    AA22334455667788991010JJKKK
    

    Sample output

    5
    



    直接枚举每一个位置。。小于给出的牌。然后将剩下的牌进行排列组合。。

    直接枚举版:

    #include <bits/stdc++.h>
    using namespace std ;
    const int mod = 1e9+7;
    int cnt[15] , num[60] , cnt1[15] ;
    long long f[60] , inv[60] ;
    string s ;
    
    int Decode( char op ) {
        if( op == 'A' ) return 1 ;
        else if( op == '1' ) return 10 ;
        else if( op == 'J' ) return 11 ;
        else if( op == 'Q' ) return 12 ;
        else if( op == 'K' ) return 13 ;
        else return ( op-'0' );
    }
    
    void Delete0( string &s ) {
        string res = "" ;
        for( int i = 0 ; i < s.length() ; ++i ) if( s[i] != '0' ) {
            res += s[i] ;
        }
        s = res ;
    }
    
    long long mypow( long long  a , long long  b ) {
        long long res = 1 ;
        while( b ) {
            if( b&1 ) res = res * a % mod ;
            a = a * a % mod ;
            b >>= 1 ;
        }
        return res ;
    }
    
    void init() {
        f[0] = inv[0] = 1 ;
        for( int i = 1 ; i < 55 ; ++i ) {
            f[i] = f[i-1] * i % mod ;
            inv[i] = mypow( f[i] , mod - 2 ) ;
        }
    }
    
    int main() {
        #ifdef LOCAL
            freopen("in.txt","r",stdin);
        #endif // LOCAL
        ios::sync_with_stdio(0);
        init();
        while( cin >> s ) {
            memset( cnt , 0 , sizeof cnt ) ;
            Delete0(s);
            int n = s.length() ;
            for( int i = 0 ; i < n ; ++i ) {
                num[i] = Decode( s[i] );
                cnt[ num[i] ]++;
            }
            if( n == 52 ) { cout << '0' << endl ; continue ;}
            for( int i = 1 ; i <= 13 ; ++i ) cnt[i] = 4 - cnt[i] ;   // rest card
            long long ans = 0 ;
            int rest_card = 52 - n , c = rest_card , tag = 0 , pos = -1 ;
            for( int i = 0 ; i < n ; ++i ) {
                pos = i ;
                for( int j = 1 ; j < num[i] ; ++j ) if( cnt[j] > 0 ){
                    tag = 1 ;
                    long long tmp = f[ rest_card - i - 1 ] ;
                    for( int k = 1 ; k <= 13 ; ++k ) if( cnt[k] > 0 ) {
                        if( k == j ) tmp = tmp * inv[ cnt[k] - 1 ] % mod ;
                        else tmp = tmp * inv[ cnt[k] ] % mod ;
                    }
                    ans = ( ans + tmp ) % mod ;
                }
                if( !cnt[num[i]] ) {
                    break ;
                } else {
                    cnt[ num[i] ]-- ; c--;
                }
            }
            if( !tag ) {
                if( c > 0 || pos == n - 1 ) cout << '0' << endl ;
                else cout << '1' << endl ;
            } else {
                cout << ans << endl ;
            }
        }
        return 0 ;
    }
    View Code

    dfs:

    #include <bits/stdc++.h>
    using namespace std ;
    const int mod = 1e9+7;
    int cnt[15] , num[60] , cnt1[15] ;
    long long f[60] , inv[60] ;
    string s ;
    
    int Decode( char op ) {
        if( op == 'A' ) return 1 ;
        else if( op == '1' ) return 10 ;
        else if( op == 'J' ) return 11 ;
        else if( op == 'Q' ) return 12 ;
        else if( op == 'K' ) return 13 ;
        else return ( op-'0' );
    }
    
    void Delete0( string &s ) {
        string res = "" ;
        for( int i = 0 ; i < s.length() ; ++i ) if( s[i] != '0' ) {
            res += s[i] ;
        }
        s = res ;
    }
    
    long long mypow( long long  a , long long  b ) {
        long long res = 1 ;
        while( b ) {
            if( b&1 ) res = res * a % mod ;
            a = a * a % mod ;
            b >>= 1 ;
        }
        return res ;
    }
    
    void init() {
        f[0] = inv[0] = 1 ;
        for( int i = 1 ; i < 55 ; ++i ) {
            f[i] = f[i-1] * i % mod ;
            inv[i] = mypow( f[i] , mod - 2 ) ;
        }
    }
    long long ans ;
    int n , rest_card ;
    void dfs( int dep , int rest ) {
        if( dep == n ) return ;
        if( rest == 0 ) {
            ans = ( ans + 1 ) % mod ;
            return ;
        }
        for( int i = 1 ; i < num[dep] ; ++i ) if( cnt[i] > 0 ) {
            long long tmp = f[ rest - 1 ] ;
            for( int k = 1 ; k <= 13 ; ++k ) if( cnt[k] > 0 ) {
                if( k == i ) tmp = tmp * inv[ cnt[k] - 1 ] % mod ;
                else tmp = tmp * inv[ cnt[k] ] % mod ;
            }
            ans = ( ans + tmp ) % mod ;
        }
        if( cnt[ num[dep] ] > 0 ) {
            cnt[ num[dep] ]-- ;
            dfs( dep + 1 , rest - 1 ) ;
        }
    }
    
    int main() {
        #ifdef LOCAL
            freopen("in.txt","r",stdin);
        #endif // LOCAL
        ios::sync_with_stdio(0);
        init();
        while( cin >> s ) {
            memset( cnt , 0 , sizeof cnt ) ;
            Delete0(s);
            n = s.length() ;
            for( int i = 0 ; i < n ; ++i ) {
                num[i] = Decode( s[i] );
                cnt[ num[i] ]++;
            }
            for( int i = 1 ; i <= 13 ; ++i ) cnt[i] = 4 - cnt[i] ;
            rest_card = 52 - n ;
            ans = 0 ; dfs( 0 , rest_card );
            cout << ans << endl ;
        }
        return 0 ;
    }
    View Code
  • 相关阅读:
    C++ 模板特化
    左值引用,右值引用以及移动语义
    搜狐2016研发工程师编程题
    java配置环境变量与常用技巧
    java在线聊天项目 使用SWT快速制作登录窗口,可视化窗口Design 更换窗口默认皮肤(切换Swing自带的几种皮肤如矩形带圆角)
    c++中的结构体:声明 定义 初始化
    用指针变量作函数形参接收数组地址,解决10个整数按由小到大顺序排序问题
    java在线聊天项目 实现基本聊天功能后补充的其他功能详细需求分析 及所需要掌握的Java知识基础 SWT的激活方法,swt开发包下载,及破解激活码
    java在线聊天项目1.0版 异常处理——开启多个客户端,关闭一个客户端后,在其他客户端中再发出信息会出现异常的处理
    第十一次作业
  • 原文地址:https://www.cnblogs.com/hlmark/p/4365540.html
Copyright © 2011-2022 走看看