QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#843728 | #9962. Diminishing Fractions | ucup-team2796# | WA | 0ms | 3772kb | C++17 | 5.1kb | 2025-01-04 23:58:35 | 2025-01-04 23:58:35 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rrep(i, a, b) for (ll i = (ll)(b)-1; i >= (ll)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())
using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;
template <typename T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
assert(y != 0);
if (y < 0)
x = -x, y = -y;
return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
assert(y != 0);
if (y < 0)
x = -x, y = -y;
return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
return (x == 0 ? -1 : __builtin_ctzll(x));
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
os << "P(" << p.first << ", " << p.second << ")";
return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
os << "{";
for (int i = 0; i < vec.size(); i++) {
os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
}
os << "}";
return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
os << "{";
for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
os << "(" << itr->first << ", " << itr->second << ")";
itr++;
if (itr != map_var.end())
os << ", ";
itr--;
}
os << "}";
return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
os << "{";
for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
os << *itr;
++itr;
if (itr != set_var.end())
os << ", ";
itr--;
}
os << "}";
return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
for (; a[i] != ',' && a[i] != '\0'; i++)
cerr << a[i];
cerr << ":" << b << " ";
_show(i + 1, a, c...);
}
/**
* @brief template
*/
ll extgcd(ll a, ll b, ll &x, ll &y) {
ll d = a;
if(b != 0) {
d = extgcd(b, a % b, y, x);
y -= (a / b) * x;
} else {
x = 1;
y = 0;
}
return d;
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
int MAXS = 10;
vector<bool> Prime(MAXS+1, true);
Prime[0] = Prime[1] = false;
rep(i,2,MAXS+1) {
if (Prime[i]) {
rep(j,2,MAXS+1) {
if (i*j > MAXS) break;
Prime[i*j] = false;
}
}
}
vector<int> P(MAXS+1,-1);
rep(i,0,MAXS+1) {
if (!Prime[i]) continue;
int Cur = i;
while(Cur <= MAXS) {
P[Cur] = i;
Cur *= i;
}
}
vector<vector<ll>> ANS(MAXS+1, vector<ll>(MAXS+1, 0));
ANS[1][1] = 1;
ANS[2][1] = 1;
ANS[3][1] = -1, ANS[3][2] = 1, ANS[3][3] = 2;
rep(i,4,MAXS+1) {
if (P[i] == -1) {
rep(j,1,i+1) ANS[i][j] = ANS[i-1][j];
continue;
}
ll p = P[i];
rep(j,1,i) {
if (ANS[i-1][j] == 0) continue;
if (j == 1) {
ANS[i][p] += ANS[i-1][j];
continue;
}
if (j % p == 0) {
ANS[i][j*p] += ANS[i-1][j];
continue;
}
ll x, y;
extgcd(j,p,x,y);
ANS[i][p] += ANS[i-1][j] * x;
ANS[i][j] += ANS[i-1][j] * y;
}
if (i != p) {
ANS[i][i] += ANS[i][p] * i/p;
ANS[i][p] = 0;
}
rep(j,1,i+1) {
ll x = floor(ANS[i][j], j);
ANS[i][j] -= j * x;
ANS[i][1] += x;
}
}
int _;
cin >> _;
while(_--) {
int N;
cin >> N;
vector<pair<int,int>> AS;
rep(i,1,N+1) if (ANS[N][i] != 0) AS.push_back({ANS[N][i], i});
cout << AS[0].first << '/' << AS[0].second;
rep(i,1,AS.size()) {
cout << (AS[i].first > 0 ? "+" : "");
cout << AS[i].first << '/' << AS[i].second;
}
cout << endl;
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3772kb
input:
2 3 6
output:
-1/1+1/2+2/3 -2/1+2/3+3/4+3/5
result:
ok OK, t = 2
Test #2:
score: 0
Accepted
time: 0ms
memory: 3516kb
input:
1 1
output:
1/1
result:
ok OK, t = 1
Test #3:
score: -100
Wrong Answer
time: 0ms
memory: 3708kb
input:
10 1 2 3 4 5 6 7 8 9 10
output:
1/1 1/1 -1/1+1/2+2/3 -1/1+1/3+3/4 -2/1+2/3+3/4+3/5 -2/1+2/3+3/4+3/5 -2/1+2/3+1/4+4/5+2/7 -1/1+1/3+2/5+1/7+1/8 -2/1+4/5+5/7+3/8+1/9 -2/1+4/5+5/7+3/8+1/9
result:
wrong answer Sums do not match for modulus 8809877585262195773