Algoogle

Algorithm for Programming Contest

AOJ 2523 Time Complexity

Category: AOJ Tag: implementation

Time Complexity

問題概要


解法


全て正整数の和なので累乗の計算の時に条件を超えたらTLE
また累乗するときに超えなくても和が条件を超えたらTLE

コード


(2523.cpp) download
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
#include <iostream>
#include <string>
#define rep(i,a) for(int i = 0;i < (a); i++)
using namespace std;
typedef long long ll;

ll power(long long x, long long k) {
    if (k == 0)     return 1;
    if(x > 1000000000) return -1;
    if (k % 2 == 1){
        ll t = power(x, k-1);
        if(t < 0) return t;
        else return t * x;
    }
    else return power(x*x, k/2);
}

int main(){
    int n, T;
    string f;
    cin >> n >> T >> f;
    ll sum = 0;
    rep(i,f.size())if(f[i] == 'n'){
        ll p = power(n, f[i+2] - '0');
        if(p < 0 || p * T > 1000000000 || sum + p * T > 1000000000){
            cout << "TLE\n";
            return 0;
        }
        sum += p * T;
    }
    if(sum > 1000000000) cout << "TLE\n";
    else cout << sum << endl;
    return 0;
}

Comments