• 杭电多校第十场 hdu6432 Cyclic 打表找规律


     Cyclic

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
    Total Submission(s): 193    Accepted Submission(s): 125


    Problem Description
    Count the number of cyclic permutations of length n with no continuous subsequence [i, i + 1 mod n].
    Output the answer modulo 998244353.
     
    Input
    The first line of the input contains an integer T , denoting the number of test cases.
    In each test case, there is a single integer n in one line, denoting the length of cyclic permutations.
    1 ≤ T ≤ 20, 1 ≤ n ≤ 100000
     
    Output
    For each test case, output one line contains a single integer, denoting the answer modulo 998244353.
     
    Sample Input
    3 4 5 6
     
    Sample Output
    1 8 36
     

     题意:求满足一个方向的(a[i]+1)%n!=a[i+1]循环数列的n个数字组成的数列的可能性

    分析:首先按照题目的意思做一个全排列列出前面几项的可能数

    #include <map>
    #include <set>
    #include <stack>
    #include <cmath>
    #include <queue>
    #include <cstdio>
    #include <vector>
    #include <string>
    #include <bitset>
    #include <cstring>
    #include <iomanip>
    #include <iostream>
    #include <algorithm>
    #define ls (r<<1)
    #define rs (r<<1|1)
    #define debug(a) cout << #a << " " << a << endl
    using namespace std;
    typedef long long ll;
    const ll maxn = 1e5+10;
    const ll mod = 998244353;
    const double pi = acos(-1.0);
    const double eps = 1e-8;
    ll a[maxn];
    int main() {
        ios::sync_with_stdio(0);
        ll T, n;
        scanf("%lld",&T);
        while( T -- ) {
            scanf("%lld",&n);
            for( ll i = 0; i < n; i ++ ) {
                a[i] = i+1;
            }
            ll cnt = 0;
            while(next_permutation(a,a+n)) {
                bool flag = true;
                for( ll i = 0; i < n-1; i ++ ) {
                    ll t;
                    if( (a[i]+1)%n == 0 ) {
                        t = a[i]+1;
                    } else {
                        t = (a[i]+1)%n;
                    }
                    if( t == a[i+1] ) {
                        flag = false;
                        break;
                    }
                }
                ll t;
                if( (a[n-1]+1)%n == 0 ) {
                    t = a[n-1]+1;
                } else {
                    t = (a[n-1]+1)%n;
                }
                if( t == a[0] ) {
                    flag = false;
                }
                if(flag) {
                    cnt ++;
                    /*for( ll i = 0; i < n; i ++ ) {
                        cout << a[i] << " ";
                    }
                    cout << endl;*/
                }
            }
            printf("%lld
    ",cnt/n);
        }
        return 0;
    }
    

      接着找规律

    AC代码:

    #include <map>
    #include <set>
    #include <stack>
    #include <cmath>
    #include <queue>
    #include <cstdio>
    #include <vector>
    #include <string>
    #include <bitset>
    #include <cstring>
    #include <iomanip>
    #include <iostream>
    #include <algorithm>
    #define ls (r<<1)
    #define rs (r<<1|1)
    #define debug(a) cout << #a << " " << a << endl
    using namespace std;
    typedef long long ll;
    const ll maxn = 1e5+10;
    const ll mod = 998244353;
    const double pi = acos(-1.0);
    const double eps = 1e-8;
    ll a[maxn];
    int main() {
        ios::sync_with_stdio(0);
        ll T, n;
        a[1] = 0, a[2] = 0, a[3] = 0, a[4] = 1, a[5] = 8, a[6] = 36;
        for( ll i = 7; i <= maxn-10; i ++ ) {
            a[i] = ((i-3)*a[i-1]%mod+(i-2)*(2*a[i-2]+a[i-3])%mod)%mod;
        }
        scanf("%lld",&T);
        while( T -- ) {
            scanf("%lld",&n);
            printf("%lld
    ",a[n]);
        }
        return 0;
    }
    

      

    彼时当年少,莫负好时光。
  • 相关阅读:
    数据结构与算法部分习题题解
    Codeforces Round #372 +#373 部分题解
    KMP算法的正确性证明及一个小优化
    后记
    BZOJ 4089:[Sdoi2015]graft(SDOI 2015 Round 2 Day 2)
    BZOJ 4085:[Sdoi2015]bigyration(SDOI 2015 round 2 Day 1)
    使用 async await 封装微信小程序HTTP请求
    mongo创建数据库和用户
    把实体bean对象转换成DBObject工具类
    Trident整合MongoDB
  • 原文地址:https://www.cnblogs.com/l609929321/p/9522804.html
Copyright © 2020-2023  润新知