結果
| 問題 | No.3516 Very Large Range Mod |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-04-25 10:19:27 |
| 言語 | C++23 (gcc 15.2.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 81 ms / 2,000 ms |
| コード長 | 7,458 bytes |
| 記録 | |
| コンパイル時間 | 4,412 ms |
| コンパイル使用メモリ | 377,060 KB |
| 実行使用メモリ | 6,400 KB |
| 最終ジャッジ日時 | 2026-04-25 10:19:39 |
| 合計ジャッジ時間 | 10,819 ms |
|
ジャッジサーバーID (参考情報) |
judge2_0 / judge1_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 30 |
コンパイルメッセージ
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/algorithm:62,
from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/x86_64-pc-linux-gnu/bits/stdc++.h:53,
from main.cpp:1:
In function 'constexpr const _Tp& std::min(const _Tp&, const _Tp&) [with _Tp = long long int]',
inlined from 'void solve()' at main.cpp:249:20:
/home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/stl_algobase.h:239:7: warning: 'postlen' may be used uninitialized [-Wmaybe-uninitialized]
239 | if (__b < __a)
| ^~
main.cpp: In function 'void solve()':
main.cpp:230:8: note: 'postlen' was declared here
230 | ll postlen;
| ^~~~~~~
ソースコード
#include <bits/stdc++.h>
using namespace std;
#include <atcoder/all>
using namespace atcoder;
// #include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision;
#define ll long long
#define ld long double
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define vi vector<int>
#define vl vector<ll>
#define vd vector<double>
#define vb vector<bool>
#define vs vector<string>
#define vc vector<char>
#define ull unsigned long long
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
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;
}
// #define ll int
// #define ll int128_t
// #define ll int256_t
// #define ll cpp_int
constexpr ll inf = (1ll << 60);
// constexpr ll inf = (1 << 30);
// const double PI=3.1415926535897932384626433832795028841971;
// ll rui(ll a,ll b){
// if(b==0)return 1;
// if(b%2==1) return a*rui(a*a,b/2);
// return rui(a*a,b/2);
// }
// vl fact;
// ll kai(ll n){
// fact.resize(n,1);
// rep(i,n-1)fact[i+1]=fact[i]*(i+1);
// }
// using mint = ld;
// using mint = modint998244353;//static_modint<998244353>
// using mint = modint1000000007;//static_modint<1000000007>
// using mint = static_modint<922267487>; // 多分落とされにくい NOT ntt-friendly
// using mint = static_modint<469762049>; // ntt-friendly
// using mint = static_modint<167772161>; // ntt-friendly
using mint = modint;//mint::set_mod(mod);
// ll const mod=1000000007ll;
// ll const mod=998244353ll;
// ll modrui(ll a,ll b,ll mod){
// a%=mod;
// if(b==0)return 1;
// if(b%2==1) return a*modrui(a*a%mod,b/2,mod)%mod;
// return modrui(a*a%mod,b/2,mod)%mod;
// }
// void incr(vl &v,ll n){// n進法
// ll k=v.size();
// v[k-1]++;
// ll now=k-1;
// while (v[now]>=n)
// {
// v[now]=0;
// if(now==0)break;
// v[now-1]++;
// now--;
// }
// return;
// }
// vector<mint> fact,invf;
// void init_modfact(ll sz){
// fact.resize(sz);
// invf.resize(sz);
// fact[0]=1;
// rep(i,sz-1){
// fact[i+1]=fact[i]*(i+1);
// }
// invf[sz-1]=1/fact[sz-1];
// for(ll i=sz-2; i>=0; i--){
// invf[i]=invf[i+1]*(i+1);
// }
// }
// mint choose(ll n,ll r){
// if(n<r || r<0)return 0;
// return fact[n]*invf[r]*invf[n-r];
// }
// vector<mint> modpow,invpow;
// void init_modpow(ll x,ll sz){
// mint inv=1/mint(x);
// modpow.assign(sz,1);
// invpow.assign(sz,1);
// rep(i,sz-1){
// modpow[i+1]=modpow[i]*x;
// invpow[i+1]=invpow[i]*inv;
// }
// }
// long long phi(long long n) {// O(sqrt(n))
// long long res = n;
// for (long long i = 2; i * i <= n; i++) {
// if (n % i == 0) {
// res -= res / i;
// while (n % i == 0) n /= i;
// }
// }
// if (n > 1) res -= res / n;
// return res;
// }
template<typename T>
struct Diophantine {
private:
// 外部からは一切呼び出せない補助関数
static T floor_div(T a, T b) {
T d = a / b, m = a % b;
if (m != 0 && ((a < 0) ^ (b < 0))) d--;
return d;
}
static T ceil_div(T a, T b) {
T d = a / b, m = a % b;
if (m != 0 && !((a < 0) ^ (b < 0))) d++;
return d;
}
static T extgcd(T a, T b, T &x, T &y) {
if (b == 0) { x = 1; y = 0; return a; }
T x1, y1;
T d = extgcd(b, a % b, x1, y1);
x = y1; y = x1 - a / b * y1;
return d;
}
public:
// ax + by = c かつ x in [lx, rx], y in [ly, ry] となる (x,y) の個数を返す
// 呼び出し方: ll ans = Diophantine<ll>::count(a, b, c, lx, rx, ly, ry);
static T count(T a, T b, T c, T lx, T rx, T ly, T ry) {
if (lx > rx || ly > ry) return 0;
if (a == 0 && b == 0) return (c == 0) ? (rx - lx + 1) * (ry - ly + 1) : 0;
if (a == 0) {
if (c % b != 0) return 0;
T y = c / b;
return (ly <= y && y <= ry) ? (rx - lx + 1) : 0;
}
if (b == 0) {
if (c % a != 0) return 0;
T x = c / a;
return (lx <= x && x <= rx) ? (ry - ly + 1) : 0;
}
T x0, y0;
T g = extgcd(abs(a), abs(b), x0, y0);
if (c % g != 0) return 0;
if (a < 0) x0 = -x0;
if (b < 0) y0 = -y0;
x0 *= c / g;
y0 *= c / g;
T bg = b / g, ag = -a / g;
T t_min = -4000000000000000000LL; // -4e18
T t_max = 4000000000000000000LL; // 4e18
auto update_t_range = [&](T v0, T coeff, T L, T R) {
if (coeff == 0) {
if (v0 < L || v0 > R) { t_min = 1; t_max = -1; }
} else if (coeff > 0) {
t_min = max(t_min, ceil_div(L - v0, coeff));
t_max = min(t_max, floor_div(R - v0, coeff));
} else {
t_min = max(t_min, ceil_div(R - v0, coeff));
t_max = min(t_max, floor_div(L - v0, coeff));
}
};
update_t_range(x0, bg, lx, rx);
update_t_range(y0, ag, ly, ry);
return max((T)0, t_max - t_min + 1);
}
};
void solve() {
ll n,k,m;
cin >> n >> k >> m;
mint::set_mod(m);
vl b(n);
vector<mint> c(n);
rep(i,n)cin >> b[i];
rep(i,n){
ll x;
cin >> x;
c[i]=x;
}
ll l=0;
ll r=0;
mint sum=0;
ll postlen;
{
ll lensum=0;
rep(i,n){
if(lensum+b[i]>=k){
r=i;
postlen=b[i]-(k-lensum);
sum+=(k-lensum)*c[i];
break;
}
lensum+=b[i];
sum+=b[i]*c[i];
}
}
ll ans=0;
if(sum==0)ans++;
ll prelen=b[0];
while(r<n){
ll dist=min(postlen,prelen);
mint diff=c[r]-c[l];
if(diff!=0){
// ll s,t;
// ll g=extend_gcd((ll)diff.val(),m,s,t);
// if(sum.val()%g==0){
// s*=-(sum.val())/g;
// t*=-(sum.val())/g;
// // ans={ 1 <= x <= dist | sum+(c[r]-c[l])*x==0 (mod m)}
// // x=s+(m/g)*Z
// s--;
// s%=m/g;
// s+=m/g;
// s%=m/g;
// s++;
// ll cnt=0;
// if(dist-s>=0)cnt=(dist-s)/(m/g)+1;
// ans+=cnt;
// }
ans+=Diophantine<ll>::count(diff.val(),m,-sum.val(),1,dist,-inf,inf);
}
if(diff==0){
if(sum==0){
ans+=dist;
}
}
postlen-=dist;
prelen-=dist;
sum+=diff*dist;
if(prelen==0){
l++;
if(l<n)prelen=b[l];
}
if(postlen==0){
r++;
if(r<n)postlen=b[r];
}
}
cout << ans << endl;
}
int main(){
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
// ll mx=450;
// vc fl(mx+1,0);
// for(ll d=2;d<=mx;d++){
// if(fl[d])continue;
// ll x=d;
// ps.push_back(x);
// while(x<=mx){
// fl[x]=1;
// x+=d;
// }
// }
ll t = 1;
// cin >> t;
while (t--) solve();
}