結果

問題 No.1022 Power Equation
ユーザー wkwk
提出日時 2020-04-15 21:55:11
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 32 ms / 2,000 ms
コード長 4,366 bytes
コンパイル時間 1,632 ms
コンパイル使用メモリ 165,788 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-09 19:01:02
合計ジャッジ時間 2,219 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 9 ms
6,940 KB
testcase_02 AC 13 ms
6,940 KB
testcase_03 AC 19 ms
6,944 KB
testcase_04 AC 31 ms
6,940 KB
testcase_05 AC 31 ms
6,944 KB
testcase_06 AC 31 ms
6,940 KB
testcase_07 AC 32 ms
6,940 KB
testcase_08 AC 29 ms
6,940 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'long int getx(long int, long int, long int, long int)':
main.cpp:168:1: warning: control reaches end of non-void function [-Wreturn-type]
  168 | }
      | ^

ソースコード

diff #

#include <bits/stdc++.h>
#define REP(i, n) for(int i = 0; (i) < (n); (i)++)
#define INF 5000000000000000
using namespace std;

struct Edge{
    int to;
    long weight;
    Edge(int t, long w) : to(t), weight(w) {}
};

long modpow(long a, long n, long mod) {
    long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

// a^{-1} mod を計算する
long modinv(long a, long mod) {
    return modpow(a, mod - 2, mod);
}

struct compare1 { 
    bool operator()(const pair<long, long>& value,  
                                const long& key) 
    { 
        return (value.first < key); 
    } 
    bool operator()(const long& key,  
                    const pair<long, long>& value) 
    { 
        return (key < value.first); 
    } 
};

struct UnionFind {
    vector<int> par;
    vector<int> rank;

    UnionFind(int n = 1){
        init(n);
    }

    void init(int n = 1){
        par.resize(n);
        rank.resize(n);
        REP(i, n) par[i] = i, rank[i] = 0; 
    }

    int root(int x){
        if(par[x] == x) return x;
        else return par[x] = root(par[x]);
    }

    bool issame(int x, int y){
        return root(x) == root(y);
    }

    bool merge(int x, int y){
        x = root(x); y = root(y);
        if(x == y) return false;
        if(rank[x] < rank[y]) swap(x, y);
        if(rank[x] == rank[y]) rank[x]++;
        par[y] = x;
        return true;
    }
};

template<class Abel> struct weightedUnionFind{
    vector<int> par;
    vector<int> rank;
    vector<Abel> diff_weight;

    weightedUnionFind(int n = 1, Abel SUM_UNITY = 0){
        init(n, SUM_UNITY);
    }

    void init(int n = 1, Abel SUM_UNITY = 0){
        par.resize(n); rank.resize(n); diff_weight.resize(n);
        REP(i, n) par[i] = i, rank[i] = 0, diff_weight[i] = SUM_UNITY; 
    }

    int root(int x){
        if(par[x] == x) return x;
        else{
            int r = root(par[x]);
            diff_weight[x] += diff_weight[par[x]];
            return par[x] = r;
        }
    }

    Abel weight(int x){
        root(x);
        return diff_weight[x];
    }

    bool issame(int x, int y){
        return root(x) == root(y);
    }

    bool merge(int x, int y, Abel w){
        w += weight(x); w -= weight(y);
        x = root(x); y = root(y);
        if(x == y) return false;
        if(rank[x] < rank[y]) swap(x, y), w = -w;
        if(rank[x] == rank[y]) rank[x]++;
        par[y] = x;
        diff_weight[y] = w;
        return true;
    }

    Abel diff(int x, int y){
        return weight(y) - weight(x);
    }
};

using Graph = vector<vector<int>>;
using P = pair<int, int>;

/*
void dijkstra(int s, int V, Graph &G, long* d){
    priority_queue<P, vector<P>, greater<P>> pque;
    fill(d, d + V, INF);
    d[s] = 0;
    pque.push(P(0, s));

    while(!pque.empty()){
        P p = pque.top(); pque.pop();
        int now = p.second;
        if(d[now] < p.first) continue;
        REP(i, G[now].size()){
            Edge e = G[now][i];
            if(d[e.to] > d[now] + e.weight){
                d[e.to] = d[now] + e.weight;
                pque.push(P(d[e.to], e.to));
            }
        }
    }
}
*/

long GCD(long a, long b){
    if(b == 0l) return a; 
    if(a < b) return GCD(b, a);
    else return GCD(b, a%b);
}

long getx(long x, long N, long left, long right){
    //cout << left << " " << right << endl;
    long center = (right + left) / 2l;
    long center2 = center + 1l;
    long temp = 1l, temp2 = 1l;
    bool flag = true, flag2 = true;
    for(long i=0;i<x;i++){
        if(flag) temp *= center;
        if(flag2) temp2 *= center2;
        if(temp > N) flag = false;
        if(temp2 > N) flag2 = false;
    }
    if(flag && !flag2) return center;
    if(!flag && !flag2 && center == 2) return 0l;
    if(flag2) return getx(x, N, center, right);
    if(!flag && !flag2) return getx(x, N, left, center);
}

int main()
{
    int T; cin >> T;
    REP(i, T){
        long N; cin >> N;
        long ans = 0;
        ans += N * N; 
        for(long b = 1l; b <= 30l; b++) for(long d = 1l; d <= 30l; d++){
            if(GCD(b, d) == 1l) 
            {
                long temp = getx(max(b, d), N, 2l, N+1l);
                if(temp != 0l) ans += (temp - 1l) * (N/max(b, d));
            }
        }
        cout << ans << endl;
    }
    return 0;
}

0