結果

問題 No.1855 Intersected Lines
ユーザー HIR180HIR180
提出日時 2022-02-25 22:06:59
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 142 ms / 2,000 ms
コード長 5,671 bytes
コンパイル時間 2,748 ms
コンパイル使用メモリ 188,732 KB
実行使用メモリ 21,764 KB
最終ジャッジ日時 2023-09-16 17:03:46
合計ジャッジ時間 7,833 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 13 ms
16,080 KB
testcase_01 AC 13 ms
15,840 KB
testcase_02 AC 13 ms
15,832 KB
testcase_03 AC 13 ms
15,824 KB
testcase_04 AC 14 ms
15,956 KB
testcase_05 AC 13 ms
15,856 KB
testcase_06 AC 13 ms
15,832 KB
testcase_07 AC 13 ms
15,864 KB
testcase_08 AC 109 ms
18,468 KB
testcase_09 AC 122 ms
20,508 KB
testcase_10 AC 109 ms
18,396 KB
testcase_11 AC 132 ms
21,764 KB
testcase_12 AC 96 ms
17,628 KB
testcase_13 AC 80 ms
16,708 KB
testcase_14 AC 104 ms
18,304 KB
testcase_15 AC 135 ms
20,316 KB
testcase_16 AC 128 ms
21,304 KB
testcase_17 AC 142 ms
21,604 KB
testcase_18 AC 135 ms
20,580 KB
testcase_19 AC 136 ms
20,376 KB
testcase_20 AC 135 ms
20,912 KB
testcase_21 AC 135 ms
21,300 KB
testcase_22 AC 136 ms
20,116 KB
testcase_23 AC 123 ms
20,124 KB
testcase_24 AC 124 ms
20,164 KB
testcase_25 AC 124 ms
20,872 KB
testcase_26 AC 121 ms
20,240 KB
testcase_27 AC 124 ms
21,128 KB
testcase_28 AC 13 ms
15,820 KB
testcase_29 AC 13 ms
15,972 KB
testcase_30 AC 13 ms
16,048 KB
testcase_31 AC 13 ms
15,812 KB
testcase_32 AC 13 ms
15,828 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