QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#850661 | #9962. Diminishing Fractions | Zawos | WA | 1691ms | 110468kb | C++23 | 2.6kb | 2025-01-10 10:53:46 | 2025-01-10 10:53:47 |
Judging History
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];
int inv[5217][5217];
pair<ll,ll> extend_euclid(ll a, ll b) { // returns {x,y}
if (b == 0) { return {1, 0}; }
pair<ll,ll> p = extend_euclid(b, a % b);
return {p.second, p.first - a / b * p.second};
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
memset(wh,-1,sizeof(wh));
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) continue;
pair<ll,ll> pa = extend_euclid(pot[j].second,pot[i].second);
pa.first = (pa.first%pot[i].second+pot[i].second)%pot[i].second;
inv[i][j] = pa.first;
}
}
int t;
cin >>t;
while(t--){
int n;
cin >> n;
if(n == 1){
cout <<1<<'\n';
continue;
}else if(n == 2){
cout <<"1/2\n";
continue;
}
unordered_map<int,int> mp;
for(int i = 1; i <= n; i++){
if(wh[i] != -1){
mp[pot[wh[i]].first] = wh[i];
}
}
double ans = 0.0;
int co = 0;
for(auto &u: mp){
int a = 1;
for(auto &e:mp){
if(u.first == e.first) continue;
a =a*inv[u.second][e.second]%pot[u.second].second;
}
if(co > 0)cout<<'+';
cout<<a<<"/"<<pot[u.second].second;
co++;
ans += (double)a/(double)pot[u.second].second;
}
ans += 0.5;
cout<<"-"<<floor(ans)<<'\n';
}
}
详细
Test #1:
score: 0
Wrong Answer
time: 1691ms
memory: 110468kb
input:
2 3 6
output:
2/3+1/2-1 3/5+2/3+3/4-2
result:
wrong answer Expected '/' in fraction