19.5.15 Educational CF Round 65 (Div. 2) (2 / 7)

A. From Hero to Zero

  • 用乘法代替循环以防止超时
  • ac代码
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    /*************************************************************************
    > File Name: a.cpp
    > Author: Wqr_
    > Mail: xueduanwei@126.com
    > Created Time: 2019年06月05日 星期三 22时22分36秒
    ************************************************************************/

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<vector>
    #include<algorithm>
    #include<map>
    #include<queue>
    #include<cmath>
    #define iofuck std::ios::sync_with_stdio(false)
    using namespace std;
    typedef long long ll;
    ll n, k;
    int main(){
    iofuck;
    int t;
    cin >> t;
    while(t--){
    cin >> n >> k;
    ll ans = 0;
    for(;n > 0;){
    if(n % k == 0){
    n /= k;
    ans++;
    }else{
    ans += n % k;
    n -= (n % k);
    }
    }
    cout << ans << endl;
    }
    return 0;
    }

B. Catch Overflow!

  • 用两个栈保存for和add
  • 具体见代码
  • ac代码
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    /*************************************************************************
    > File Name: b.cpp
    > Author: Wqr_
    > Mail: xueduanwei@126.com
    > Created Time: 2019年06月05日 星期三 22时49分10秒
    ************************************************************************/

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<vector>
    #include<algorithm>
    #include<map>
    #include<stack>
    #include<queue>
    #include<cmath>
    #define iofuck std::ios::sync_with_stdio(false)
    using namespace std;
    typedef unsigned long long ll;
    ll l;
    ll bian = 4294967295;
    ll ans = 0;
    ll tmp = 0;
    ll fornum = 0;
    stack<ll> loop;
    stack<ll> addd;
    int main(){
    iofuck;
    cin >> l;
    string in;
    addd.push(0);
    while(l--){
    cin >> in;
    if(in[0] == 'a'){
    addd.top()++;
    }else if(in[0] == 'f'){
    addd.push(0);
    ll loopnum;
    cin >> loopnum;
    loop.push(loopnum);
    fornum++;
    }else if(in[0] == 'e'){
    fornum--;
    ll tmpp = addd.top() * loop.top();
    if(tmpp / loop.top() != addd.top() || tmpp > bian){
    cout << "OVERFLOW!!!" << endl;
    return 0;
    }
    loop.pop();
    addd.pop();
    addd.top() += tmpp;
    }
    }
    if(!addd.empty()){
    ans += addd.top();
    }
    if(ans <= bian)
    cout << ans << endl;
    else {
    cout << "OVERFLOW!!!" << endl;
    return 0;
    }
    return 0;
    }