2020 CDUT寒假集训第一场(思维+dp专场)

2020 CDUT寒假集训第一场(思维+dp专场)

A - Vasya and Golden Ticket

  • 思路:暴力

  • AC代码


#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;

ll mult_mod(ll x, ll y, ll mod){
    return (x * y - (ll)(x / (long double)mod * y + 1e-3) * mod + mod) % mod;
}

ll pow_mod(ll a, ll b, ll p){
    ll res = 1;
    while (b){
        if (b & 1)
            res = mult_mod(res, a, p);
        a = mult_mod(a, a, p);
        b >>= 1;
    }
    return res % p;
}

ll gcd(ll a, ll b){
    return b ? gcd(b, a % b) : a;
}

int n, sum, div_, cnt, tmp;
bool flag;
string s;

int main(){
#ifndef ONLINE_JUDGE
    freopen("my_in.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> s;
    for (auto a: s)
        sum += a - '0';
    for (int i = 2; i <= n; i ++ ){
        cnt = 0, tmp = 0;
        div_ = sum / i;
        if (sum % i == 0){
            for (int j = 0; j < n; j ++ ){
                if (tmp < div_)
                    tmp += (s[j] - '0');
                if (tmp == div_){
                    tmp = 0;
                    cnt ++ ;
                }
                if (cnt == i && j == n - 1){
                    cout << "YES\n";
                    return 0;
                }
                if (tmp > div_)
                    break;
            }
        }
    }
    cout << "NO\n";
    return 0;
}

B - Slime

  • 思路:思维题 \(ans\)为排序后的\((a_n-a_1)+\sum_{i=2}^n abs(a_i)\)

  • AC代码


#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;

ll mult_mod(ll x, ll y, ll mod){
    return (x * y - (ll)(x / (long double)mod * y + 1e-3) * mod + mod) % mod;
}

ll pow_mod(ll a, ll b, ll p){
    ll res = 1;
    while (b){
        if (b & 1)
            res = mult_mod(res, a, p);
        a = mult_mod(a, a, p);
        b >>= 1;
    }
    return res % p;
}

ll gcd(ll a, ll b){
    return b ? gcd(b, a % b) : a;
}

const int N = 5e5 + 10;
const ll INF = 1e18;

int n;
ll ans;
ll a[N];

int main(){
#ifndef ONLINE_JUDGE
    freopen("my_in.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    if (n == 1){
        ll x;
        cin >> x;
        cout << x << "\n";
        return 0;
    }
    for (int i = 1; i <= n; i ++ )
        cin >> a[i];
    sort(a + 1, a + n + 1);
    ans = a[n] - a[1];
    for (int i = 2; i < n; i ++ )
        ans += abs(a[i]);
    cout << ans << "\n";
    return 0;
}

E - Multiplicity

  • 思路:枚举\(a_i\)的因数 可以将二维dp压成一维 \(dp_i = dp_i + dp_{i-1}\)

  • AC代码


#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;

ll mult_mod(ll x, ll y, ll mod){
    return (x * y - (ll)(x / (long double)mod * y + 1e-3) * mod + mod) % mod;
}

ll pow_mod(ll a, ll b, ll p){
    ll res = 1;
    while (b){
        if (b & 1)
            res = mult_mod(res, a, p);
        a = mult_mod(a, a, p);
        b >>= 1;
    }
    return res % p;
}

ll gcd(ll a, ll b){
    return b ? gcd(b, a % b) : a;
}

const int N = 1e5 + 10;
const int mod = 1e9 + 7;

int n, ans;
int a[N], dp[N];

int main(){
#ifndef ONLINE_JUDGE
    freopen("my_in.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    dp[0] = 1;
    cin >> n;
    for (int i = 1; i <= n; i ++ ){
        cin >> a[i];
        for (int j = sqrt(a[i]); j >= 1; j -- ){
            if (a[i] % j == 0){
                int div = a[i] / j;
                if (div <= n)
                    dp[div] = ((dp[div] + dp[div - 1]) % mod + mod) % mod;
                if (j <= n && j != div)
                    dp[j] = ((dp[j] + dp[j - 1]) % mod + mod) % mod;
            }
        }
        // for (int j = 1; j <= n; j ++ )
        //     cout << dp[j] << "\n";
    }
    for (int i = 1; i <= n; i ++ )
        ans = ((ans + dp[i]) % mod + mod) % mod;
    cout << ans << "\n";
    return 0;
}

G - Make Product Equal One

  • 思路:水题 没开ll wa1

  • AC代码


#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;

ll mult_mod(ll x, ll y, ll mod){
    return (x * y - (ll)(x / (long double)mod * y + 1e-3) * mod + mod) % mod;
}

ll pow_mod(ll a, ll b, ll p){
    ll res = 1;
    while (b){
        if (b & 1)
            res = mult_mod(res, a, p);
        a = mult_mod(a, a, p);
        b >>= 1;
    }
    return res % p;
}

ll gcd(ll a, ll b){
    return b ? gcd(b, a % b) : a;
}

ll n, a, ans, num_0, num_neg;

int main(){
#ifndef ONLINE_JUDGE
    freopen("my_in.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for (int i = 1; i <= n; i ++ ){
        cin >> a;
        if (a == 0)
            num_0 ++ ;
        else if (a > 0)
            ans += a - 1;
        else{
            ans += -1 - a;
            num_neg ++ ;
        }
    }
    if (num_neg % 2 && num_0 == 0)
        ans += 2;
    cout << ans + num_0 << "\n";
    return 0;
}

H - Restricted RPS

  • 思路:模拟

  • AC代码


#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;

ll mult_mod(ll x, ll y, ll mod){
    return (x * y - (ll)(x / (long double)mod * y + 1e-3) * mod + mod) % mod;
}

ll pow_mod(ll a, ll b, ll p){
    ll res = 1;
    while (b){
        if (b & 1)
            res = mult_mod(res, a, p);
        a = mult_mod(a, a, p);
        b >>= 1;
    }
    return res % p;
}

ll gcd(ll a, ll b){
    return b ? gcd(b, a % b) : a;
}

int t, n, a, b, c, cnt;
string s;

int main(){
#ifndef ONLINE_JUDGE
    freopen("my_in.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> t;
    while (t -- ){
        string ans;
        cnt = 0;
        cin >> n >> a >> b >> c >> s;
        for (int i = 0; i < n; i ++ ){
            ans += '#';
            if (s[i] == 'R'  && b){
                ans[i] = 'P';
                b -- ;
                cnt ++ ;
            }
            else if (s[i] == 'P' && c){
                ans[i] = 'S';
                c -- ;
                cnt ++ ;
            }
            else if (s[i] == 'S' && a){
                ans[i] = 'R';
                a -- ;
                cnt ++ ;
            }
        }
        if (cnt < (n + 1) / 2)
            cout << "NO\n";
        else{
            cout << "YES\n";
            for (int i = 0; i < n; i ++ ){
                if (ans[i] == '#'){
                    if (a){
                        ans[i] = 'R';
                        a -- ;
                    }
                    else if (b){
                        ans[i] = 'P';
                        b -- ;
                    }
                    else if (c){
                        ans[i] = 'S';
                        c -- ;
                    }
                }
            }
            cout << ans << "\n";
        }
    }
    return 0;
}
posted @ 2020-03-13 11:52  Misuchii  阅读(162)  评论(0编辑  收藏  举报