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
  • 相关阅读:
    Java中常见的几种加密算法
    关于redis中过期的key的处理
    Redis 的内存淘汰策略
    AES加密算法原理(加密过程)
    springboot中从配置文件中读取相关参数值
    springboot中切换配置(多个配置文件--生产、开发、测试)
    mysql跨库查询数据
    SpringBoot2.0 的默认连接池----Hikari
    Druid连接池中的缓存机制
    Opus编解码练习
  • 原文地址:https://www.cnblogs.com/hlmark/p/4365540.html
Copyright © 2011-2022 走看看