結果

問題 No.1404 誕生日プレゼント
ユーザー cn_449cn_449
提出日時 2020-12-09 01:06:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,231 bytes
コンパイル時間 1,588 ms
コンパイル使用メモリ 144,556 KB
実行使用メモリ 16,356 KB
最終ジャッジ日時 2023-10-19 02:39:40
合計ジャッジ時間 33,782 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 2 ms
4,348 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 18 ms
4,832 KB
testcase_24 AC 18 ms
4,832 KB
testcase_25 AC 14 ms
4,832 KB
testcase_26 AC 4 ms
4,348 KB
testcase_27 AC 6 ms
4,348 KB
testcase_28 AC 106 ms
15,204 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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";
}
0