結果
| 問題 | No.2480 Sequence Sum |
| コンテスト | |
| ユーザー |
ryoku
|
| 提出日時 | 2026-09-26 11:38:25 |
| 言語 | C++23(gcc16) (gcc 16.1.0 + boost 1.92.0 + ACL) |
| 結果 |
AC
不安定
|
| 実行時間 | 9 ms / 500 ms |
| + 574µs | |
| コード長 | 20,718 bytes |
| 記録 | |
| コンパイル時間 | 4,993 ms |
| コンパイル使用メモリ | 324,396 KB |
| 実行使用メモリ | 9,844 KB |
| 最終ジャッジ日時 | 2026-09-26 11:38:33 |
| 合計ジャッジ時間 | 6,427 ms |
|
ジャッジサーバーID (参考情報) |
judge3_0 / judge2_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 13 |
ソースコード
#define CP_BUNDLED_SOURCE
#ifndef CP_BUNDLE_HEADER_3840A09F237A779E
#define CP_BUNDLE_HEADER_3840A09F237A779E
#ifdef TEMPLATE
#else
#define TEMPLATE
# pragma GCC optimize("O3")
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <climits>
#include <cassert>
#include <functional>
#include <iterator>
#include <utility>
#include <complex>
#include <bitset>
#include <chrono>
#include <random>
#include <limits>
#include <optional>
#include <variant>
#include <any>
#include <array>
#include <bit>
#include <compare>
#include <concepts>
#include <numbers>
#include <ranges>
#include <span>
#include <string_view>
#include <tuple>
#include <type_traits>
#include <version>
using namespace std;
using uint=unsigned;
using ll=long long;
using ull=unsigned long long;
using ld=long double;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
using i128=__int128;
using u128=unsigned __int128;
template<class T>using vc=vector<T>;
template<class T>using vvc=vc<vc<T>>;
template<class T>using vvvc=vvc<vc<T>>;
template<class T>using smpq=priority_queue<T,vector<T>,greater<T>>;
template<class T>using bipq=priority_queue<T>;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define REP(i,j,n) for(ll i=(j);i<(ll)(n);i++)
#define DREP(i,n,m) for(ll i=(n);i>=(m);i--)
#define drep(i,n) for(ll i=((n)-1);i>=0;i--)
#define rall(x) x.rbegin(),x.rend()
#define mp(...) make_pair(__VA_ARGS__)
#define pb push_back
#define fi first
#define se second
#define is insert
#define bg begin()
#define ed end()
#define all(x) x.begin(),x.end()
void scan(int&a) { cin >> a; }
void scan(ll&a) { cin >> a; }
void scan(string&a) { cin >> a; }
void scan(char&a) { cin >> a; }
void scan(uint&a) { cin >> a; }
void scan(ull&a) { cin >> a; }
void scan(bool&a) { cin >> a; }
void scan(ld&a){ cin>> a;}
template<class T> void scan(vector<T>&a) { for(auto&x:a) scan(x); }
void read() {}
template<class Head, class... Tail> void read(Head&head, Tail&... tail) { scan(head); read(tail...); }
#define INT(...) int __VA_ARGS__; read(__VA_ARGS__);
#define LL(...) ll __VA_ARGS__; read(__VA_ARGS__);
#define ULL(...) ull __VA_ARGS__; read(__VA_ARGS__);
#define STR(...) string __VA_ARGS__; read(__VA_ARGS__);
#define VC(type, name, ...) vector<type> name(__VA_ARGS__); read(name);
#define VVC(type, name, size, ...) vector<vector<type>> name(size, vector<type>(__VA_ARGS__)); read(name);
template<class T>void print(T a) { cout << a; }
template<class T> void print(vector<T>a) { for(int i=0;i<(int)a.size();i++){if(i)cout<<" ";print(a[i]);}}
void PRT() { cout <<endl; return ; }
template<class T> void PRT(T a) { print(a); cout <<endl; return; }
template<class Head, class... Tail> void PRT(Head head, Tail ... tail) { print(head); cout << " "; PRT(tail...); return; }
template<class T,class F>
bool chmin(T &x, F y){
if(x>y){
x=y;
return true;
}
return false;
}
template<class T, class F>
bool chmax(T &x, F y){
if(x<y){
x=y;
return true;
}
return false;
}
template<typename T>
T floor(T a,T b){
return a/b-(a%b&&((a<0)!=(b<0)));
}
template<typename T>
T ceil(T a,T b){
return a/b+(a%b&&((a<0)==(b<0)));
}
template<typename T>
T bmod(T x,T y){
return x-y*floor(x,y);
}
template<typename T>
pair<T,T>divmod(T x,T y){
T q=floor(x,y);
return{q,x-q*y};
}
void YesNo(bool b){
cout<<(b?"Yes":"No")<<endl;
}
void YESNO(bool b){
cout<<(b?"YES":"NO")<<endl;
}
void AliceBob(bool b){
cout<<(b?"Alice":"Bob")<<endl;
}
void TakahashiAoki(bool b){
cout<<(b?"Takahashi":"Aoki")<<endl;
}
void Yes(){
cout<<"Yes"<<endl;
}
void No(){
cout<<"No"<<endl;
}
vc<int>stovi(const string&s,const string&S){
vc<int>v(s.size());
rep(i,s.size()){
auto t=S.find(s[i]);
assert(t!=string::npos);
v[i]=t;
}
return v;
}
template<class T=ll>
T isqrt(T x){
T F=sqrtl(x);
while((F+1)*(F+1)<=x)F++;
while(F*F>x)F--;
return F;
}
template<class T>
T tri(T x){return x*(x-1)/2;}
//[l,r)
template<class T>T tri(T l,T r){return (r-l)*(l+r-1)/2;}
//n を先頭に持ってくる
template<class T>
vc<T>rot(vc<T>v,int n){
rotate(v.begin(),v.begin()+n,v.end());
return v;
}
template<class T>
vc<T>iota(int n){
vc<T>v(n);rep(i,n)v[i]=i;
return v;
}
template<class T,int minfirst>
vc<int>argsort(const vc<T>&a){
auto idx=iota<int>(a.size());
sort(all(idx),[&](int i,int j){
return (minfirst?make_pair(a[i],i)<make_pair(a[j],j):make_pair(a[i],i)>make_pair(a[j],j));
});
return idx;
}
template<class T>
vvc<T>trans(const vvc<T>&a){
assert(a.size()&&a[0].size());
rep(i,a.size())assert(a[i].size()==a[0].size());
vvc<T>b(a[0].size(),vc<T>(a.size()));
rep(i,a.size())rep(j,a[0].size())b[j][i]=a[i][j];
return b;
}
vc<string>trans(const vc<string>&a){
assert(a.size()&&a[0].size());
rep(i,a.size())assert(a[i].size()==a[0].size());
vc<string>b(a[0].size(),string(a.size(),0));
rep(i,a.size())rep(j,a[0].size())b[j][i]=a[i][j];
return b;
}
template<class T>
int popcount(T n){
return __builtin_popcountll(n);
}
template<class T,class L=ll>
L sum(const vc<T>&a){
return accumulate(all(a),L(0));
}
template<class T>
struct subset_view{
T s;
struct iterator{
T s,x;
bool done;
T operator*()const{return x;}
iterator&operator++(){
if(x==0)done=true;
else x=(x-1)&s;
return*this;
}
bool operator!=(const iterator&r)const{return done!=r.done;}
};
iterator begin()const{return{s,s,false};}
iterator end()const{return{s,0,true};}
};
template<class T>
subset_view<T>subset(T s){
return{s};
}
template<class T>
T max(vc<T>&a){
return *max_element(all(a));
}
template<class T>
T min(vc<T>&a){
return *min_element(all(a));
}
template<class T,class L=ll>
vc<L> presum(vc<T> &a){
vc<L> ret(a.size()+1);
rep(i,a.size())ret[i+1]=ret[i]+a[i];
return ret;
}
template<class T, class F>
vc<T> &operator+=(vc<T> &a,F b){
for (auto&v:a)v += b;
return a;
}
template<class T, class F>
vc<T> &operator-=(vc<T>&a,F b){
for (auto&v:a)v-=b;
return a;
}
template<class T, class F>
vc<T> &operator*=(vc<T>&a,F b){
for (auto&v:a)v*=b;
return a;
}
template<class T=ll>
constexpr T pow(T a,T b){
T res=1;
while(b){
if(b&1)res*=a;
a*=a;
b/=2;
}
return res;
}
constexpr ll ten(ll a){
return pow<ll>(10,a);
}
template<typename T>constexpr T inf=numeric_limits<T>::max()/2-1;
template<class T>
int tbit(T x){
using U=make_unsigned_t<T>;
U y=(U)x;
return y?(int)bit_width(y)-1:-1;
}
template<class T>
int lbit(T x){
using U=make_unsigned_t<T>;
U y=(U)x;
return y?(int)countr_zero(y):-1;
}
template<class T>
int tbit(T x,int p){
using U=make_unsigned_t<T>;
constexpr int W=numeric_limits<U>::digits;
U y=(U)x;
if(p<0)return -1;
if(p>=W-1)return tbit(y);
return tbit(y&((U(1)<<(p+1))-1));
}
template<class T>
int lbit(T x,int p){
using U=make_unsigned_t<T>;
constexpr int W=numeric_limits<U>::digits;
U y=(U)x;
if(p<0)return lbit(y);
if(p>=W)return -1;
return lbit(y&(~U(0)<<p));
}
istream& operator>>(istream&is,i128&x){
string s;is>>s;
x=0;
int i=0,neg=0;
if(s[0]=='-')neg=1,i=1;
for(;i<(int)s.size();i++)x=x*10+s[i]-'0';
if(neg)x=-x;
return is;
}
ostream& operator<<(ostream&os,i128 x){
if(x==0)return os<<0;
if(x<0)os<<"-";
u128 y=x<0?-(u128)x:(u128)x;
string s;
while(y)s.pb('0'+y%10),y/=10;
reverse(all(s));
return os<<s;
}
#ifdef LOCAL
#include "debug/debug.hpp"
inline int dbging=1;
#else
#define dbg(...) ((void)0)
#endif
struct template_setup{
template_setup(){
#ifdef LOCAL
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
cin.tie(0)->sync_with_stdio(0);
#ifdef LOCAL
cout<<fixed<<setprecision(6);
#else
cout<<fixed<<setprecision(20);
#endif
}
};
inline template_setup template_setup;
#if defined(LOCAL)&&!defined(CP_NO_AUTO_ALL)&&!defined(CP_BUNDLED_SOURCE)
#include <all.hpp>
#endif
#endif
#endif
//ax+by=c
//res.fi.fi*t+res.fi.se, res.se.fi*t+res.se.se という解集合
#ifndef CP_BUNDLE_HEADER_43928B84ADA57EB6
#define CP_BUNDLE_HEADER_43928B84ADA57EB6
template<class T>
struct range_set{
T covered;
using P=pair<T,T>;
using It=typename set<P>::iterator;
set<P>st;
range_set():covered(0){}
//[l,r) を insert
void insert(T l,T r){
assert(l<=r);
if(l==r)return;
erase(l,r);
{
auto it=st.lower_bound({l,-inf<T>});
if(it!=st.begin()&&prev(it)->second==l){
l=prev(it)->first;
erase(prev(it)->first,prev(it)->second);
}
}
{
auto it=st.lower_bound({r,-inf<T>});
if(it!=st.end()&&it->first==r){
r=it->second;
erase(it->first,it->second);
}
}
st.insert({l,r});covered+=r-l;
}
//[l,r) に含まれる区間を削除
//はみ出るやつは残しておく
void erase(T l,T r){
assert(l<=r);
if(l==r)return;
rep(_,1){
auto it=st.upper_bound({l,-inf<T>});
if(it==st.begin())break;
it=prev(it);
if(it->second<=l)break;
covered-=min<T>(r,it->second)-l;
T tif=it->first,tis=it->second;
st.erase(it);
if(tif!=l)st.insert({tif,l});
if(tis>r)st.insert({r,tis});
}
while(1){
auto it=st.lower_bound({l,-inf<T>});
if(it==st.end()||it->first>=r)break;
if(it->second>=r){
covered-=r-it->first;
T tis=it->second;st.erase(it);
if(tis!=r)st.insert({r,tis});
break;
}
covered-=it->second-it->first;
st.erase(it);
}
}
//x を含む区間
It find(T x){
auto it=st.upper_bound({x,inf<T>});
if(it==st.begin())return st.end();
if(prev(it)->second<=x)return st.end();
return prev(it);
}
//x が含まれるか
bool contains(T x){
return find(x)!=st.end();
}
//[l,r) が全て含まれるか
bool contains(T l,T r){
assert(l<=r);
if(l==r)return true;
auto it=find(l);
return it!=st.end()&&r<=it->second;
}
//x を含む区間、なければ x より後の最初の区間
It lower_bound(T x){
auto it=st.lower_bound({x,-inf<T>});
if(it!=st.begin()&&prev(it)->second>x)return prev(it);
return it;
}
//[l,r) と交わる部分を列挙
vc<P>enumerate(T l,T r){
assert(l<=r);
vc<P>res;
for(auto it=lower_bound(l);it!=st.end()&&it->first<r;it++){
T a=max(l,it->first),b=min(r,it->second);
if(a<b)res.pb({a,b});
}
return res;
}
void clear(){
st.clear();covered=0;
}
void debug(){
int dbging=1;
dbg("==========="s);
for(auto&[l,r]:st){
dbg(l,r);
}
dbg("==========="s);
}
};
// 区間ごとに monoid の値を持つ range set
template<class T,class info_type>
struct range_set_info{
using value_type=typename info_type::value_type;
struct Node{
T l,r;
value_type val;
};
struct cmp{
bool operator()(const Node&a,const Node&b)const{
return a.l<b.l;
}
};
using It=typename set<Node,cmp>::iterator;
using CIt=typename set<Node,cmp>::const_iterator;
set<Node,cmp>st;
It lower_bound(T x){
auto it=st.lower_bound({x,x,info_type::e()});
if(it!=st.begin()&&prev(it)->r>x)return prev(it);
return it;
}
CIt lower_bound(T x)const{
auto it=st.lower_bound({x,x,info_type::e()});
if(it!=st.begin()&&prev(it)->r>x)return prev(it);
return it;
}
void add(vector<Node>&v,T l,T r,const value_type&val){
if(l<r&&val!=info_type::e())v.pb({l,r,val});
}
//[l,r) に info を作用させる
void apply(T l,T r,const value_type&info){
assert(l<=r);
if(l==r||info==info_type::e())return;
vector<Node>v;
auto it=lower_bound(l);
T cur=l;
while(it!=st.end()&&it->l<r){
Node x=*it;
it=st.erase(it);
if(cur<x.l)add(v,cur,min(r,x.l),info_type::op(info_type::e(),info));
T a=max(l,x.l),b=min(r,x.r);
add(v,x.l,a,x.val);
add(v,a,b,info_type::op(x.val,info));
add(v,b,x.r,x.val);
cur=max(cur,b);
}
if(cur<r)add(v,cur,r,info_type::op(info_type::e(),info));
for(auto&x:v)st.insert(x);
}
//[l,r) の情報を極大な区間の集合として返す
vector<tuple<T,T,value_type>> get(T l,T r)const{
assert(l<=r);
vector<tuple<T,T,value_type>>res;
if(l==r)return res;
auto add=[&](T a,T b,const value_type&val){
if(a==b)return;
if(!res.empty()&&std::get<2>(res.back())==val)std::get<1>(res.back())=b;
else res.emplace_back(a,b,val);
};
T cur=l;
for(auto it=lower_bound(l);it!=st.end()&&it->l<r;it++){
if(cur<it->l)add(cur,min(r,it->l),info_type::e());
T a=max(cur,it->l),b=min(r,it->r);
add(a,b,it->val);
cur=max(cur,b);
}
add(cur,r,info_type::e());
return res;
}
void clear(){st.clear();}
};
#endif
#ifndef CP_BUNDLE_HEADER_084E7F455B05C243
#define CP_BUNDLE_HEADER_084E7F455B05C243
#ifndef CP_BUNDLE_HEADER_AD5407378ADD496E
#define CP_BUNDLE_HEADER_AD5407378ADD496E
#ifndef CP_BUNDLE_HEADER_479ABBBFBD96A77B
#define CP_BUNDLE_HEADER_479ABBBFBD96A77B
struct barrett{
using u64=uint64_t;
using u32=uint32_t;
using i128=__int128_t;
u64 m;
int mod;
void set(int mod_){
assert(mod_>0);
mod=mod_;
m=(i128(1)<<64)/mod;
}
unsigned reduce(uint64_t x){
assert(mod>0);
x-=(((i128)x*m)>>64)*mod;
return x<mod?x:x-mod;
}
};
#endif
template<int id>
struct dynamic_modint{
using u32=uint32_t;
using u64=uint64_t;
u32 val;
dynamic_modint():val(0){}
dynamic_modint(ll x){
ll v=x%get_mod();
if(v<0)v+=get_mod();
val=v;
}
static dynamic_modint raw(int v){
assert(v>=0);
dynamic_modint mi;
mi.val=v;
return mi;
}
dynamic_modint &operator+=(const dynamic_modint&m){
u32 md=get_mod();
u32 x=val+m.val-md;
val=x+(md&-(x>>31));
return *this;
}
dynamic_modint &operator-=(const dynamic_modint&m){
u32 md=get_mod();
u32 x=val-m.val;
val=x+(md&-(x>>31));
return *this;
}
dynamic_modint &operator*=(const dynamic_modint&m){
val=rem(u64(val)*m.val);
return *this;
}
dynamic_modint &operator/=(const dynamic_modint&m){
val=rem(u64(val)*m.inv().val);
return *this;
}
dynamic_modint operator-() const{
return dynamic_modint(val?get_mod()-val:0);
}
dynamic_modint operator+() const {
return *this;
}
friend dynamic_modint operator+(dynamic_modint lhs, const dynamic_modint& rhs){
return lhs+=rhs;
}
friend dynamic_modint operator-(dynamic_modint lhs, const dynamic_modint& rhs){
return lhs-=rhs;
}
friend dynamic_modint operator*(dynamic_modint lhs, const dynamic_modint& rhs){
return lhs*=rhs;
}
friend dynamic_modint operator/(dynamic_modint lhs,const dynamic_modint&rhs){
return lhs/=rhs;
}
bool operator==(const dynamic_modint&p) const{
return p.val==val;
}
bool operator!=(const dynamic_modint&p) const{
return p.val!=val;
}
dynamic_modint pow(int64_t n) const{
dynamic_modint res(1),mul(val);
while(n){
if(n%2)res*=mul;
mul*=mul;
n/=2;
}
return res;
}
friend ostream&operator<<(ostream&os,const dynamic_modint&p){
os<<p.val;
return os;
}
friend istream&operator>>(istream&is,dynamic_modint&p){
int64_t x;
is>>x;
p=dynamic_modint(x);
return is;
}
dynamic_modint inv()const{
int64_t a=val,b=get_mod(),u=1,v=0,t;
#ifdef LOCAL
assert(gcd(a,b)==1);
#endif
while(b>0){
t=a/b;
swap(a-=t*b,b);
swap(u-=t*v,v);
}
return dynamic_modint(u);
}
inline static u32 rem(u64 x){return barrett_reduction().reduce(x);}
static inline int &get_mod(){
static int mod=0;
return mod;
}
static void set_mod(int md){
assert(0<md&&md<=(1ll<<31)-1);
get_mod()=md;
barrett_reduction().set(md);
}
static inline barrett&barrett_reduction(){
static barrett b;
return b;
}
};
#endif
template< typename T >
T extgcd(T a, T b, T &x, T &y) {
T d = a;
if(b != 0) {
d = extgcd(b, a % b, y, x);
y -= (a / b) * x;
} else {
x = 1;
y = 0;
}
return d;
}
template<class T>
pair<T,T> inv(T x,T m){
T a1,a2;
T res=extgcd<ll>(x,m,a1,a2);
T md=m/res;
a1=(a1%md+md)%md;
return {a1,md};
}
template<class T>
pair<T,T> mod_solve(T a,T b,T m){//return x s.t. ax=b mod m
a%=m,b%=m;if(a<0)a+=m;if(b<0)b+=m;
T g=gcd(gcd(a,b),m);
a/=g,b/=g,m/=g;
if(gcd(a,m)>1)return {-1,-1};
return {(inv<ll>(a,m).first*b)%m,inv<ll>(a,m).second};
}
//x^2 ≡ b (mod p)
ll mod_sqrt(ll b,ll p){
static mt19937 mt(random_device{}());
b%=p;if(b<0)b+=p;
if(b==0)return 0;
if(p==2){
return b;
}
assert(p>=3);
using mint=dynamic_modint<20260801>;mint::set_mod(p);
if(mint(b).pow((p-1)/2)==-1){
return -1;
}
if(p%4==3){
return mint(b).pow((p+1)/4).val;
}
ll t=[&](){
ll w;
while(1){
ll t=mt()%p;
w=t*t-b;
if(mint(w).pow((p-1)/2)==mint(-1)){
return t;
}
}
assert(0);
}();
mint w=mint(t*t-b);
using T=pair<mint,mint>;
auto ml=[&](T a,T b)->T{
return T{a.first*b.first+a.second*b.second*w,a.second*b.first+a.first*b.second};
};
ll e=(p+1)/2;
T ans={1,0};
T gy={t,1};
while(e){
if(e%2)ans=ml(ans,gy);
gy=ml(gy,gy);
e/=2;
}
return ans.first.val;
}
#endif
template<class T>
pair<pair<T,T>,pair<T,T>>extgcdasexp(T a,T b,T c){
T g=gcd(a,b);
assert(a||b);
if(c%g)return {{-inf<T>,-inf<T>},{-inf<T>,-inf<T>}};
c/=g,a/=g,b/=g;
T x,y;
extgcd(a,b,x,y);
x*=c,y*=c;
return {{-b,x},{a,y}};
}
//at+b \in [l,r] なる t の閉区間
template<class T>
pair<T,T>validsegment(T a,T b,T l,T r){
if(a==0){
if(l<=b&&b<=r)return {-inf<T>,inf<T>};
return {0,-1};
}
if(a>0){
return {ceil<T>(l-b,a),floor<T>(r-b,a)};
}else{
return {ceil<T>(r-b,a),floor<T>(l-b,a)};
}
}
void solve(){
LL(n);
range_set<ll>ans;
ll in=isqrt(n);
for(ll x=1;x<=in;x++){
auto[ab,b]=extgcdasexp<ll>(x,1,n);
ll L=-inf<ll>,R=inf<ll>;
//b>=1
auto seg=validsegment<ll>(b.fi,b.se,1,2e10);
chmax(L,seg.fi),chmin(R,seg.se);
dbg(x,L,R);
dbg(ab,b);
//a>=1
ll ac=ab.fi-b.fi;
ll db=b.se-ab.se;
dbg(ac,db);
if(ac>0){
chmax(L,ceil<ll>(db+1,ac));
}else{
chmin(R,floor<ll>(db+1,ac));
}
if(L>R)continue;
dbg(L,R);
ll sl=ab.fi*L+ab.se;
ll sr=ab.fi*R+ab.se;
if(sl>sr)swap(sl,sr);
dbg(x,sl,sr+1);
ans.insert(sl,sr+1);
}
for(ll ab=1;ab<=in;ab++){
auto [x,b]=extgcdasexp<ll>(ab,1,n);
ll L=-inf<ll>,R=inf<ll>;
//x>=1
auto seg=validsegment<ll>(x.fi,x.se,1,2e10);
chmax(L,seg.fi),chmin(R,seg.se);
//1<=b<k
auto seg2=validsegment<ll>(b.fi,b.se,1,ab-1);
chmax(L,seg2.fi),chmin(R,seg2.se);
if(L<=R){
ans.insert(ab,ab+1);
}
}
PRT(ans.covered);
}
signed main(){
int t=1;
// cin >> t;
while(t--)solve();
}
ryoku