2019/11 第一次訓練賽知識點以及補題

1.快速冪

二進制快速冪

ll qpow(ll a, ll b) {
    ll ans = 1;
    while(b) {
        if(b&1) ans = ans * a % mod;
        a = a * a %mod;
        b>>=1;
    }
    return ans;
}

十進制快速冪

ll tqpow(ll a) {
    ll ans = 1, s = a;
    while(t>=0) {
        ll cnt = c[t] - '0', cur = s;
        for(int i = 1;i <= cnt; i++)
            ans = ans * s % mod;
        for(int i = 1; i < 10; i++)
            cur = cur * s % mod;
        s = cur;
        ans %= mod;
        t--;
    }
    return ans;
}

例題:http://acm.hdu.edu.cn/showproblem.php?pid=1061

(十進制快速冪 AC代碼)

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <stdlib.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e5+7;
char c[MAXN];
ll b,t;
ll mod = 10;
ll tqpow(ll a) {
    ll ans = 1, s = a;
    while(t>=0) {
        ll cnt = c[t] - '0', cur = s;
        for(int i = 1;i <= cnt; i++)
            ans = ans * s % mod;
        for(int i = 1; i < 10; i++)
            cur = cur * s % mod;
        s = cur;
        ans %= mod;
        t--;
    }
    return ans;
}
ll T;
int main() {
    cin >> T;
    while(T--) {
       cin >> c;
       t = strlen(c);
        t--;
        ll qq = 1;
       b = 0;
       for(int i = t; i>=0;i--) {
           b += qq * (c[i] - '0');
           qq *= 10;
       }
       cout << tqpow(b)<<endl;
    }
    return 0;
}

 

2.矩陣快速冪

題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1575(求n*n矩陣的k次方%mod)

矩陣快速冪AC

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <stdlib.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e2+7;
const int mod = 9973;
struct martrix {
    int a[MAXN][MAXN];
}m1,m2;
ll n, k;
martrix mul(martrix m1, martrix m2) {
    martrix m3;
    memset(m3.a, 0, sizeof m3.a);
    for(int i = 1;i <= n;i++) {
        for(int j = 1; j <= n; j++) {
            for(int k = 1;k <= n; k++) {
                m3.a[i][j] += m1.a[i][k]*m2.a[k][j];
                m3.a[i][j] %= mod;
            }
        }
    }
    return m3;
}
martrix mpow(martrix m1) {
    martrix res;
    memset(res.a, 0, sizeof res.a);
    for(int i = 1; i <= n; i++) res.a[i][i] = 1;
    while(k!=0) {
        if(k%2!=0) {
            res = mul(res,m1);
            k--;
        }
        else {
            m1 = mul(m1,m1);
            k>>=1;
        }
    }
    return res;
}
int main() {
    int t;
    cin >> t;
    while(t--) {
        int sum = 0;
        cin >> n >> k;
        for(int i = 1;i <= n;i++) {
            for(int j = 1; j <= n; j++) {
                cin >> m1.a[i][j];
                m1.a[i][j]%=mod;
            }
        }
        m2 = mpow(m1);
        for(int i = 1; i <= n; i++) {
            sum += m2.a[i][i];
            sum %= mod;
        }
        cout << sum << endl;
    }
}

 

題目:http://acm.hdu.edu.cn/showproblem.php?pid=2157(本題見詳解)

https://blog.csdn.net/Sensente/article/details/103234396

 

3.字符串題目

#include <iostream>
using namespace std;
int n;
char s[50+5];
int num1, num0;
int onum1,onum0;
int lens;
bool f;
int main() {
    cin >> n;
    if(n == 1) {
        cin >> s;
        int len = strlen(s);
        for(int i = 0, j = len -1; i<len/2,j>len/2;i++,j--) {
            if(s[i] == s[j]) f = 0;
            else {f = 1;break;}
        }
        if(f) cout << 0 << endl;
        else cout << 1 << endl;
    }else {
        int k = n;
        while(k--) {
            cin >> s;
            int len = strlen(s);
            lens += len/2;
            for(int i = 0; i < len; i++) {
                if(s[i] == '0') num0++;
                else num1++;
            }
        }
        num0 /= 2;
        num1 /= 2;
        //cout << lens <<" " << num1 << " "<<num0 << endl;
        if(lens <= num1 + num0) cout << n << endl;
        else cout << n-1<<endl;
    }

    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章