反复迭代(iterate)
题面
定义长度为 的序列 的线段树为一个序列,由如下方式生成:
- 令大于等于 的最小的 的整次幂为 。在 前面添加 个 。
- 定义长度为 的序列 ,如下递归定义:
- 就是 的线段树。
现给定一个正整数序列 和自然数 ,执行 次以下操作:
- 将 替换为 的线段树。
求操作后序列的和,对 取模。
。
解法
先把第一次补完,然后定义一次操作是把正常定义的操作的两步反过来,显然答案不变。
手玩一下可以发现数列里面的值不外乎两种:
- 线段树上的值。
显然,假如一开始不用补,则 的个数为迭代的次数。
对于一个线段树上的值,显然它要么在上一次迭代出现,要么由它的两个儿子生成。手推一下发现它出现的次数是组合数。
有一个例外是根节点。我们计算出所有数的个数,拿总共要有的个数减去它就得到了根节点的个数。于是这道题就做完了。
复杂度 。
#include <bits/stdc++.h>
//#define CinCout
#define rep(i,n) for(int i=0,_##i##__end=(n);i<_##i##__end;++i)
#define per(i,n) for(int i=(n)-1;i>=0;--i)
#define rep1(i,n) for(int i=1,_##i##__end=(n);i<=_##i##__end;++i)
#define per1(i,n) for(int i=(n);i>=1;--i)
#define pb push_back
#define mp make_pair
#define debugv(x) cout<<#x<<'='<<x<<",line "<<__LINE__-1<<endl
#define debug cout<<"at line "<<__LINE__-1<<" in function "<<__FUNCTION__<<endl
#define nextp next_permutation
#define pq priority_queue
#define fi first
#define se second
#ifdef CinCout
#define Yes cout<<"Yes"<<endl
#define No cout<<"No"<<endl
#define YES cout<<"YES"<<endl
#define NO cout<<"NO"<<endl
#else
#define Yes puts("Yes")
#define No puts("No")
#define YES puts("YES")
#define NO puts("NO")
#endif
typedef long long ll;
using namespace std;
const ll mod1=998244353;
//ios_base::sync_with_stdio(false);???cin
unsigned time_related_rand()
{
return ll(chrono::high_resolution_clock::now().time_since_epoch().count());
}
int n,k;
int a[444444];
ll binom[222];
int l;
ll qkpw(ll x,ll y)
{
ll r=1;while(y)
{
if(y&1) r=r*x%mod1;x=x*x%mod1;y>>=1;
}
return r;
}
struct segtree{
int v[1888888];
ll c[1888888];
ll s[1888888];
ll build(int i,int l,int r,int *a,int dep)
{
if(i==1)
{
int m=(l+r)>>1;
ll u=build(i<<1,l,m,a,dep-1)+build(i<<1|1,m+1,r,a,dep-1);
v[i]=max(v[i<<1],v[i<<1|1]);
s[i]=(n*qkpw(2,k)-(k)+mod1*3)%mod1;
c[i]=(s[i]+mod1*3-s[i<<1]-s[i<<1|1])%mod1;
return (v[i]*c[i]+u)%mod1;
}
else if(l==r)
{
v[i]=a[l];
s[i]=c[i]=binom[dep];
return v[i]*c[i]%mod1;
}
else
{
int m=(l+r)>>1;
ll u=build(i<<1,l,m,a,dep-1)+build(i<<1|1,m+1,r,a,dep-1);
v[i]=max(v[i<<1],v[i<<1|1]);
c[i]=binom[dep];
s[i]=(c[i]+s[i<<1]+s[i<<1|1])%mod1;
return (v[i]*c[i]+u)%mod1;
}
return 0;
}
};
segtree seg;
ll inve(ll ff)
{
return qkpw(ff,mod1-2);
}
int main(int argc,char*argv[])
{
ios_base::sync_with_stdio(false);
cin>>n;
if(n==1)
{
cin>>k;cout<<k%998244353<<endl;return 0;
}
int v=1;
while(v<n)
{
++l;v<<=1;
}
for(int i=v-n+1;i<=v;++i)
{
cin>>a[i];a[i]%=mod1;
}
cin>>k;n=v;
binom[0]=1;
for(int i=0;i<=44;++i) binom[i+1]=binom[i]*(k+i)%mod1*inve(i+1)%mod1;
cout<<seg.build(1,1,n,a,l)<<endl;
return 0;
}幂次方程(equation)
题面
给定一个正整数 ,试构造正整数 使得 。
多测。你需要保证 。
。
题解
注意到如果 很大则必须使 。不妨设 ,其中 互质。
也就是说, 是 的倍数。不妨以此分类讨论。
如果 是奇数,取 ,则有 ,得 。
如果 是模 余 ,取 ,则有 ,得 。
如果 是 的倍数,取 ,则有 ,取 。
注意特判 的时候以上解法会得到 。
#include <bits/stdc++.h>
//#define CinCout
#define rep(i,n) for(int i=0,_##i##__end=(n);i<_##i##__end;++i)
#define per(i,n) for(int i=(n)-1;i>=0;--i)
#define rep1(i,n) for(int i=1,_##i##__end=(n);i<=_##i##__end;++i)
#define per1(i,n) for(int i=(n);i>=1;--i)
#define pb push_back
#define mp make_pair
#define debugv(x) cout<<#x<<'='<<x<<",line "<<__LINE__-1<<endl
#define debug cout<<"at line "<<__LINE__-1<<" in function "<<__FUNCTION__<<endl
#define nextp next_permutation
#define pq priority_queue
#define fi first
#define se second
#ifdef CinCout
#define Yes cout<<"Yes"<<endl
#define No cout<<"No"<<endl
#define YES cout<<"YES"<<endl
#define NO cout<<"NO"<<endl
#else
#define Yes puts("Yes")
#define No puts("No")
#define YES puts("YES")
#define NO puts("NO")
#endif
typedef long long ll;
using namespace std;
const ll mod1=998244353;
const ll mod2=1000000007;
//ios_base::sync_with_stdio(false);???cin
unsigned time_related_rand()
{
return ll(chrono::high_resolution_clock::now().time_since_epoch().count());
}
int t;
ll a;
void solve_test()
{
cin>>a;
if(a==2)
{
cout<<"4 2 4\n";
}
else if(a%2==1)
{
cout<<4<<" "<<8*a-2<<" "<<2*a<<"\n";
}
else if(a%4==2)
{
cout<<2<<" "<<a<<" "<<a-2<<"\n";
}
else
{
cout<<1<<" "<<2*a-2<<" "<<a/2<<"\n";
}
}
int main(int argc, char*argv[])
{
ios_base::sync_with_stdio(false);
cin>>t;while(t--)solve_test();
return 0;
}二重求和(summation)
题面
定义
其中 定义为 向下取整。
给定 ,求 的异或和。
或 ,。
题解
令 表示 的因子个数。有
证明:考虑
当 时会发生什么。
那么线性筛 两次前缀和即可。考虑怎么单点求。
数论分块计算即可。
#include <bits/stdc++.h>
//#define CinCout
#define rep(i,n) for(int i=0,_##i##__end=(n);i<_##i##__end;++i)
#define per(i,n) for(int i=(n)-1;i>=0;--i)
#define rep1(i,n) for(int i=1,_##i##__end=(n);i<=_##i##__end;++i)
#define per1(i,n) for(int i=(n);i>=1;--i)
#define pb push_back
#define mp make_pair
#define debugv(x) cout<<#x<<'='<<x<<",line "<<__LINE__-1<<endl
#define debug cout<<"at line "<<__LINE__-1<<" in function "<<__FUNCTION__<<endl
#define nextp next_permutation
#define pq priority_queue
#define fi first
#define se second
#ifdef CinCout
#define Yes cout<<"Yes"<<endl
#define No cout<<"No"<<endl
#define YES cout<<"YES"<<endl
#define NO cout<<"NO"<<endl
#else
#define Yes puts("Yes")
#define No puts("No")
#define YES puts("YES")
#define NO puts("NO")
#endif
typedef long long ll;
using namespace std;
const ll mod1=998244353;
const ll mod2=1000000007;
//ios_base::sync_with_stdio(false);???cin
unsigned time_related_rand()
{
return ll(chrono::high_resolution_clock::now().time_since_epoch().count());
}
const int M=30000007;
const int N=30000000;
int d[M],lw[M];
bool np[M];int cnt;int p[3141592];
ll l,r,pp;
void sieve()
{
np[1]=1;d[1]=1;lw[1]=1;cnt=0;
for(int i=2;i<=N;++i)
{
if(!np[i])
{
d[i]=2;lw[i]=1;p[cnt++]=i;
}
for(int j=0;j<cnt;++j)
{
int rr=p[j]*i;if(rr>N) break;
np[rr]=1;
if(i%p[j])
{
d[rr]=d[p[j]]*d[i];lw[rr]=i;
}
else
{
d[rr]=d[i]+d[lw[rr]=lw[i]];break;
}
}
}
rep1(i,N) d[i]%=pp;
rep1(i,N)
{
d[i]+=d[i-1];if(d[i]>=pp)d[i]-=pp;
}
rep1(i,N)
{
d[i]+=d[i-1];if(d[i]>=pp)d[i]-=pp;
}
}
void solve_5e7()
{
sieve();
int s=0;for(int i=l;i<=r;++i)s^=d[i];
cout<<s<<endl;
}
void solve_1e15()
{
ll n=l,u,f,g;
ll L=1,R;
bool fl=0;
ll s1=0,s2=0;
while(L<=n)
{
ll k=n/L;u=k%pp;
R=n/k;u=u*(u+1)/2%pp;
if(fl||R>=pp)
{
f=R%pp;g=L%pp;fl=1;
}
else
{
f=R;g=L;
}
s1+=(R-L+1)*k;
s2=(s2+(-f*(f+1)+g*(g-1))/2%pp*u)%pp;
L=R+1;
}
s1%=pp;s1*=(n+1)%pp;s1%=pp;
if(s2<0)s2+=pp;
cout<<(s1+s2)%pp<<endl;
}
int main(int argc,char *argv[])
{
cin>>l>>r>>pp;
if(r==l) solve_1e15();
else solve_5e7();
return 0;
}反子数组(subarray)
题面
定义一个序列的反子数组为其去掉一个连续子数组后留下的序列。比如, 的反子数组有 ,,,, 等,但没有 ,, 等。特别地,一个序列的前缀或后缀都是其反子数组(包括空数组和原数组)。显然地,一个序列的反子数组一定是其子序列。
给定长度为 、值域为 的序列 , 次询问,询问 有多少个本质不同的反子数组。两个序列是本质不同的,当且仅当它们长度不同,或它们有一个同样位置的值不同。
。
题解
引理:一个长 的序列若有 对相等的字符,则其有 个本质不同的反子数组。
证明:考虑同一个长度的反子数组不断变一个的过程。
于是这道题转化成了一道很经典的题。莫队或者这个做法均可。
#include <bits/stdc++.h>
//#define CinCout
#define rep(i,n) for(int i=0,_##i##__end=(n);i<_##i##__end;++i)
#define per(i,n) for(int i=(n)-1;i>=0;--i)
#define rep1(i,n) for(int i=1,_##i##__end=(n);i<=_##i##__end;++i)
#define per1(i,n) for(int i=(n);i>=1;--i)
#define pb push_back
#define mp make_pair
#define debugv(x) cout<<#x<<'='<<x<<",line "<<__LINE__-1<<endl
#define debug cout<<"at line "<<__LINE__-1<<" in function "<<__FUNCTION__<<endl
#define nextp next_permutation
#define pq priority_queue
#define fi first
#define se second
#ifdef CinCout
#define Yes cout<<"Yes"<<endl
#define No cout<<"No"<<endl
#define YES cout<<"YES"<<endl
#define NO cout<<"NO"<<endl
#else
#define Yes puts("Yes")
#define No puts("No")
#define YES puts("YES")
#define NO puts("NO")
#endif
typedef long long ll;
using namespace std;
const ll mod1=998244353;
const ll mod2=1000000007;
//ios_base::sync_with_stdio(false);???cin
unsigned time_related_rand()
{
return ll(chrono::high_resolution_clock::now().time_since_epoch().count());
}
const int N=400;
int n,m,k;
int v[100005];
ll a[100005];
int l[100005],r[100005];
vector<int> ids[100005];
vector<int> pos[100005];
bool fl[100005];
vector<int> special;
vector<int> sp_sum[100005];
struct fenwick{
int a[100005];
int s[N];
void add(int id)
{
++a[id];
++s[(id-1)/N];
}
int sum(int id)
{
int v=0;
rep(j,id/N)
{
v+=s[j];
}
rep(j,id%N)
{
v+=a[id-j];
}
return v;
}
};
fenwick tree;
int main(int argc, char *argv[])
{
ios_base::sync_with_stdio(false);
cin>>n>>k;
rep1(i,n)
{
cin>>v[i];pos[v[i]].pb(i);
}
rep1(i,k)
{
if(pos[i].size()>=N)
{
fl[i]=1;special.pb(i);
}
else fl[i]=0;
}
rep(i,special.size()) sp_sum[0].pb(0);
rep1(i,n)
{
rep(j,special.size())
{
sp_sum[i].pb(sp_sum[i-1][j]+(v[i]==special[j]));
}
}
cin>>m;
rep1(i,m)
{
cin>>l[i]>>r[i];assert(l[i]<=r[i]);ids[r[i]].pb(i);
}
rep1(i,n)
{
if(!fl[v[i]])
{
rep(j,pos[v[i]].size())
{
int x=pos[v[i]][j];
if(x<i)
{
tree.add(x);
}
}
}
ll t=tree.sum(i);
rep(j,ids[i].size())
{
int x=ids[i][j];
int L=l[x];
a[x]=t-tree.sum(L-1);
rep(k,special.size())
{
ll t=sp_sum[i][k]-sp_sum[L-1][k];
a[x]+=t*(t-1)/2;
}
a[x]=(i-L+1ll)*(i-L+2)/2+1-a[x];
}
}
rep1(i,m) cout<<a[i]<<endl;
return 0;
}