CodeForces #292 div.2 题解

本文精选了三道算法竞赛题目并提供了详细的解题思路及AC代码。包括路径判断问题、状态变化模拟问题以及数位操作问题,覆盖了基础的数据结构与算法知识。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A题:

题意:求出三个数字x,y,n,判断从(0,0)到(x,y)能否恰好通过n步

思路:判断两点间最短需要的步数min,满足n>=min&&(n&1==min&1)即可

AC代码:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <deque>
#include <list>
#include <cctype>
#include <algorithm>
#include <climits>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#define ll long long
#define ull unsigned long long
#define all(x) (x).begin(), (x).end()
#define clr(a, v) memset( a , v , sizeof(a) )
#define pb push_back
#define mp make_pair
#define read(f) freopen(f, "r", stdin)
#define write(f) freopen(f, "w", stdout)
using namespace std;

int main()
{
    ios::sync_with_stdio( false );
    ll x, y, n;
    while ( cin >> x >> y >> n ){
        int sum = abs(x) + abs(y);
        if ( sum <= n && ( (sum & 1 ) == ( n & 1 ) ) ){
            cout << "Yes" << endl;
        }
        else{
            cout << "No" << endl;
        }
    }
    return 0;
}


B题:

题意:有m个男朋友和n个女朋友,其中男朋友中有k个编号为num1...numk的人是高兴的状态,女朋友中也有k个编号为num1...numk人是高兴状态,第i天挑出男生中编号为i%m和女生中编号为i%n的一起吃饭,若其中至少有一个为高兴,那么两人都会变成高兴状态,求一段时间之后,所有的朋友都会变得高兴。

思路:用循环暴力跑,判断所有组合,并查即可。

AC代码:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <deque>
#include <list>
#include <cctype>
#include <algorithm>
#include <climits>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#define ll long long
#define ull unsigned long long
#define all(x) (x).begin(), (x).end()
#define clr(a, v) memset( a , v , sizeof(a) )
#define pb push_back
#define mp make_pair
#define read(f) freopen(f, "r", stdin)
#define write(f) freopen(f, "w", stdout)
using namespace std;
bool g[105];
bool b[105];
vector<int> j[105];
int main()
{
    ios::sync_with_stdio( false );
    int m, n, k;
    while ( cin >> m >> n ){
        int cnt = m + n;
        int  tmp;
        bool flag = 1;
        clr ( b, 0 );
        clr ( g, 0 );
        for ( int i = 0; i < 105; i ++ ){
            j[i].clear();
        }
        cin >> k;
        for ( int i = 0; i < k; i ++ ){
            cin >> tmp;
            b[tmp] = 1;
            cnt --;
        }
        cin >> k;
        for ( int i = 0; i < k; i ++ ){
            cin >> tmp;
            g[tmp] = 1;
            cnt --;
        }
        for ( int i = 0; cnt; i ++ ){
            int p = i % m;
            int q = i % n;
            if ( b[p] ){
                if ( g[q] ){
                    continue;
                }
                else{
                    cnt --;
                    g[q] = 1;
                }
            }
            else{
                if ( g[q] ){
                    b[p] = 1;
                    cnt --;
                }
                else{
                    for ( int l = 0; l < j[p].size(); l ++ ){
                        if ( j[p][l] == q ){
                            flag = 0;
                            cnt = 0;
                            break;
                        }
                    }
                    if ( flag ){
                        j[p].pb ( q );
                    }
                }
            }
        }
        if ( flag )
            cout << "Yes" << endl;
        else
            cout << "No" << endl;
    }
    return 0;
}


C题:

题意:给出一个数m,将这个数每一位的阶乘的乘积表示成另一个数的每一位阶乘的乘积,求出最大的可能。

思路:判断每一位,分解成尽可能多的位数,最后将所有结果升序排列。

AC代码:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <deque>
#include <list>
#include <cctype>
#include <algorithm>
#include <climits>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#define ll long long
#define ull unsigned long long
#define all(x) (x).begin(), (x).end()
#define clr(a, v) memset( a , v , sizeof(a) )
#define pb push_back
#define mp make_pair
#define read(f) freopen(f, "r", stdin)
#define write(f) freopen(f, "w", stdout)
using namespace std;

bool cmp ( const int &a, const int &b ){
    return a > b;
}
int main()
{
    ios::sync_with_stdio( false );

    ll tmp, k;
    while ( cin >> k >> tmp ){
        vector<int> ans;
        ans.clear();

        while ( tmp ){
            ll n = tmp % 10;
            tmp /= 10;
            while ( n ){
                if ( n == 1 ){break;}
                else if ( n == 2 ){ans.pb(2);break;}
                else if ( n == 3 ){ans.pb(3);break;}
                else if ( n == 4 ){ans.pb(3);ans.pb(2);ans.pb(2);break;}
                else if ( n == 5 ){ans.pb(5);break;}
                else if ( n == 6 ){ans.pb(5);ans.pb(3);break;}
                else if ( n == 7 ){ans.pb(7);break;}
                else if ( n == 9 ){ans.pb(7);ans.pb(3);ans.pb(3);ans.pb(2);break;}
                else if ( n == 8 ){ans.pb(7);ans.pb(2);ans.pb(2);ans.pb(2);break;}
            }
        }
        sort ( all(ans), cmp );
        for ( int i = 0; i < ans.size(); i ++ ){
            cout << ans[i];
        }
        cout << endl;
    }
    return 0;
}



(未完待续)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值