2020CCPC网络选拔赛题解

Before

题目链接

榜单

在这里插入图片描述
排名和队号一样

1002 Graph Theory Class

题目大意

定义一个完全无向图 G = ( V , E ) G=(V,E) G=(V,E)的边权 W i , j = l c m ( i + 1 , j + 1 ) W_{i,j}=lcm(i+1,j+1) Wi,j=lcm(i+1,j+1),问你当 ∣ V ∣ = n |V|=n V=n该图的最小生成树权值模上 k k k为多少

数据范围

1 ≤ n ≤ 1 0 10 1 \leq n \leq 10^{10} 1n1010, 1 0 8 ≤ k ≤ 1 0 9 10^{8} \leq k \leq 10^{9} 108k109

解题思路

首先考虑如何在该图中找到一个最小生成树,经过模拟,我们很快可以发现只需将所有 i + 1 i+1 i+1为质数的节点挑出来作为一颗子树的根节点,然后将所有此类节点(除 2 2 2以外)与 2 2 2相连,则可以满足题目要求(证明略,可以想象如果更换一条边必然会产生更大的权值),此时该树的权值为:
W = ∑ i = 3 n + 1 i + ∑ i = 3 , i   i s   p r i m e n + 1 i W=\sum_{i=3}^{n+1}i+\sum_{i=3, i\ is\ prime}^{n+1}i W=i=3n+1i+i=3,i is primen+1i
于是我们只需要求出 n + 1 n+1 n+1以内的质数和即可,由于 n n n很大,所以 O ( n ) O(n) O(n)以上的质数筛法显然超时,这是我们需要用到一个叫做 M i n 25 Min25 Min25筛的东西,用于求出任意正整数以内的质数和

另外要注意前一项用等差数列求和时要先判断奇偶,否则会导致溢出

AC代码

#include 
using namespace std;
typedef long long ll;
const int N = 1000010;
struct Min25 {

    ll prime[N], id1[N], id2[N], flag[N], ncnt, m;

    ll g[N], sum[N], a[N], T, n;

    inline int ID(ll x) {
        return x <= T ? id1[x] : id2[n / x];
    }

    inline ll calc(ll x) {
        return x * (x + 1) / 2 - 1;
    }

    inline ll f(ll x) {
        return x;
    }
    inline void Init() {
        memset(prime, 0, sizeof(prime));
        memset(id1, 0, sizeof(id1));
        memset(id2, 0, sizeof(id2));
        memset(flag, 0, sizeof(flag));
        memset(g, 0, sizeof(g));
        memset(sum, 0, sizeof(sum));
        memset(a, 0, sizeof(a));
        ncnt = m = T = n = 0;
    }
    inline void init() {
        T = sqrt(n + 0.5);
        for (int i = 2; i <= T; i++) {
            if (!flag[i]) prime[++ncnt] = i, sum[ncnt] = sum[ncnt - 1] + i;
            for (int j = 1; j <= ncnt && i * prime[j] <= T; j++) {
                flag[i * prime[j]] = 1;
                if (i % prime[j] == 0) break;
            }
        }
        for (ll l = 1; l <= n; l = n / (n / l) + 1) {
            a[++m] = n / l;
            if (a[m] <= T) id1[a[m]] = m; else id2[n / a[m]] = m;
            g[m] = calc(a[m]);
        }
        for (int i = 1; i <= ncnt; i++)
            for (int j = 1; j <= m && (ll)prime[i] * prime[i] <= a[j]; j++)
                g[j] = g[j] - (ll)prime[i] * (g[ID(a[j] / prime[i])] - sum[i - 1]);
    }

    inline ll solve(ll x) {
        if (x <= 1) return x;
        return n = x, init(), g[ID(n)];
    }

}a;
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    int T;
    cin >> T;
    while (T--) {
        ll n, k;
        cin >> n >> k;
        a.Init();
        ll sum = 0;
        if (n >= 3) {
            if ((n + 4) & 1) sum = (n - 1) / 2 % k * ((n + 4) % k) % k;
            else sum = (n + 4) / 2 % k * ((n - 1) % k) % k;
        }
        sum = (sum + a.solve(n + 1) % k) % k;
        cout << (sum + k - 2) % k << '\n';
    }
    return 0;
}

1003 Express Mail Taking

题目大意

一共有 n n n个柜子,有 m m m件快递分别放在不同的柜子中,每个柜子之间的距离为 1 1 1,你开始时处于 1 1 1号柜子处,每次拿快递时,你需要首先到 k k k号柜子处输入指令,然后去往与输入指令对应的柜子处取快递,取完最后一件快递后你需要回到 1 1 1号柜子处,问你取完所有快递的最短路径是多少

数据范围

1 ≤ k ≤ n ≤ 1 0 9 1\leq k \leq n \leq 10^{9} 1kn109
1 ≤ m < m i n ( n , 1 0 9 ) 1 \leq m < min(n, 10^{9}) 1m<min(n,109)
∑ m ≤ 2 × 1 0 6 \sum m \leq 2 \times 10^{6} m2×106

解题思路

a i a_{i} ai为第 i i i件快递的所在柜子编号, a j a_{j} aj表示最后一次取出的快递柜子编号,则有:
D = ( k − 1 ) + 2 × ∑ i = 1 , i ≠ j m ∣ a i − k ∣ + ∣ a j − k ∣ + a j − 1 D = (k - 1) + 2 \times \sum_{i=1,i\neq j}^{m}|a_{i}-k|+|a_{j}-k|+a_{j}-1 D=(k1)+2×i=1,i=jmaik+ajk+aj1
那么我们只需要枚举 a j a_{j} aj的值即可,时间复杂度 O ( n ) O(n) O(n)

AC代码

#include 
#include 
#include 

using namespace std;
typedef long long ll;

const int N = 1e6 + 5;

ll a[N];

int main()
{
    int T;
    scanf("%d", &T);
    while (T--)
    {
        ll n, m, k;
        ll sum = 0;
        scanf("%lld %lld %lld", &n, &m, &k);
        for (int i = 1; i <= m; ++i)
        {
            scanf("%lld", &a[i]);
            ll x =  a[i] - k;
            if (x < 0) x = -x;
            sum += x;
        }
        ll ans = LLONG_MAX;
        for (int i = 1; i <= m; ++i)
        {
            ll x = a[i] - k;
            if (x < 0) x = -x;
            ans = min(ans, k - 1 + 2 * sum + a[i] - 1 - x);
        }
        printf("%lld\n", ans);
    }
    return 0;
}

1007 CCPC Training Class

题目大意

花里胡哨,就不解释了

解题思路

观察样例发现答案为字符串中出现次数最多的字符的个数

AC代码

#include 
using namespace std;
const int maxn = 1e5 + 10;
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    int T, cnt = 0;
    cin >> T;
    while (T--) {
        string s;
        cin >> s;
        int a[26] = { 0 };
        int len = s.length(), Max = 0;
        for (int i = 0; i < len; ++i) {
            ++a[s[i] - 'a'];
            Max = max(Max, a[s[i] - 'a']);
        }
        cout << "Case " << "#" << ++cnt << ": " << Max << '\n';
    }
    return 0;
}

1010 Reports

题目大意

给定一个数组,问你是否有相邻的两个 0 0 0或相邻的两个 1 1 1

解题思路

签到题,直接模拟即可

AC代码

#include 
using namespace std;
int a[66];
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    int T;
    cin >> T;
    while (T--) {
        int n;
        cin >> n;
        for (int i = 1; i <= n; ++i) cin >> a[i];
        bool tag = false;;
        for (int i = 2; i <= n; ++i) {
            if (a[i] == a[i - 1]) {
                tag = true;
                break;
            }
        }
        if (tag) cout << "NO" << '\n';
        else cout << "YES" << '\n';
    }
    return 0;
}

1011 3x3 Convolution

题目大意

有一个 n × n n \times n n×n的矩阵 A A A和一个 3 × 3 3 \times 3 3×3的矩阵 K K K,现做如下定义

  • C ( A , K ) C(A,K) C(A,K) n × n n\times n n×n的矩阵,其中 C x , y = ∑ i = 1 m i n ( n − x + 1 , 3 ) ∑ j = 1 m i n ( n − y + 1 , 3 ) A x + i − 1 , y + j − 1 ⋅ K i , j C_{x,y}=\sum_{i=1}^{min(n-x+1,3)}\sum_{j=1}^{min(n-y+1,3)}A_{x+i-1,y+j-1} \cdot K_{i,j} Cx,y=i=1min(nx+1,3)j=1min(ny+1,3)Ax+i1,y+j1Ki,j
  • C m ( A , K ) = C ( C m − 1 ( A , K ) , K ) C^{m}(A, K)=C(C^{m-1}(A,K),K) Cm(A,K)=C(Cm1(A,K),K)
  • C 1 ( A , K ) = C ( A , K ) C^{1}(A,K)=C(A,K) C1(A,K)=C(A,K)
  • K i , j = K i , j ′ / ( ∑ x = 1 3 ∑ y = 1 3 K x , y ′ ) K_{i,j}=K'_{i,j}/(\sum_{x=1}^{3}\sum_{y=1}^{3}K'_{x,y}) Ki,j=Ki,j/(x=13y=13Kx,y)

现在给出矩阵 A A A和矩阵 K ′ K' K,问你 l i m t → ∞ C t ( A , K ) lim_{t \to \infty}C^{t}(A,K) limtCt(A,K)是多少

数据范围

3 ≤ n ≤ 50 3 \leq n \leq 50 3n50
0 ≤ A i , j ≤ 1000 0 \leq A_{i,j} \leq 1000 0Ai,j1000
0 ≤ K i , j ′ ≤ 1000 0 \leq K'_{i,j} \leq 1000 0Ki,j1000
∑ x = 1 3 ∑ y = 1 3 K x , y ′ > 0 \sum_{x=1}^{3}\sum_{y=1}^{3}K'_{x,y} > 0 x=13y=13Kx,y>0

解题思路

根据定义可知 K i , j ≤ 1 K_{i,j} \leq 1 Ki,j1,那么当 K K K矩阵中存在两个及以上不为 0 0 0的数时答案为 0 0 0
当有且仅有一个不为 0 0 0的元素时,考虑当 K 1 , 1 ≠ 0 K_{1,1}\neq 0 K1,1=0,那么显然答案为原 A A A矩阵,否则模拟发现其答案依然为 0 0 0

这题需要注意行末空格问题,不然会 P E PE PE

AC代码

#include 
#include 
#include 

using namespace std;
typedef long long ll;

int a[55][55];
int k1[3][3];
int main()
{
    int T;
    scanf("%d", &T);
    while (T--) 
    {
        int n;
        scanf("%d", &n);
        for (int i = 1; i <= n; ++i)
            for (int j = 1; j <= n; ++j)
                scanf("%d", &a[i][j]);
        int sum = 0;
        for(int i= 1; i <= 3; ++ i)
            for (int j = 1; j <= 3; ++j)
            {
                scanf("%d", &k1[i][j]);
                sum += k1[i][j];
            }
        if (sum == k1[1][1])
        {
            for (int i = 1; i <= n; ++i)
            {
                for (int j = 1; j <= n; ++j)
                    printf("%d%c",a[i][j], j == n ? '\n':' ');
            }
        }
        else
        {
            for (int i = 1; i <= n; ++i)
            {
                for (int j = 1; j <= n; ++j)
                    printf("0%c", j == n ? '\n' : ' ');
            }
        }
    }
    return 0;
}

你可能感兴趣的:(CCPC)