Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <cmath>
- #include <vector>
- #include <complex>
- #include <iomanip>
- #include <cstring>
- #define task "SWAPGAME"
- using namespace std;
- using ll = long long;
- using ld = long double;
- using cd = complex<long double>;
- const long double PI = acos(-1);
- const int M = 2e5;
- const ll BASE = 1e15;
- const int gd = log10(BASE);
- const int maxn = M / gd + 1;
- struct Bignum
- {
- int n;
- ll a[maxn];
- Bignum(ll x = 0)
- {
- memset(a, 0, sizeof a);
- n = 0;
- do
- {
- a[n++] = x % BASE;
- x /= BASE;
- } while (x);
- }
- void fix()
- {
- ++n;
- for (int i = 0; i < n - 1; ++i)
- {
- a[i + 1] += a[i] / BASE;
- a[i] %= BASE;
- if (a[i] < 0)
- {
- a[i] += BASE;
- --a[i + 1];
- }
- }
- while (n > 1 && a[n - 1] == 0)
- --n;
- }
- Bignum &operator*=(const ll &x)
- {
- for (int i = 0; i < n; ++i)
- a[i] *= x;
- fix();
- return *this;
- }
- friend ostream &operator<<(ostream &out, const Bignum &x)
- {
- int i = x.n;
- while (i > 0 && x.a[i] == 0)
- --i;
- out << x.a[i];
- for (--i; ~i; --i)
- out << setw(gd) << setfill('0') << x.a[i];
- return out;
- }
- };
- const int N = 1e5 + 5;
- int a[N], b[N], n, l(0);
- bool vis[N];
- vector<int> adj[N], same[N], loop[N];
- void Read()
- {
- cin >> n;
- for (int i = 1; i <= n; ++i)
- cin >> a[i],
- same[a[i]].push_back(i);
- for (int i = 1; i <= n; ++i)
- cin >> b[i],
- same[b[i]].push_back(i);
- }
- void dfs(int v)
- {
- vis[v] = 1;
- loop[l].push_back(v);
- int cnt = 0;
- for (auto i : adj[v])
- if (!vis[i] && cnt == 0)
- {
- ++cnt;
- dfs(i);
- }
- }
- Bignum Pow(ll a, ll b)
- {
- Bignum ans(1);
- while (b--)
- ans *= a;
- return ans;
- }
- void Solve()
- {
- for (int i = 1; i <= n; ++i)
- if (same[i][0] != same[i][1])
- {
- adj[same[i][0]].push_back(same[i][1]);
- adj[same[i][1]].push_back(same[i][0]);
- }
- for (int i = 1; i <= n; ++i)
- if (!vis[i] && adj[i].size() != 0)
- {
- ++l;
- dfs(i);
- }
- cout << Pow(2, l) << "\n";
- int ans = 0;
- for (int i = 1; i <= l; ++i)
- {
- int cur = 0;
- for (int j = 1; j < loop[i].size(); ++j)
- {
- int t = loop[i][j],
- h = loop[i][j - 1];
- if (a[t] == a[h] || b[t] == b[h])
- {
- ++cur;
- swap(a[t], b[t]);
- }
- }
- ans += min(cur, (int)loop[i].size() - cur);
- }
- cout << ans;
- }
- int main()
- {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- if (fopen(task ".INP", "r"))
- {
- freopen(task ".INP", "r", stdin);
- freopen(task ".OUT", "w", stdout);
- }
- Read();
- Solve();
- }
Add Comment
Please, Sign In to add comment