ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

【Day1】一名菜鸟ACMer的暑假训练

2022-07-02 14:03:13  阅读:155  来源: 互联网

标签:... cout int 菜鸟 构造 Day1 ACMer solve include


Day1

板刷CF构造题

A. Nastia and Nearly Good Numbers

https://codeforces.com/problemset/problem/1521/A
构造\(z=abt_3,y=at_2,x=at_1\),根据等式\(z=x+y\),可以约去a
故可以构造 1 3b-1 3b (系数)
注意特判b==1,全部都是good,无法构造

#include<bits/stdc++.h>

using namespace std;

void solve () {
    long long a, b;
    cin >> a >> b;
    if (b == 1) {
        cout << "NO\n";
        return ;
    }
    cout << "YES\n";
    cout << a << ' ' << a * (3*b-1) << ' ' << a*3*b << endl;
    
    
}

int main () {
    int t;
    cin >> t;
    while (t --) {
        solve ();
    }
}

//x+y=z
//一个%ab==0,另两个%a==0
//t1+t2==bt3
//1 b-1 b

A. K-divisible Sum

https://codeforces.com/problemset/problem/1476/A
懂抽屉原理的宝宝( ̄︶ ̄*))就可以秒解啦
小心格式的输出

#include <bits/stdc++.h>

using namespace std;

void solve () {
    int n, k;
    cin >> n >> k;
    if (n == 1)         cout << k << endl;
    else if (n == k)    cout << 1 << endl;
    else if (n < k)     cout << (int)ceil (1.0*k/n) << endl;
    else {
        double dx = n - k;
        //cout << dx << ' ';
        k = ceil(dx/k)*k+k;
        //cout << k << ' ';
        cout << (int)ceil (1.0*k/n) << endl;
    }
}

int main () {
    int t;
    cin >> t;
    while (t --) {
        solve();
    }
}
//抽屉原理
//啊啊啊气死我了!!输出要注意格式int

C. Not Adjacent Matrix

https://codeforces.com/problemset/problem/1520/C
灵光乍现,根据对角线来填充,这样子就不会有相邻的在上下左右了。
注意怎么构造:找规律+普适性规律

#include <bits/stdc++.h>

using namespace std;
const int N = 105;
int a[N][N];

void print (int n) {
    for (int i = 1; i <= n; i ++) {
        for (int j = 1; j <= n; j ++)
            cout << a[i][j] << ' ';
        cout << endl;
    }
}

void solve () {
    int n;
    cin >> n;
    if (n == 1)     cout << "1\n";
    else if (n == 2)    cout << "-1\n";
    else {
        a[1][1] = 1;
        //处理第一行, 增量为 1*n, 2*(n-i+2)...
        a[1][2] = a[1][1] + n;
        for (int i = 3; i <= n; i ++)   a[1][i] = a[1][i-1] + 2*(n-i+2);
        //处理第一列 增量为 ...9,7,5,3 通项:2*(n-i)+3
        for (int i = 2; i <= n; i ++)   a[i][1] = a[i-1][1] + 2*(n-i)+3;
        //然后就可以根据对角线求啦
        for (int i = 2; i <= n; i ++)
            for (int j = 2; j <= n; j ++)
                a[i][j] = a[i-1][j-1] + 1;

        print (n);
    }
}

int main () {
    int t;
    cin >> t;
    while (t --) {
        solve ();
    }
}
//上下左右不得相邻
//按照对角线填
//从主对角线开始填,然后一上一下挨个填

B. Same Parity Summands

https://codeforces.com/problemset/problem/1352/B
奇偶性相同的k个数和为n
构造:
1 1 1 1...n-k+1
2 2 2 2...n-2*(k-1)
//构造没有问题,但是要记得判断末项是否大于0

#include <bits/stdc++.h>

using namespace std;

void solve () {
    int n, k;
    cin >> n >> k;
    if (n % k == 0) {
        cout << "YES" << endl;
        for (int i = 0; i < k; i ++)
            cout << n / k << ' ';
        cout << endl;
    }
    else {
        int res1 = n-k+1, res2 = n-2*(k-1);
        if (res1 > 0 && res1 & 1) {
            cout << "YES\n";
            for (int i = 0; i < k - 1; i ++)   cout << "1 ";
            cout << n-k+1 << endl;
            return ;
        }
        if (res2 > 0 && res2 % 2 == 0) {
            cout << "YES\n";
            for (int i = 0; i < k - 1; i ++)    cout << "2 ";
            cout << n - 2 * (k-1) << endl;
            return ;
        }
        cout << "NO\n";
    }

}

int main () {
    int t;
    cin >> t;
    while (t --) {
        solve ();
    }
}

//奇偶性相同的k个数和为n
//构造:
// 1 1 1 1...n-k+1
// 2 2 2 2...n-2*(k-1)
//构造没有问题,但是要记得判断末项是否大于0

标签:...,cout,int,菜鸟,构造,Day1,ACMer,solve,include
来源: https://www.cnblogs.com/CTing/p/16437455.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有