1 条题解

  • 0
    @ 2024-11-3 17:10:40

    这是一道打表题,题目需要你找规律来输出C[ni][ki]C[n_i][k_i]的值,在给与你一段程序后,你需要找这个程序有什么特点,当你无法一眼看出时,你需要的就是按照题目要求(即将题目程序跑一遍)输出对应答案所需要范围内的数,看这些数在排列上有什么特点。

    所以让我们先运行这个程序:

    int c[505][505];
        for(int i = 0; i < 20; i++) {
            c[i][0] = c[i][i] = 1;
        }
        for (int i = 2; i < 20; i++) {
            for (int j = 1; j < i; j++) {
                c[i][j] = c[i][j - 1] + c[i - 1][j - 1];
            }
        }
        for(int i = 0; i < 20; i++) {
            for(int j = 0; j < i; j++) {
                cout << c[i][j] << " ";
            }
            cout << endl;
        }
    

    在运行输出后会发现打表得到的数呈下方这样排列。

    1
    1 2
    1 2 4
    1 2 4 8
    1 2 4 8 16
    1 2 4 8 16 32
    1 2 4 8 16 32 64
    1 2 4 8 16 32 64 128
    1 2 4 8 16 32 64 128 256
    1 2 4 8 16 32 64 128 256 512
    1 2 4 8 16 32 64 128 256 512 1024
    1 2 4 8 16 32 64 128 256 512 1024 2048
    1 2 4 8 16 32 64 128 256 512 1024 2048 4096
    1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192
    1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384
    1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768
    1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536
    1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072
    1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 131072 262144
    

    故,我们能够规律总结得到,我们需要输出的答案,C[ni][ki]C[n_i][k_i]的值就是 2 的 kik_i 次方

    #include<bits/stdc++.h>
    #define int long long
    #define pii pair<int,int>
    #define endl '\n'
    using namespace std;
    const int mod = 1e9 + 7, inf = 1e18, N = 2e5 + 5;
    int c[N];
    int ksm(int a, int b) {//通过快速幂优化运算时间,在运算时记得进行取模操作
        int res = 1;
        while(b) {
            if(b & 1) res = res * a % mod;
            a = a * a % mod;
            b >>= 1;
        }
        return res;
    }
    
    void solve() {
        int t;
        cin >> t;
        vector<int> a(t);
        int mx = 0;
        for(int i = 0; i < t; i++) {
            cin >> a[i];
            mx = max(mx, a[i]);
        }
        vector<int> b(t);
        for(int i = 0; i < t; i++) {
            cin >> b[i];
        }//因为最多1e5次询问,所以我们可以选择线下将所有答案都跑出来,进行O(1)输出
        for(int i = 0; i <= 1e5 + 5; i++) {
            c[i] = ksm(2, i);
        }
        for(int i = 0; i < t; i++) {
            cout << c[b[i]] % mod << endl;
        }
        //打表程序
    //    int c[505][505];
    //    for(int i = 0; i < 20; i++) {
    //        c[i][0] = c[i][i] = 1;
    //    }
    //    for (int i = 2; i < 20; i++) {
    //        for (int j = 1; j < i; j++) {
    //            c[i][j] = c[i][j - 1] + c[i - 1][j - 1];
    //        }
    //    }
    //    for(int i = 0; i < 20; i++) {
    //        for(int j = 0; j < i; j++) {
    //            cout << c[i][j] << " ";
    //        }
    //        cout << endl;
    //    }
    }
    
    signed main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr),cout.tie(nullptr);
        int T = 1;
    //    cin >> T;
        while(T--) {
            solve();
        }
        return 0;
    }
    
    • 1

    信息

    ID
    1041
    时间
    1000ms
    内存
    256MiB
    难度
    7
    标签
    递交数
    17
    已通过
    8
    上传者