QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#850940#9962. Diminishing FractionsZawosWA 95ms216856kbC++233.2kb2025-01-10 13:22:522025-01-10 13:22:53

Judging History

你现在查看的是最新测评结果

  • [2025-01-10 13:22:53]
  • 评测
  • 测评结果:WA
  • 用时:95ms
  • 内存:216856kb
  • [2025-01-10 13:22:52]
  • 提交

answer


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define FOR(i,a,b) for (int i = (a); i < (b); i++)
using namespace std;
using namespace __gnu_pbds;
using ll=long long;
using ld=long double;
using vi=vector<int>;

template<class T> using oset =tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update> ;
//上
const int N = 50001;
int wh[N];
ll F[5217][5217];
int euclid(int a, int b, ll &x, ll &y) {
	if (!b) return x = 1, y = 0, a;
	int d = euclid(b, a % b, y, x);
	return y -= a/b * x, d;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    memset(wh,-1,sizeof(wh));
    FOR(i,0,5217) FOR(j,0,5217) F[i][j] = 1;
    vector<int> prime(N+1,1);
    for(int i = 2; i <= N; i++){
        if(prime[i] == 1) for(int j = i; j <=N;j+=i) prime[j] = i;
    }

    vector<pair<int,int>> pot;

    for(int i = 2; i <= N;i++){
        unordered_map<int,int> fac;
        int x = i;
        int r = 1;
        while( x > 1){
            fac[prime[x]]++;
            r *= prime[x];
            x /= prime[x];
            
            if(fac.size() > 1) break;
        }
        if(fac.size() == 1){
            for(auto &u: fac) pot.push_back({u.first,r});
        }
    }
    for(int i = 0; i < 5217;i++) wh[pot[i].second] = i;
    for(int i = 0; i <5217;i++){
        for(int j = 0; j < 5217;j++){
            if(pot[i].first == pot[j].first){
                if(j >0) F[i][j] = F[i][j-1];
            }else{
                if(j >0){
                    F[i][j] = F[i][j-1]*pot[j].first;
                }else{
                    F[i][j] *= pot[j].first;
                }
            } 
        }
    }
    ll y;

    int t;
    cin >>t;
    unordered_map<int,int> mp;
    vector<pair<int,int>> q;
    for(int tt = 0; tt < t; tt++){
        int n;
        cin >> n;
        q.push_back({n,tt});
    }
    sort(q.begin(),q.end());
    vector<pair<int,string>> res;
    int last = 1;
    int lastp = 0;
    for(int tt = 0; tt < t; tt++){
        if(q[tt].first == 1){
            res.push_back({q[tt].second,"1/1\n"});
            continue;
        }else if(q[tt].first == 2){
            res.push_back({q[tt].second,"1/2\n"});
            continue;
        }
        
        for(int i = last; i <= q[tt].first; i++){
            if(wh[i] != -1){
                mp[pot[wh[i]].first] = wh[i];
                lastp = wh[i];
            }
            last++;
        }
        double ans = 0.0;
        int co = 0;
        res.push_back({q[tt].second,""});
        for(auto &u: mp){
            ll a = F[wh[pot[u.second].second]][lastp];
            euclid(a,pot[u.second].second,a,y);
            if(a <0) a += pot[u.second].second;
            if(co > 0)(res.back()).second+="+";
            (res.back()).second+=to_string(a)+"/"+to_string(pot[u.second].second);
            co++;
            ans += (double)a/(double)pot[u.second].second;
        }
        ans += 0.5;
        int xx = floor(ans);
        (res.back()).second+="-"+to_string(xx)+"/1\n";
    }
    sort(res.begin(),res.end());
    for(auto &u: res) for(auto&e:u.second) printf("%c",e);
}

详细

Test #1:

score: 100
Accepted
time: 71ms
memory: 216772kb

input:

2
3
6

output:

2/3+1/2-1/1
3/5+2/3+3/4-2/1

result:

ok OK, t = 2

Test #2:

score: 0
Accepted
time: 77ms
memory: 216668kb

input:

1
1

output:

1/1

result:

ok OK, t = 1

Test #3:

score: 0
Accepted
time: 79ms
memory: 216856kb

input:

10
1
2
3
4
5
6
7
8
9
10

output:

1/1
1/2
2/3+1/2-1/1
1/3+3/4-1/1
3/5+2/3+3/4-2/1
3/5+2/3+3/4-2/1
2/7+4/5+2/3+1/4-2/1
1/7+2/5+1/3+1/8-1/1
5/7+4/5+1/9+3/8-2/1
5/7+4/5+1/9+3/8-2/1

result:

ok OK, t = 10

Test #4:

score: -100
Wrong Answer
time: 95ms
memory: 216684kb

input:

54
7
20
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
3
47
81

output:

2/7+4/5+2/3+1/4-2/1
15/19+12/17+4/13+9/11+2/7+3/5+5/9+15/16-5/1
1/2
2/3+1/2-1/1
1/3+3/4-1/1
3/5+2/3+3/4-2/1
3/5+2/3+3/4-2/1
2/7+4/5+2/3+1/4-2/1
1/7+2/5+1/3+1/8-1/1
5/7+4/5+1/9+3/8-2/1
5/7+4/5+1/9+3/8-2/1
1/11+3/7+4/5+5/9+1/8-2/1
1/11+3/7+4/5+5/9+1/8-2/1
10/13+6/11+4/7+3/5+8/9+5/8-4/1
10/13+6/11+4/7+...

result:

wrong answer Numerator = 0 is out of range [1..29]