# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
52298 |
2018-06-25T08:31:29 Z |
윤교준(#1348) |
Space Pirate (JOI14_space_pirate) |
C++11 |
|
1556 ms |
69168 KB |
#include <bits/stdc++.h>
#define rf(x) (x)=0;while(*p<48)p++;while(47<*p)(x)=((x)<<3)+((x)<<1)+(*p++&15);
#define pb push_back
#define sz(V) ((int)(V).size())
#define allv(V) ((V).begin()),((V).end())
#define revv(V) reverse(allv(V))
#define sorv(V) sort(allv(V))
#define univ(V) (V).erase(unique(allv(V)),(V).end())
#define upmin(a,b) (a)=min((a),(b))
#define upmax(a,b) (a)=max((a),(b))
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
int rd(int s, int e) { return rand() % (e-s+1) + s; }
ll gcd(ll a, ll b) { for(;b;a%=b,swap(a,b)){} return a; }
namespace SUB12 {
const int MAXN = 3005;
vector<int> CV[MAXN];
vector<int> UV[MAXN];
int UVI[MAXN][MAXN];
int CI[MAXN], CJ[MAXN], Cn;
int chki[MAXN];
bitset<MAXN> chk;
ll Ans[MAXN];
int A[MAXN];
int N; ll K;
int f(int a, int b) {
if(CI[UV[1][0]] == CI[a]) {
int l = sz(UV[1]) - 1;
l += (CJ[a] - CJ[UV[1][0]] + sz(CV[CI[a]])) % sz(CV[CI[a]]);
l++;
l += sz(UV[b]) - 1;
if(CI[UV[b][0]] != CI[a]) {
int idx = (K - l + CJ[UV[b][0]]) % sz(CV[CI[UV[b][0]]]);
return CV[CI[UV[b][0]]][idx];
}
int c = UV[b][0];
int cl = (CJ[a] - CJ[c] + sz(CV[CI[a]])) % sz(CV[CI[a]]);
cl++;
cl += sz(UV[b]) - 1;
int idx = (K - l) % cl;
if(idx <= (CJ[a] - CJ[c] + sz(CV[CI[a]])) % sz(CV[CI[a]])) {
idx = (CJ[c] + idx) % sz(CV[CI[a]]);
return CV[CI[a]][idx];
}
idx -= (CJ[a] - CJ[c] + sz(CV[CI[a]])) % sz(CV[CI[a]]);
idx--;
return UV[b][sz(UV[b])-idx-1];
}
if(0 <= UVI[1][a]) {
if(0 <= UVI[b][a]) {
int l = sz(UV[1]) - sz(UV[a]) + 1;
int cl = sz(UV[b]) - sz(UV[a]) + 1;
int idx = (K - l) % cl;
return UV[b][sz(UV[b])-idx-1];
}
int l = sz(UV[1]) - sz(UV[a]) + sz(UV[b]);
int c = UV[b][0];
int idx = (K - l + CJ[c]) % sz(CV[CI[c]]);
return CV[CI[c]][idx];
}
int l = sz(UV[1]) - 1;
int c = UV[1][0];
int idx = (K - l + CJ[c]) % sz(CV[CI[c]]);
return CV[CI[c]][idx];
}
int solve() {
fill(chki, chki+N+1, -1);
for(int i = 1; i <= N; i++) if(!chk[i]) {
vector<int> V;
int c = 0, cs = -1;
for(int idx = i;;) {
V.pb(idx);
chk[idx] = true;
chki[idx] = c++;
idx = A[idx];
if(chk[idx]) {
cs = chki[idx];
break;
}
}
for(int v : V) chki[v] = -1;
if(cs < 0) continue;
Cn++;
for(int i = cs; i < c; i++) {
CV[Cn].pb(V[i]);
CI[V[i]] = Cn;
CJ[V[i]] = i-cs;
}
}
for(int i = 1; i <= N; i++) if(CI[i]) UV[i].pb(i);
for(int i = 1; i <= N; i++) if(UV[i].empty()) {
vector<int> V;
for(int idx = i;;) {
V.pb(idx);
idx = A[idx];
if(!UV[idx].empty()) break;
}
revv(V);
for(int v : V) {
UV[v] = UV[A[v]];
UV[v].pb(v);
}
}
for(int i = 1; i <= N; i++) {
fill(UVI[i], UVI[i]+N+1, -1);
for(int j = 0; j < sz(UV[i]); j++)
UVI[i][UV[i][j]] = j;
}
for(int a = 1; a <= N; a++) for(int b = 1; b <= N; b++)
Ans[f(a, b)]++;
for(int i = 1; i <= N; i++)
printf("%lld\n", Ans[i]);
return 0;
}
}
ll mul(ll a, ll b, const ll &mod) {
ll ret = 0;
for(a %= mod, b %= mod; a; a >>= 1) {
if(a&1) ret = (ret + b) % mod;
b = (b<<1) % mod;
}
return ret;
}
ll mypow(ll a, ll b, const ll& mod) {
ll ret = 1; for(a %= mod; b; b >>= 1) {
if(b&1) ret = mul(ret, a, mod);
a = mul(a, a, mod);
}
return ret;
}
bool mrtest(ll x, ll a) {
if(!(x%a)) return false;
for(ll d = x-1, t;; d >>= 1) {
t = mypow(a, d, x);
if(d&1) return ((1 != t) && (x-1 != t));
if(x-1 == t) return false;
}
return false;
}
const int mrV[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};
bool mrrun(ll x, const bool iscut = true) {
if(2 == x) return true;
if(x < 2 || !(x&1)) return false;
if(iscut && x < 10000) {
for(int i = 3; i*i <= x; i += 2)
if(!(x%i)) return false;
return true;
}
for(int v : mrV) {
if(x == v) return true;
if(40 < x && mrtest(x, v)) return false;
}
return 40 < x;
}
ll f(ll x, ll n, ll c) { return mul(x, x, n) + c % n; }
void g(vector<ll> &ret, ll x) {
if(1 == x) return;
if(mrrun(x)) {
ret.pb(x); return;
}
for(ll a, b, c, t;;) {
a = b = rd(2, x-1);
c = rd(1, 20);
do {
a = f(a, x, c);
b = f(f(b, x, c), x, c);
} while(1 == (t = gcd(abs(a-b), x)));
if(a == b) continue;
g(ret, t); g(ret, x/t);
return;
}
}
void run(vector<ll> &ret, ll x) {
ret.clear(); if(x < 2) return;
for(; !(x&1); x >>= 1) ret.pb(2);
g(ret, x);
sorv(ret);
}
const int MAXN = 100005;
vector<int> CV[MAXN];
int CI[MAXN], CJ[MAXN], Cn;
bitset<MAXN> chk;
ll Ans[MAXN];
int A[MAXN];
int N; ll K;
void gazua(const vector<pii> &V, int t, int idx, int s, ll &ret) {
if(sz(V) == idx) {
ret += s+1;
return;
}
for(int i = 0; i <= V[idx].second; i++) {
gazua(V, t, idx+1, s, ret);
if(ll(V[idx].first) * s > t) break;
s *= V[idx].first;
}
}
int main() {
scanf("%d%lld", &N, &K);
for(int i = 1; i <= N; i++) scanf("%d", &A[i]);
if(N <= 3000) {
SUB12 :: N = N;
SUB12 :: K = K;
for(int i = 1; i <= N; i++)
SUB12 :: A[i] = A[i];
return SUB12 :: solve();
}
for(int i = 1; i <= N; i++) if(!chk[i]) {
Cn++;
for(int idx = i, c = 0;;) {
CI[idx] = Cn;
CJ[idx] = c++;
CV[Cn].pb(idx);
chk[idx] = true;
idx = A[idx];
if(chk[idx]) break;
}
}
Ans[(K + CJ[1]) % sz(CV[CI[1]])] += ll(N) * (N - sz(CV[CI[1]]));
for(int i = 1; i <= N; i++) {
if(CI[i] != CI[1]) {
Ans[i] += sz(CV[CI[1]]);
continue;
}
vector<int> V;
int t = sz(CV[CI[1]]);
{
vector<ll> Q;
run(Q, K - (CJ[i] - CJ[1] + t) % t);
for(ll v : Q) if(v <= t) V.pb(v);
}
vector<int> SV = V; univ(SV);
vector<pii> Q(sz(SV), pii(0, 0));
for(int i = 0; i < sz(SV); i++) Q[i].first = SV[i];
for(int i = 0, j = 0; i < sz(V); i++) {
if(Q[j].first != V[i]) j++;
Q[j].second++;
}
ll ret = 0;
gazua(Q, t, 0, 1, ret);
Ans[i] += ret;
}
for(int i = 1; i <= N; i++)
printf("%lld\n", Ans[i]);
return 0;
}
Compilation message
space_pirate.cpp: In function 'int main()':
space_pirate.cpp:233:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%lld", &N, &K);
~~~~~^~~~~~~~~~~~~~~~~~
space_pirate.cpp:234:35: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i = 1; i <= N; i++) scanf("%d", &A[i]);
~~~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
3324 KB |
Output is correct |
2 |
Correct |
5 ms |
3324 KB |
Output is correct |
3 |
Correct |
4 ms |
3356 KB |
Output is correct |
4 |
Correct |
4 ms |
3432 KB |
Output is correct |
5 |
Correct |
5 ms |
3448 KB |
Output is correct |
6 |
Correct |
5 ms |
3484 KB |
Output is correct |
7 |
Correct |
5 ms |
3484 KB |
Output is correct |
8 |
Correct |
5 ms |
3484 KB |
Output is correct |
9 |
Correct |
5 ms |
3488 KB |
Output is correct |
10 |
Correct |
6 ms |
3664 KB |
Output is correct |
11 |
Correct |
4 ms |
3664 KB |
Output is correct |
12 |
Correct |
4 ms |
3664 KB |
Output is correct |
13 |
Correct |
5 ms |
3664 KB |
Output is correct |
14 |
Correct |
4 ms |
3664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
3324 KB |
Output is correct |
2 |
Correct |
5 ms |
3324 KB |
Output is correct |
3 |
Correct |
4 ms |
3356 KB |
Output is correct |
4 |
Correct |
4 ms |
3432 KB |
Output is correct |
5 |
Correct |
5 ms |
3448 KB |
Output is correct |
6 |
Correct |
5 ms |
3484 KB |
Output is correct |
7 |
Correct |
5 ms |
3484 KB |
Output is correct |
8 |
Correct |
5 ms |
3484 KB |
Output is correct |
9 |
Correct |
5 ms |
3488 KB |
Output is correct |
10 |
Correct |
6 ms |
3664 KB |
Output is correct |
11 |
Correct |
4 ms |
3664 KB |
Output is correct |
12 |
Correct |
4 ms |
3664 KB |
Output is correct |
13 |
Correct |
5 ms |
3664 KB |
Output is correct |
14 |
Correct |
4 ms |
3664 KB |
Output is correct |
15 |
Correct |
242 ms |
40036 KB |
Output is correct |
16 |
Correct |
202 ms |
40036 KB |
Output is correct |
17 |
Correct |
217 ms |
40036 KB |
Output is correct |
18 |
Correct |
432 ms |
40036 KB |
Output is correct |
19 |
Correct |
300 ms |
40036 KB |
Output is correct |
20 |
Correct |
482 ms |
47644 KB |
Output is correct |
21 |
Correct |
1399 ms |
62748 KB |
Output is correct |
22 |
Correct |
686 ms |
62748 KB |
Output is correct |
23 |
Correct |
1432 ms |
69168 KB |
Output is correct |
24 |
Correct |
755 ms |
69168 KB |
Output is correct |
25 |
Correct |
189 ms |
69168 KB |
Output is correct |
26 |
Correct |
377 ms |
69168 KB |
Output is correct |
27 |
Correct |
485 ms |
69168 KB |
Output is correct |
28 |
Correct |
392 ms |
69168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1556 ms |
69168 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
3324 KB |
Output is correct |
2 |
Correct |
5 ms |
3324 KB |
Output is correct |
3 |
Correct |
4 ms |
3356 KB |
Output is correct |
4 |
Correct |
4 ms |
3432 KB |
Output is correct |
5 |
Correct |
5 ms |
3448 KB |
Output is correct |
6 |
Correct |
5 ms |
3484 KB |
Output is correct |
7 |
Correct |
5 ms |
3484 KB |
Output is correct |
8 |
Correct |
5 ms |
3484 KB |
Output is correct |
9 |
Correct |
5 ms |
3488 KB |
Output is correct |
10 |
Correct |
6 ms |
3664 KB |
Output is correct |
11 |
Correct |
4 ms |
3664 KB |
Output is correct |
12 |
Correct |
4 ms |
3664 KB |
Output is correct |
13 |
Correct |
5 ms |
3664 KB |
Output is correct |
14 |
Correct |
4 ms |
3664 KB |
Output is correct |
15 |
Correct |
242 ms |
40036 KB |
Output is correct |
16 |
Correct |
202 ms |
40036 KB |
Output is correct |
17 |
Correct |
217 ms |
40036 KB |
Output is correct |
18 |
Correct |
432 ms |
40036 KB |
Output is correct |
19 |
Correct |
300 ms |
40036 KB |
Output is correct |
20 |
Correct |
482 ms |
47644 KB |
Output is correct |
21 |
Correct |
1399 ms |
62748 KB |
Output is correct |
22 |
Correct |
686 ms |
62748 KB |
Output is correct |
23 |
Correct |
1432 ms |
69168 KB |
Output is correct |
24 |
Correct |
755 ms |
69168 KB |
Output is correct |
25 |
Correct |
189 ms |
69168 KB |
Output is correct |
26 |
Correct |
377 ms |
69168 KB |
Output is correct |
27 |
Correct |
485 ms |
69168 KB |
Output is correct |
28 |
Correct |
392 ms |
69168 KB |
Output is correct |
29 |
Incorrect |
1556 ms |
69168 KB |
Output isn't correct |
30 |
Halted |
0 ms |
0 KB |
- |