QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#316707 | #8169. R-Connected Components | ucup-team008# | RE | 2ms | 4328kb | C++20 | 4.5kb | 2024-01-28 02:21:39 | 2024-01-28 02:21:39 |
Judging History
answer
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <vector>
using namespace std;
// BEGIN NO SAD
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
typedef vector<int> vi;
#define f first
#define s second
#define derr if(1) cerr
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#define debug(x...) cerr << "\e[91m"<<__func__<<":"<<__LINE__<<" [" << #x << "] = ["; _print(x); cerr << "\e[39m" << flush;
// END NO SAD
template<class Fun>
class y_combinator_result {
Fun fun_;
public:
template<class T>
explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {}
template<class ...Args>
decltype(auto) operator()(Args &&...args) {
return fun_(std::ref(*this), std::forward<Args>(args)...);
}
};
template<class Fun>
decltype(auto) y_combinator(Fun &&fun) {
return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}
template<class T>
bool updmin(T& a, T b) {
if(b < a) {
a = b;
return true;
}
return false;
}
template<class T>
bool updmax(T& a, T b) {
if(b > a) {
a = b;
return true;
}
return false;
}
typedef int64_t ll;
struct disjoint_set {
vector<int> p, sz;
disjoint_set(int n) {
p.resize(n); fill(all(p), -1);
sz.resize(n); fill(all(sz), 1);
}
int find(int x) {
return p[x] < 0 ? x : (p[x] = find(p[x]));
}
int getsz(int x) {
return sz[find(x)];
}
bool merge(int x, int y) {
// x goes to y
x = find(x);
y = find(y);
if(x == y) return false;
p[x] = y;
sz[y] += sz[x];
return true;
}
};
int slow(int n) {
if(n == 1) return 1;
disjoint_set dsu(n*n);
int ncomps = n*n;
for(int a = 0; a < n*n; a++) for(int b = 0; b < a; b++) {
int xa = a/n; int ya = a%n;
int xb = b/n; int yb = b%n;
xa -= xb;
ya -= yb;
if(xa*xa+ya*ya==n) ncomps -= dsu.merge(a, b);
}
if(ncomps == n*n) return -1;
return ncomps;
}
const int SZ = 2e5;
int minp[SZ];
void init() {
iota(minp, minp + SZ, 0);
for(int i = 2; i * i < SZ; i++) {
for(int j = i*i; j < SZ; j += i) updmin(minp[j], i);
}
}
int fast(int n) {
int ret = n;
while(n > 1) {
int p = minp[n];
int c = 0;
while(n%p==0) {
c++;
n /= p;
}
if(p%4 == 3 && c%2 == 1) return -1;
if(p%4 == 1) {
while(ret%p==0) ret /= p;
}
}
return ret;
}
void rsolve() {
int n;
cin >> n;
int ret = fast(n);
if(ret < 0) cout << "inf\n";
else cout << ret << "\n";
}
void solve() {
init();
for(int i = 1; false && i <= 1000; i++) {
debug(i);
debug(slow(i));
debug(fast(i));
assert(slow(i) == fast(i));
}
int t;
cin >> t;
while(t--) rsolve();
}
// what would chika do
// are there edge cases (N=1?)
// are array sizes proper (scaled by proper constant, for example 2* for koosaga tree)
// integer overflow?
// DS reset properly between test cases
// are you doing geometry in floating points
// are you not using modint when you should
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 4328kb
input:
3 1 2 3
output:
1 2 inf
result:
ok 3 tokens
Test #2:
score: -100
Runtime Error
input:
100 971962039 377418539 436722941 974460973 408831757 674955527 838941797 566099869 224191573 85539073 544795513 157335071 243499759 907206901 570172403 871918511 594778897 773009569 9371917 23810669 169348601 804358351 78636461 382633897 182514781 846151963 274168729 929192339 91532527 172531889 18...