結果

問題 No.1855 Intersected Lines
ユーザー HIR180HIR180
提出日時 2022-02-25 22:06:59
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 132 ms / 2,000 ms
コード長 5,671 bytes
コンパイル時間 3,135 ms
コンパイル使用メモリ 190,992 KB
実行使用メモリ 20,640 KB
最終ジャッジ日時 2024-07-03 16:59:44
合計ジャッジ時間 8,344 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 13 ms
15,820 KB
testcase_01 AC 14 ms
15,872 KB
testcase_02 AC 14 ms
15,872 KB
testcase_03 AC 13 ms
15,872 KB
testcase_04 AC 14 ms
16,128 KB
testcase_05 AC 13 ms
15,988 KB
testcase_06 AC 14 ms
16,100 KB
testcase_07 AC 13 ms
16,032 KB
testcase_08 AC 99 ms
18,756 KB
testcase_09 AC 114 ms
20,164 KB
testcase_10 AC 100 ms
18,624 KB
testcase_11 AC 121 ms
20,416 KB
testcase_12 AC 87 ms
17,984 KB
testcase_13 AC 71 ms
16,960 KB
testcase_14 AC 95 ms
18,364 KB
testcase_15 AC 125 ms
20,464 KB
testcase_16 AC 116 ms
19,904 KB
testcase_17 AC 124 ms
20,544 KB
testcase_18 AC 125 ms
20,388 KB
testcase_19 AC 126 ms
20,416 KB
testcase_20 AC 129 ms
20,640 KB
testcase_21 AC 132 ms
20,416 KB
testcase_22 AC 128 ms
20,544 KB
testcase_23 AC 115 ms
20,160 KB
testcase_24 AC 113 ms
20,288 KB
testcase_25 AC 113 ms
20,144 KB
testcase_26 AC 115 ms
20,284 KB
testcase_27 AC 116 ms
20,284 KB
testcase_28 AC 13 ms
15,872 KB
testcase_29 AC 13 ms
16,000 KB
testcase_30 AC 14 ms
16,000 KB
testcase_31 AC 13 ms
15,852 KB
testcase_32 AC 14 ms
15,872 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//Let's join Kaede Takagaki Fan Club !!
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cassert>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <cassert>
#include <iomanip>
#include <chrono>
#include <random>
#include <bitset>
#include <complex>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
#define int long long
//#define L __int128
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define a first
#define b second
#define fi first
#define sc second
//#define rng(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
#define all(x) x.begin(),x.end()
#define si(x) int(x.size())
#define pcnt(x) __builtin_popcountll(x)

#ifdef LOCAL
#define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl
#else
#define dmp(x) void(0)
#endif
 
template<class t,class u> bool chmax(t&a,u b){if(a<b){a=b;return true;}else return false;}
template<class t,class u> bool chmin(t&a,u b){if(b<a){a=b;return true;}else return false;}
 
template<class t> using vc=vector<t>;
 
template<class t,class u>
ostream& operator<<(ostream& os,const pair<t,u>& p){
	return os<<"{"<<p.fi<<","<<p.sc<<"}";
}
 
template<class t> ostream& operator<<(ostream& os,const vc<t>& v){
	os<<"{";
	for(auto e:v)os<<e<<",";
	return os<<"}";
}
 
 //https://codeforces.com/blog/entry/62393
struct custom_hash {
	static uint64_t splitmix64(uint64_t x) {
		// http://xorshift.di.unimi.it/splitmix64.c
		x += 0x9e3779b97f4a7c15;
		x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
		x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
		return x ^ (x >> 31);
	}
 
	size_t operator()(uint64_t x) const {
		static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
		return splitmix64(x + FIXED_RANDOM);
	}
	//don't make x negative!
	size_t operator()(pair<int,int> x)const{
		return operator()(uint64_t(x.first)<<32|x.second);
	}
};
//unordered_set -> dtype, null_type
//unordered_map -> dtype(key), dtype(value)
using namespace __gnu_pbds;
template<class t,class u>
using hash_table=gp_hash_table<t,u,custom_hash>;

template<class T>
void g(T &a){
	cin >> a;
}
template<class T>
void o(const T &a,bool space=false){
	cout << a << (space?' ':'\n');
}
//ios::sync_with_stdio(false);
const ll mod = 998244353;
//const ll mod = 1000000007;
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
template<class T>
void add(T&a,T b){
	a+=b;
	if(a >= mod) a-=mod;
}
ll modpow(ll x,ll n){
	ll res=1;
	while(n>0){
		if(n&1) res=res*x%mod;
		x=x*x%mod;
		n>>=1;
	}
	return res;
}
#define _sz 800005
ll F[_sz],R[_sz];
void make(){
	F[0] = 1;
	for(int i=1;i<_sz;i++) F[i] = F[i-1]*i%mod;
	R[_sz-1] = modpow(F[_sz-1], mod-2);
	for(int i=_sz-2;i>=0;i--) R[i] = R[i+1] * (i+1) % mod;
}
ll C(int a,int b){
	if(b < 0 || a < b) return 0;
	return F[a]*R[b]%mod*R[a-b]%mod;
}
int s, n;
int t[105]={837999373,313580260,505603539,24153594,328755408,180832141,3564012,623549475,948586464,600151703,103301913,118555701,312830265,138894910,8602635,391938921,490519779,94713314,341087236,46796575,622213052,514331555,245435618,438425500,189778246,979296190,459460284,901002909,645695209,891313291,180467815,525968915,289206327,564758681,235130858,950038481,15311243,238703419,143846931,625627616,321857112,390400372,52725121,327806090,693370916,32055438,427700806,317190899,196383360,141879510,727976930,66184294,854393807,282970591,175404832,322246221,921939064,366741457,312430909,750348159,289398141,128011307,923484137,684399504,420499048,958870061,936599017,71920520,791758286,742379541,777193439,859365829,938368272,31921323,661797027,185637776,806063720,568973445,791607937,934543240,340619730,638168352,635368915,338314267,211345147,374557572,408294734,230678080,783222480,159960230,806264491,897355463,667751870,760246454,611815003,433927037,780714991,137196301,531695784};

void solve(){make();
	cin>>s>>n;
	vc<P>vec;
	int r = 0, b = 0;
	rep(i, n){
		string c; int v; cin >> c >> v;
		int j = (c[0] == 'R');
		if(vec.empty() or vec.back().b != j) vec.eb(v, j);
		else vec.back().a += v;
		
		(j?r:b) += v;
	}
	auto f = [&](int x){
		if(x < 4) return 0LL;
		int ret = R[4];
		for(int i=x;i>=x-3;i--){
			ret = ret * i % mod;
		}
		return ret;
	};
	int ans = 0;
	ans += f(r)*modpow((r-1)*(r-3)%mod, mod-2)%mod
	+f(b)*modpow((b-1)*(b-3)%mod, mod-2)%mod;
	
	rep(_, 2){
		vc<int>rui(vec.size()+1, 0);
		int sum = 0;
		for(int i=0;i<vec.size();i++){
			if(vec[i].b == 0) sum += vec[i].a;
			else{
				rui[i+1] = sum * vec[i].a;
			}
			rui[i+1] = (rui[i+1] + rui[i]) % mod;
		}
		sum = 0;
		for(int i=vec.size()-1;i>=0;i--){
			if(vec[i].b == 1) sum += vec[i].a;
			else{
				ans += rui[i] * vec[i].a % mod * sum % mod*modpow(r-1,mod-2)%mod
				*modpow(b-1,mod-2)%mod;
			}
		}
		for(int i=0;i<vec.size();i++) vec[i].b ^= 1;
	}
	
	ans = ans % mod;
	o((ans%mod+mod)%mod);
}
signed main(){
	cin.tie(0);
	ios::sync_with_stdio(0);
	cout<<fixed<<setprecision(20);
	int t; t = 1; //cin >> t;
	while(t--) solve();
}
0