Codeforces Round #609 (Div. 2) ABC 题解

A. Equation

题意:找出两个合数a,b使得 a - b = n。

思路:水题,让a = 10n, b = 9n ,那么ab必定都是合数。

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };

int main()
{
    ll n = read();
    cout<<10*n<<' '<<9*n<<endl;
    return 0;
}

B. Modulo Equality

题意:找到一个最小的x,使得每个(a[i] + x)%m后能在b数组中找到一一对应的。

看到n才2000,尝试暴力一点的思路。从结果入手,a[1]在+x模m后肯定和b数组中的某个数是对应的,借此思路,我们就枚举一遍b数组,假设当前b[p]就是a[1]+x变过去的。这样我们就知道了和b[p]匹配时的x,然后拿这个x遍历一遍a数组看是否合法,合法的话就更新我们的min值。时间复杂度O(n²)

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <unordered_map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 3e3+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };

ll a[maxn];
ll b[maxn];
unordered_map<ll,ll> Map;
unordered_map<ll,ll> Map1;

int main()
{
    ll n = read(), m = read();
    rep(i,1,n) a[i] = read();
    rep(i,1,n) b[i] = read(), Map[b[i]] ++;
    ll mi = inf;
    int p = 1;
    while(p<=n)
    {
        Map1.clear();
        for(auto it = Map.begin(); it != Map.end(); it++) Map1[it->fi] = it->se;
        ll x = 0;
        if(a[1] > b[p]) x = m - (a[1] - b[p]);
        else x = b[p] - a[1];

        rep(i,1,n) Map1[(a[i]+x)%m] -- ;
        int flag = 1;
        for(auto it = Map1.begin(); it != Map1.end(); it++)
        {
            if(it->se!=0)
            {
                flag = 0;
                break;
            }
        }
        if(flag) mi = min(mi, x);
        p++;
    }
    cout<<mi<<endl;
    return 0;
}

C. Long Beautiful Integer

题意:给一个数x,找到一个y>=x且y[i] = y[i-k](i>=k)。

思路:贪心一下,其实我们需要研究的只是前k个数,要构造的数后面都是这k个的循环。
1.如果我们直接构造x前k个数的循环,如果此时的y还大于等于x话,就是最优的,什么都不用改,直接重复这k段。
2.否则,就让前k段+1,这个时候再重复肯定比x大,而且还能保证是最小的。
所以就按照上面判断条件走一下,如果要+1还要考虑一下进位。

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 2e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };


int main()
{
    string s;
    ll n = read(), k = read();
    cin>>s;
    string t;
    for(int i=0; i < k; i++) t += s[i];
    for(int i=k; i<n; i++) t += t[i-k];
    if(t >= s)
    {
        cout<<n<<'
';
        cout<<t<<'
';
    }
    else
    {
        int e = 1;
        per(i,k-1,0)
        {
            int cur = t[i] - '0';
            int tmp = cur;
            cur = (cur + e)%10;
            e = (tmp+e)/10;
            t[i] = cur + '0';
            if(!e) break;
        }
        cout<<n<<'
';
        rep(i,k,n-1) t[i] = t[i-k];
        cout<<t<<'
';
    }
    return 0;
}

原文地址:https://www.cnblogs.com/Bgwithcode/p/13547429.html