zoukankan      html  css  js  c++  java
  • 【纪中集训2019.3.29】整除分块

    题目

    描述

    ​ 本题的背景是整除分块;

    ​ 定义一个数列$a_{n,i} = lfloor frac{n}{i} floor $ ;

    ​ 求$sum_{i=l}^{r} mex(a_n) $ ;

    ​ 其中(mex)表示序列中最小的没有出现过的自然数;

    ​ 答案对(998244353)取模 ;

    范围

    (1 le T le 65536 , 1 le l ,r le 10^{36})

    ​ 评测系统支持使用 $ _ _ int218 $ ,但是不能直接读入输出,需要你手写 $ IO $ ;

    题解

    • Part 1

    • 如果你写过整除分块,根据整除分块的写法基础,(i)(a_n)中不出现的充要条件是:

      [egin{align} lfloor frac{n}{lfloor frac{n}{i} floor} floor eq i \ lfloor frac{n}{lfloor frac{n}{i} floor} floor ge i+1 \ frac{n}{lfloor frac{n}{i} floor} ge i +1 \ frac{n}{i+1} ge lfloor frac{n}{i} floor \ \ 令: lfloor frac{n}{i} floor = a \ 结合上面的推导,根据定义有:\ \ a(i+1) le n lt i(a+1) \ 且在 0 le a lt i 时有意义; end{align} ]

    • 意思是我们得到了关于不出现(i)的关于(n)的区间,记这样子的区间为(S(i,a))

    • Part 2

    • 观察式子的左右两边,可以得到(S(i+1,a-1))(S(i,a))可以拼接,推广得在同一副对角线上的区间连续:

    • 即这样的形式:

    • 记以(S(i,0))开头的对角线标号为(i),按照标号,从小到大考虑每一条跳条对角线;

    • 注意到(|S(i,a)| = i(a+1) - a(i+1) = i - a)

    • 所以有:第(i)条线不会影响第(1 o i-1)条线已经覆盖的区间的答案;

    • 发现多出来的区间长度依次是:$1 1 2 2 3 3 cdots i i cdots $ ;

    • 容易发现每一次的段都是从后往前看都是一个连续增大的数列(相邻差最大为1);

    • 对于第(2i)个多出来的区间,从后往前第一小段的长度为2,值为(i),以后值+1,长度+2,直到总长度为(i)

    • 对于第(2i-1)个多出来的区间,从后往前第一小段的长度为1,值为(i),以后值+1,长度+2,直到总长度为(i)

    • Part 3

    • 可以通过打表直接到(Part 3)

    • 分奇数段和偶数段求和,最后再删去剩下的一小段:

    • 对于奇数段:

      [egin{align} &= sum_{i=1}^{l}sum_{j=1}^{i} i + sqrt{j-1} = sum_{i=1}^{l} i^2 +sum_{i=1}^{l} sum_{j=1}^{i} sqrt{j-1} \ o &sum_{i=1}^{l} sum_{j=1}^{i} sqrt{j-1}= sum_{j=1}^{l}sqrt{j-1}(l-j+1)\ &=sum_{j=1}^{l}(l-j+1) sum_{k=1}[k^2<j]= sum_{k=1}^{k^2<l}sum_{j=1}^{l-k^2}j\ &直接运用等差数列求和再预处理次方和即可求得答案; end{align} ]

    • 对于偶数段,可以不用求出(k^2+k)的逆,可以在奇数段的推导上直接得出:

      [egin{align} &sum_{k=1}^{k^2+k<l}sum_{j=1}^{l-k^2-k}j end{align} ]

      //__int128真不是好东西,不仅不能直接读入开根还要炸精度!!! 
      #include<bits/stdc++.h>
      #define mod 998244353
      #define eps 1e-9
      #define ll __int128
      #define ld long double
      #define il inline 
      using namespace std;
      ll n,iv2,iv4,iv6,iv30;
      il char gc(){
      	static char*p1,*p2,s[1000000];
      	if(p1==p2)p2=(p1=s)+fread(s,1,1000000,stdin);
      	return(p1==p2)?EOF:*p1++;
      }
      il ll rd(){
      	ll x=0;char c=gc();
      	while(c<'0'||c>'9')c=gc();
      	while(c>='0'&&c<='9')x=x*10+c-'0',c=gc();
      	return x;
      }
      char ps[1000000],*pp=ps;
      il void push(char x){
      	if(pp==ps+1000000)fwrite(ps,1,1000000,stdout),pp=ps;
      	*pp++=x;
      }
      il void print(ll x){
      	static int sta[100],top;
      	if(!x){push('0'),push('
      ');return;}
      	while(x)sta[++top]=x%10,x/=10;
      	while(top)push(sta[top--]^'0');
      	push('
      ');
      }
      il void flush(){fwrite(ps,1,pp-ps,stdout);}
      il ll pw(ll x,ll y){
      	ll re=1;
      	while(y){
      		if(y&1)re=re*x%mod;
      		y>>=1;x=x*x%mod;
      	}
      	return re;
      }
      il void inc(ll&x,ll y){x+=y;if(x>=mod)x-=mod;}
      il void dec(ll&x,ll y){x-=y;if(x<0)x+=mod;}
      il ll get1(ll n){
      	//return floor(sqrt((ld)1.0*n)+eps);
      	ll re=floor(sqrt((ld)1.0*n)+eps);
      	while(re*re>=n)re--;
      	return re;
      }
      il ll get2(ll n){
      	//return floor((sqrt((ld)4.0*n-3)-1)/2+eps);
      	ll re=floor((sqrt((ld)4.0*n-3)-1)/2+eps);
      	while(re*re+re>=n)re--;
      	return re;
      }
      il ll cal1(ll n){n%=mod;return n*(n+1)%mod*iv2%mod;}
      il ll cal2(ll n){n%=mod;return n*(n+1)%mod*(2*n+1)%mod*iv6%mod;}
      il ll cal3(ll n){n%=mod;return n*(n+1)%mod*n%mod*(n+1)%mod*iv4%mod;}
      il ll cal4(ll n){n%=mod;return n*(n+1)%mod*(2*n+1)%mod*(3ll*n*n%mod+3*n%mod+mod-1)%mod*iv30%mod;}
      il ll solve1(ll l){
      	ll n=get1(l),re=0;
      	inc(re,(n%mod)*(l%mod)%mod*(l%mod+1)%mod);
      	dec(re,(2*(l%mod)+1)*cal2(n)%mod);
      	inc(re,cal4(n));
      	re=(re*iv2%mod+cal2(l)-1)%mod; 
      	return re;
      }
      il ll solve2(ll l){
      	ll n=get2(l),re=0;
      	inc(re,(n%mod)*(l%mod)%mod*(l%mod+1)%mod);
      	dec(re,(2*(l%mod)+1)*cal1(n)%mod);
      	dec(re,2*l*cal2(n)%mod);
      	inc(re,2*cal3(n)%mod);
      	inc(re,cal4(n)%mod);
      	re=((re*iv2%mod+cal2(l))%mod+cal1(l))%mod;
      	return re;
      }
      il ll solve3(ll l,ll n){
      	ll m=get1(n),re=(n%mod)*(l%mod)%mod;
      	inc(re,(m%mod)*(n%mod)%mod);
      	dec(re,cal2(m));
      	return re;
      }
      il ll solve4(ll l,ll n){
      	ll m=get2(n),re=(n%mod)*((l%mod)+1)%mod;
      	inc(re,(m%mod)*(n%mod)%mod);
      	dec(re,cal2(m));
      	dec(re,cal1(m));
      	return re;
      }
      il ll solve(ll n){
      	ll l=get2(n+1)+1,m=l*(l+1)-1,re=0;
      	inc(re,solve1(l));
      	inc(re,solve2(l));
      	if(m>n)dec(re,solve4(l,min(l,m-n)));
      	if(m-l>n)dec(re,solve3(l,min(l,m-n-l)));
      	return re;
      }
      int main(){
      	freopen("mex.in","r",stdin);
      	freopen("mex.out","w",stdout);
      	iv2=pw(2,mod-2);iv4=pw(4,mod-2);
      	iv6=pw(6,mod-2);iv30=pw(30,mod-2);
      	ll C=rd(),T=rd();
      	while(T--){
      		ll l=rd(),r=rd();
      		ll ans=(solve(r)-solve(l-1)+mod)%mod;
      		print(ans);
      	}
      	flush();
      	return 0;
      }
      
  • 相关阅读:
    eclipse编码格式设置教程、如何为eclipse设置编码格式?
    Eclipse中使用SVN
    个人mysql配置命令
    MySQL新建用户,授权,删除用户,修改密码等命令
    MySQL修改root密码的多种方法
    MySQL 5.6 for Windows 解压缩版配置安装
    在windows下安装mysql5.6.24版本
    CS231n assignment2 Q3 Dropout
    CS231n assignment2 Q1 Fully-connected Neural Network
    CS231n assignment2 Q2 Batch Normalization
  • 原文地址:https://www.cnblogs.com/Paul-Guderian/p/10637349.html
Copyright © 2011-2022 走看看