Thứ Hai, 20 tháng 3, 2023

Code c++ Bài Tổng các chữ số trong đề thi OLP Miền Trung Tây Nguyên bảng Không chuyên vòng chung kết năm 2023

#include <bits/stdc++.h>
using namespace std;


typedef long long ll;
typedef double db;
const int K = 1e6 + 5;
ll n, k, p[20];
ll dp[13][2][K], f[13][2][K];
int digit[13];

inline int sumdigit(ll x){
    int res = 0;
    while(x) res += x % 10, x /= 10;
    return res;
}

inline int len(ll x){
    int res = 0;
    while(x) ++res, x /= 10;
    return res;
}

inline int get(ll x, int row){
    return (x / p[row]) % 10;
}

void readip(){
    cin >> n >> k;
}

ll F(int i, bool t, int m){
    if (i < 0) return m == 0;
    if (f[i][t][m] != -1 && !t) return f[i][t][m];
    ll &res = f[i][t][m];
    res = 0;

    int lim = t ? digit[i] : 9;

    for(int d=0;d<=lim;d++)
        res += F(i - 1, t & (d == lim), (1LL * m * 10 + d) % k);
    return res;
}

ll Dp(int i, bool t, int m){
    if (i < 0) return 0;
    if (dp[i][t][m] != -1 && !t) return dp[i][t][m];
    ll &res = dp[i][t][m];
    res = 0;

    int lim = t ? digit[i] : 9;

    for(int d=0;d<=lim;d++){
        ll tmp = F(i - 1, t & (d == lim), (1LL * m * 10 + d) % k);
        res += 1LL * d * tmp;
        res += Dp(i - 1, t & (d == lim) , (1LL * m * 10 + d) % k);
    }
    return res;
}

void solve(){
    if (k > 1e5){
        ll res = 0;
        for (int i = 1; 1LL * i * k <= n; ++i)
            res += sumdigit(1LL * i * k);
        cout << res << endl;
        return;
    }
    memset(dp, -1, sizeof dp);
    memset(f, -1, sizeof f);

    for(int i=0;i<=12;i++) digit[i] = get(n,i);
    cout << Dp(len(n) - 1, 1, 0) << endl;
}

int main(){
 //   freopen("b.inp","r",stdin);
 //   freopen("b.out","w",stdout);
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    p[0] = 1;

    for(int i=1;i<=13;i++)
        p[i] = 10LL * p[i-1];
    int t = 2;

    while (t--){
        readip();
        solve();
    }
}

Code c++ Bài Vòng tròn số trong Đề thi OLP Miền Trung Tây Nguyên bảng Không chuyên vòng chung kết năm 2023

#include <bits/stdc++.h>
using namespace std;

#define FOR(i, n) for (int (i) = 0; (i) < (n); ++(i))
#define REP(i,a,b) for(int (i)=(a);(i)<=(b);++i)


const int INF = 1e9 , mod = 1e9 + 7;

template <class T>
inline bool maximize(T &x, const T &y){ if (x < y){x = y; return 1;} return 0; }

typedef long long ll;
typedef double db;
const int N = 2e3 + 5;

ll dp[N][N][3], a[N];
int n , k;
void readip(){
    cin >> n >> k;
    FOR(i, n) cin >> a[i];
    memset(dp, -0x3f, sizeof dp);
}

void solve(){
    FOR(i, 3) dp[0][0][i] = 0;
    REP(i, 1, n){
        FOR(j, i) FOR(t, 3){
            maximize(dp[i][j][t], dp[i-1][j][t]);
            ll val = dp[i-1][j][t];
            if ((t+1) % 3 == 1) val -= a[i - 1];
            else val += a[i - 1];
            maximize(dp[i][j+1][(t+1)%3], val);
        }
    }
    ll res = -1e18;
    FOR(i, 3) maximize(res, dp[n][3*k][i]);
    cout << res;
}

int main(){

        ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
        freopen("b.inp","r",stdin);
        freopen("b.out","w",stdout);

        readip();
        solve();

}

Code c++ bài Phần thưởng trong Đề thi OLP Miền Trung Tây Nguyên bảng Không chuyên vòng chung kết năm 2023

#include <bits/stdc++.h>
 
#define ll long long
#define fi first
#define sc second
 
using namespace std;

 void io(){
     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
 }

int main(){
    io();
    ll n,s;
    cin >> n>> s;
    ll sum =0;
    ll totalw=0;
    vector<pair<ll,ll>> ct,cs;
    for (int i=0;i<n;i++){
        ll w,v,c;

        cin >> w >> v >> c;

        if (c==0) cs.push_back({w,v}); else {
            ct.push_back({w,v});
            sum+=v;
            totalw+=w;
        }
    }
    sort(ct.begin(),ct.end());
    sort(cs.begin(),cs.end());
    ll d1=ct.size();
    ll d2=cs.size();
    ll j=0;
    if (d2==0) {
        cout << sum ;
        return 0;
    }
    ll ans = sum;
    ll f[d2]{};
    f[0]=cs[0].sc;
    for (int i=1;i<d2;i++){
        f[i]=max(f[i-1],cs[i].sc);
    }
    if (d1==0) {
        cout << f[n-1];
        return 0;
    }
    // for (auto i : cs) cout << i.fi << ' '<< i.sc << '\n';
    for (int i=0;i<d1;i++){
        if (cs[j].fi+totalw-ct[i].fi<=s&&j<d2){
        while (cs[j].fi+totalw-ct[i].fi<=s&&j<d2){
            ans = max(ans,sum-ct[i].sc+f[j]);
            j++;
        }
        j--;
    }
        if (j==d2) j--;
    }
    cout << ans;
}

Code c++ bài Bảng số trong đề thi OLP Miền Trung Tây Nguyên bảng Không chuyên vòng chung kết năm 2023

#include<bits/stdc++.h>
using namespace std;
long long a[4][4],x;
void nhap(){
    for(int i = 1; i <= 3; i++){
        for(int j = 1; j <= 3; j++){
            cin>>a[i][j];
        }
    }
}
void giai(){
    x = a[1][1];
    long long s = 0;
    long long T = a[1][1] + a[1][2] + a[1][3];
    for(int i = 2; i <= 3; i++){
        s = 0;
        for(int j = 1; j <= 3; j++){
            s += a[i][j];
        }
        if(s != T){
            cout<<"NO";
            exit(0);
        }
    }

    for(int j = 1; j <= 3; j++){
        s = 0;
        for(int i = 1; i <= 3; i++){
            s += a[i][j];
        }
        if(s != T){
            cout<<"NO";
            exit(0);
        }
    }

    for(int i = 1; i <= 3; i++){
        for(int j = 1; j <= 3; j++){
            if(a[i][j] != x){
                cout<<"YES";
                exit(0);
            }
        }
    }
    cout<<"NO";
}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    nhap();
    giai();
    return 0;
}

Đề thi học sinh giỏi lớp 9 môn tin học thành phố Hồ Chí Minh năm học 2022 - 2023

Mời các bạn tham khảo
Đề thi học sinh giỏi lớp 9 môn tin học thành phố Hồ Chí Minh năm học 2022 - 2023


VTS c++

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, n) for (int(i) = 0;
  (i) < (n); ++(i))
  #define REP(i, a, b) for (int(i) = (a);
    (i) <= (b); ++i)

    const int INF = 1e9,
      mod = 1e9 + 7;

template < class T >
  inline bool maximize(T & x,
    const T & y) {
    if (x < y) {
      x = y;
      return 1;
    }
    return 0;
  }

typedef long long ll;
typedef double db;
const int N = 2e3 + 5;

ll dp[N][N][3], a[N];
int n, k;
void readip() {
  cin >> n >> k;
  FOR(i, n) cin >> a[i];
  memset(dp, -0x3f, sizeof dp);
}

void solve() {
  FOR(i, 3) dp[0][0][i] = 0;
  REP(i, 1, n) {
    FOR(j, i) FOR(t, 3) {
      maximize(dp[i][j][t], dp[i - 1][j][t]);
      ll val = dp[i - 1][j][t];
      if ((t + 1) % 3 == 1) val -= a[i - 1];
      else val += a[i - 1];
      maximize(dp[i][j + 1][(t + 1) % 3], val);
    }
  }
  ll res = -1e18;
  FOR(i, 3) maximize(res, dp[n][3 * k][i]);
  cout << res;
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  freopen("b.inp", "r", stdin);
  freopen("b.out", "w", stdout);

  readip();
  solve();

}