第17场-快乐AC赛

A - 看我,看我,我最简单了

POJ - 2387

这道题是以前记录过的最短路板子题,然而我还是脑抽用Floyd交了一发

解题报告:https://www.cnblogs.com/RioTian/p/12879353.html

B - 我也很简单

HDU - 1061

快速幂,注意对 (10) 取模即可

// Author : RioTian
// Time : 20/11/05
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 10;
int _, n;
ll qpow(ll a, ll b) {
    ll ans = 1;
    a %= mod;
    for (; b; a = a * a % mod, b >>= 1)
        if (b & 1) ans = ans * a % mod;
    return ans;
}
int main() {
    // freopen("in.txt", "r", stdin);
    // ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> _;
    while (_--) {
        cin >> n;
        cout << qpow(n, n) << endl;
    }
    return 0;
}

C - 我更简单了,一眼就可以看穿我

CodeForces - 450B

先说下规律吧

[f1=x,f2=y,∀i(i≥2)fi=fi−1+fi+1 ]

我们可以转化递推式

[f_{i+1}=f_{i}-f{i−1}\即 f_i=f_{i−1}−f_{i−2} ]

此时可以先打个表推一下前面的答案。很容易发现是循环节为 (6) 的循环,这样的话直接推导前6位即可

(注意点:步步取模)

ll dp[20], n;
int main() {
    // freopen("in.txt","r",stdin);
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> dp[1] >> dp[2] >> n;
    dp[1] = (dp[1] + mod) % mod;
    dp[2] = (dp[2] + mod) % mod;
    for (int i = 3; i <= 6; ++i) dp[i] = (dp[i - 1] - dp[i - 2] + mod) % mod;
    dp[0] = dp[6];
    cout << dp[n % 6];
}

然后再考虑正解:矩阵快速幂 (相关博客尚未发布)

[由f_i=f_{i−1}−f_{i−2}\对于n>=2\egin{pmatrix}f_n\f_{n-1}end{pmatrix} = egin{pmatrix}1&-1\1&0end{pmatrix}=egin{pmatrix}y\xend{pmatrix} ]

// Author : RioTian
// Time : 20/11/05
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;
const ll mod = 1e9 + 7;
ll x, y, n;
struct Matrix {
    ll mat[3][3];
    Matrix operator*(const Matrix &b) const {
        Matrix ans;
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) {
                ans.mat[i][j] = 0;
                for (int k = 0; k < 2; k++) {
                    ans.mat[i][j] =
                        (ans.mat[i][j] + mat[i][k] * b.mat[k][j] % mod + mod) %
                        mod;
                }
            }
        }
        return ans;
    }
};
Matrix q_pow(Matrix a, ll b) {
    Matrix ans;
    memset(ans.mat, 0, sizeof(ans.mat));
    for (int i = 0; i < 2; i++) {
        ans.mat[i][i] = 1;
    }
    while (b) {
        if (b & 1) ans = ans * a;
        b >>= 1;
        a = a * a;
    }
    return ans;
}
int main() {
    scanf("%lld%lld", &x, &y);
    scanf("%lld", &n);
    if (n == 1) {
        printf("%lld
", (x % mod + mod) % mod);
        return 0;
    }
    if (n == 2) {
        printf("%lld
", (y % mod + mod) % mod);
        return 0;
    }
    Matrix ans;
    ans.mat[0][0] = 1;
    ans.mat[0][1] = -1;
    ans.mat[1][0] = 1;
    ans.mat[1][1] = 0;
    ans = q_pow(ans, n - 2);
    ll ret =
        ((ans.mat[0][0] * y % mod + ans.mat[0][1] * x % mod) % mod + mod) % mod;
    printf("%lld
", ret);
    return 0;
}

D - 可以看我一下

POJ - 2421

为瞄又是以前做过的题

解题报告:https://www.cnblogs.com/RioTian/p/13380764.html#3constructing-roads

E - 我可以写

HDU - 2588

题意很容易理解,但也一下子误导我进入了一个误区,正确的思路应该是在 (gcd(x,N)geqslant M) 改为 (gcd(x/M,N/M) == 1)(Rightarrow) 求 不大于 (N/M)且 与其互质的 (N/M) 的个数 即求 (ϕ(N/M))

// Author : RioTian
// Time : 20/11/05
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int _, n, m;
ll euler(ll x) {
    ll res = x;
    for (int i = 2; i * i <= x; i++)
        if (x % i == 0) {
            res = res / i * (i - 1);
            while (x % i == 0) x /= i;
        }
    if (x > 1) res = res / x * (x - 1);
    return res;
}

int main() {
    // freopen("in.txt","r",stdin);
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> _;
    while (_--) {
        cin >> n >> m;
        ll ans = 0;
        for (ll i = 1; i * i <= n; i++) {
            if (n % i == 0) {  // i是n的因数
                if (i >= m) ans += euler(n / i);
                // i*(n/i)==n,判断i对应的另一个因数是否符合
                if ((n / i) >= m && n / i != i) ans += euler(i);
            }
        }
        cout << ans << endl;
    }
    return 0;
}

F - 我最正常了

HDU - 1597

直接去找是第几个序列,找到之后因为都是1~9循环,所以取余就能得到结果咧

// Author : RioTian
// Time : 20/11/05
#include <bits/stdc++.h>
using namespace std;
int t, b, n;
int main() {
    //freopen("in.txt", "r", stdin);
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> t;
    while (t--) {
        cin >> b;
        n = 1;
        while (b > n) b -= n, n++;
        if (b % 9 == 0)
            cout << 9 << endl;
        else
            cout << b % 9 << endl;
    }
}

G - 不要看我,我最难了,你们肯定不能写出来

计蒜客 - 31434

这道题想了很久(和题目名杠上了。。),最后还是放弃。

// Author : RioTian
// Time : 20/11/05
#include <bits/stdc++.h>
using namespace std;
#define ms(a, b) memset(a, b, sizeof a)
typedef long long ll;
const ll MOD = 998244353;
const int maxn = 2000 + 10;
int w, h, k;
ll sum[maxn][maxn];
int main() {
    //这道题是文件读入,下面两句一定要加
    freopen("racing.in", "r", stdin);
    freopen("racing.out", "w", stdout);

    cin >> w >> h >> k;
    ms(sum, 0);
    sum[1][1] = 1;
    for (int i = 1; i <= h; i++) {
        for (int j = 1; j <= w; j++) {
            if (i == 1 && j == 1) continue;
            int L = max(0, i - k - 1), D = max(0, j - k - 1);
            sum[i][j] =
                2 * (sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1]) -
                (sum[L][j] + sum[i][D] - sum[L][D]);
            while (sum[i][j] < 0) sum[i][j] += MOD;
            sum[i][j] %= MOD;
        }
    }
    ll ans = sum[h][w] - sum[h - 1][w] - sum[h][w - 1] + sum[h - 1][w - 1];
    while (ans < 0) ans += MOD;
    ans %= MOD;
    cout << ans << endl;
}

The desire of his soul is the prophecy of his fate
你灵魂的欲望,是你命运的先知。

原文地址:https://www.cnblogs.com/RioTian/p/13934504.html