結果

問題 No.2649 [Cherry 6th Tune C] Anthem Flower
ユーザー shobonvip
提出日時 2024-02-23 21:35:23
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 310 ms / 2,000 ms
コード長 2,144 bytes
コンパイル時間 2,055 ms
コンパイル使用メモリ 199,928 KB
最終ジャッジ日時 2025-02-19 19:32:07
ジャッジサーバーID
(参考情報)
judge3 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--)

template <typename T> bool chmin(T &a, const T &b) {
	if (a <= b) return false;
	a = b;
	return true;
}

template <typename T> bool chmax(T &a, const T &b) {
	if (a >= b) return false;
	a = b;
	return true;
}

template <typename T> T max(vector<T> &a){
	assert(!a.empty());
	T ret = a[0];
	for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]);
	return ret;
}

template <typename T> T min(vector<T> &a){
	assert(!a.empty());
	T ret = a[0];
	for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]);
	return ret;
}

template <typename T> T sum(vector<T> &a){
	T ret = 0;
	for (int i=0; i<(int)a.size(); i++) ret += a[i];
	return ret;
}

void solve(){
	string s; cin >> s;
	vector<int> x;
	rep(i,0,(int)s.size()){
		x.push_back(s[i] - '0');
	}
	
	auto increment = [&](vector<int> x) -> vector<int> {
		vector<int> y;
		int up = 1;
		rrep(i,0,(int)x.size()){
			if (up == 0){
				y.push_back(x[i]);
			}else{
				if (x[i] + up >= 10){
					y.push_back(0);
				}else{
					y.push_back(x[i] + up);
					up = 0;
				}
			}
		}
		if (up == 1){
			y.push_back(1);
		}
		reverse(y.begin(), y.end());
		return y;
	};

	auto half = [&](vector<int> x) -> vector<int> {
		assert(x.back() % 2 == 0);
		vector<int> y = x;
		vector<int> z((int)y.size());
		rep(i,0,(int)x.size()){
			z[i] = y[i] / 2;
			if (i < (int)x.size() - 1){
				y[i] %= 2;
				y[i+1] += y[i] * 10;
			}
		}
		return z;
	};

	
	
	int m; cin >> m;
	ll tar1 = 0;
	ll tar2 = 0;
	if (x.back() % 2 == 1){
		rep(i,0,(int)x.size()){
			tar1 = (tar1 * 10 + x[i]) % m;
		}
		x = increment(x);
		x = half(x);
		rep(i,0,(int)x.size()){
			tar2 = (tar2 * 10 + x[i]) % m;
		}
	}else{
		vector<int> y = half(x);
		rep(i,0,(int)y.size()){
			tar1 = (tar1 * 10 + y[i]) % m;

		}
		x = increment(x);
		rep(i,0,(int)x.size()){
			tar2 = (tar2 * 10 + x[i]) % m;

		}
	}
	cout << tar1 * tar2 % m << '\n';
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int t; cin >> t;
	while(t--) solve();
}

0