結果
問題 | No.1404 誕生日プレゼント |
ユーザー | cn_449 |
提出日時 | 2020-12-09 00:58:01 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 124 ms / 3,153 ms |
コード長 | 4,227 bytes |
コンパイル時間 | 1,377 ms |
コンパイル使用メモリ | 144,296 KB |
実行使用メモリ | 16,148 KB |
最終ジャッジ日時 | 2024-09-18 22:35:01 |
合計ジャッジ時間 | 27,900 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 24 ms
7,040 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 25 ms
6,784 KB |
testcase_03 | AC | 25 ms
6,912 KB |
testcase_04 | AC | 25 ms
7,040 KB |
testcase_05 | AC | 26 ms
7,040 KB |
testcase_06 | AC | 25 ms
7,040 KB |
testcase_07 | AC | 25 ms
7,040 KB |
testcase_08 | AC | 26 ms
7,040 KB |
testcase_09 | AC | 26 ms
7,040 KB |
testcase_10 | AC | 26 ms
7,040 KB |
testcase_11 | AC | 27 ms
7,040 KB |
testcase_12 | AC | 28 ms
7,040 KB |
testcase_13 | AC | 112 ms
15,996 KB |
testcase_14 | AC | 97 ms
16,132 KB |
testcase_15 | AC | 124 ms
15,996 KB |
testcase_16 | AC | 102 ms
16,112 KB |
testcase_17 | AC | 99 ms
16,024 KB |
testcase_18 | AC | 103 ms
16,144 KB |
testcase_19 | AC | 109 ms
16,148 KB |
testcase_20 | AC | 108 ms
15,984 KB |
testcase_21 | AC | 102 ms
16,112 KB |
testcase_22 | AC | 103 ms
16,108 KB |
testcase_23 | AC | 15 ms
5,376 KB |
testcase_24 | AC | 16 ms
5,376 KB |
testcase_25 | AC | 12 ms
5,376 KB |
testcase_26 | AC | 4 ms
5,376 KB |
testcase_27 | AC | 6 ms
5,376 KB |
testcase_28 | AC | 97 ms
14,952 KB |
ソースコード
#include <iostream> #include <vector> #include <algorithm> #include <cmath> #include <string> #include <queue> #include <stack> #include <set> #include <map> #include <iomanip> #include <utility> #include <tuple> #include <functional> #include <bitset> #include <cassert> #include <complex> #include <stdio.h> #include <time.h> #include <numeric> #include <random> #include <unordered_map> #include <unordered_set> #define all(a) a.begin(),a.end() #define rep(i, n) for (ll i = 0; i < (n); i++) #define pb push_back #define debug(x) cerr << __LINE__ << ' ' << #x << ':' << (x) << '\n' #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") using namespace std; typedef long long ll; typedef unsigned int uint; typedef unsigned long long ull; typedef long double ld; typedef pair<ll, ll> P; typedef complex<ld> com; template<class T> using prique = priority_queue<T, vector<T>, greater<T>>; constexpr int inf = 1000000010; constexpr ll INF = 1000000000000000010; constexpr int mod1e9 = 1000000007; constexpr int mod998 = 998244353; constexpr ld eps = 1e-12; constexpr ld pi = 3.141592653589793238; constexpr ll ten(int n) { return n ? 10 * ten(n - 1) : 1; }; int dx[] = { 1,0,-1,0,1,1,-1,-1 }; int dy[] = { 0,1,0,-1,1,-1,1,-1 }; void fail() { cout << "-1\n"; exit(0); } void no() { cout << "No\n"; exit(0); } template<class T, class U> inline bool chmax(T &a, const U &b) { if (a < b) { a = b; return true; } return false; } template<class T, class U> inline bool chmin(T &a, const U &b) { if (a > b) { a = b; return true; } return false; } template<class T> istream &operator >> (istream &s, vector<T> &v) { for (auto &e : v) s >> e; return s; } template<class T> ostream &operator << (ostream &s, const vector<T> &v) { for (auto &e : v) s << e << ' '; return s; } struct fastio { fastio() { cin.tie(0); cout.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); cerr << fixed << setprecision(20); } }fastio_; ll modinv(ll a, ll mod) { a %= mod; if (a == 0) abort(); ll b = mod, u = 1, v = 0; while (b) { ll t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= mod; if (u < 0) u += mod; return u; } int main() { int n, p; cin >> n >> p; assert(1 <= n && n <= 100000); assert(p == 223577); vector<ll> a(n), b(n); rep(i, n) { cin >> a[i]; assert(0 <= a[i] && a[i] < p); } rep(i, n) { cin >> b[i]; assert(0 <= b[i] && b[i] < p); } int check = 0; check += count(a.begin(), a.end(), 0) == n; check += count(b.begin(), b.end(), 0) == n; if (check == 2) { cout << "0\n"; return 0; } if (check == 1) { cout << "-1\n"; return 0; } if (n == 1) { int dbl = 0; rep(_, p) { a[0] *= 2, a[0] %= p, dbl++; if (a[0] == b[0]) break; } if (a[0] != b[0]) { cout << "-1\n"; return 0; } cout << dbl + 1 << '\n'; rep(_, dbl) cout << "1 1 1\n"; cout << "2\n"; return 0; } vector<ll> powtwo; int memo = 1; rep(_, 2100) { powtwo.pb(memo); memo *= 2; memo %= p; } vector<P> table(p); rep(i, 2100) { for (int j = i; j < 2100; j++) { table[(powtwo[i] + powtwo[j]) % p] = { i,j }; } } vector<P> ans; int ax = -1, bx = -1; rep(i, n) { if (a[i] != 0) ax = i; if (b[i] != 0) bx = i; } if (ax == bx) { int ay = ax + 1; if (ay == n) ay = 0; if (a[ay] == 0) { ans.pb({ ay,ax }); a[ay] += a[ax]; a[ay] %= p; } ax = ay; } ll cnt = (b[bx] * modinv(214472, p) % p - a[bx] + p) * modinv(a[ax], p) % p; if (cnt) { rep(_, table[cnt].first) { ans.pb({ ax,ax }); a[ax] *= 2; a[ax] %= p; } ans.pb({ bx,ax }); a[bx] += a[ax]; a[bx] %= p; rep(_, table[cnt].second - table[cnt].first) { ans.pb({ ax,ax }); a[ax] *= 2; a[ax] %= p; } ans.pb({ bx,ax }); a[bx] += a[ax]; a[bx] %= p; } vector<ll> plus(n); rep(i, n) plus[i] = (b[i] - a[i] + p) * modinv(a[bx], p) % p; vector<vector<ll>> change(2100); rep(i, n) { if (i == bx || !plus[i]) continue; change[table[plus[i]].first].pb(i); change[table[plus[i]].second].pb(i); } rep(i, 2100) { for (int j : change[i]) ans.pb({ j,bx }); ans.pb({ bx,bx }); } assert(ans.size() < p); cout << ans.size() + 1 << '\n'; for (P pa : ans) cout << 1 << ' ' << pa.first + 1 << ' ' << pa.second + 1 << '\n'; cout << "2\n"; }