zoukankan      html  css  js  c++  java
  • LOJ

    ( ext{Solution})

    就挺奇妙的。

    我们发现,当 (ile sqrt n) 时是一个多重背包问题,当 (i>sqrt n) 时因为有 (i^2>n),其实是一个完全背包问题。

    那就分类做吧:

    • (ile sqrt n)

      先写出方程,

      [f[j]=sum f[j-k imes i] ]

      这个式子不带与 (j,k) 相关的系数,我们可以考虑直接用前缀和优化,这个可以优化到 (mathcal O(n imes sqrt n))

    • (i>sqrt n)

      继续挖掘题目的性质,我们发现题目的物品还有一个和普通完全背包不一样的地方:物品体积是连续的。这样就保证了在 ([sqrt n+1,n]) 的物品都是随便取的。

      就有一个非常神奇的式子:

      [f[i][j]=f[i-1][j-(sqrt n+1)]+f[i][j-i] ]

      定义 (f[i][j]) 为选了 (i) 个物品(不是种类),总和为 (j) 的方案总数。

      转移分别是加一个体积为 (sqrt n+1) 的物品,(i) 个物品每个都 (+1)

      为什么这样是正确的?首先我们保证了这些物品在 (n) 以内(废话背包容量就是 (n),因为加入时间的不同肯定是能覆盖所有情况的。

      时间复杂度 (mathcal O(n imes sqrt n))

    最后把两种情况合并就行了,注意 (ans) 赋初值为 (f[n])(当然只是我这种写法)。

    ( ext{Code})

    #include <cstdio>
    
    #define rep(i,_l,_r) for(register signed i=(_l),_end=(_r);i<=_end;++i)
    #define fep(i,_l,_r) for(register signed i=(_l),_end=(_r);i>=_end;--i)
    #define erep(i,u) for(signed i=head[u],v=to[i];i;i=nxt[i],v=to[i])
    #define efep(i,u) for(signed i=Head[u],v=to[i];i;i=nxt[i],v=to[i])
    #define print(x,y) write(x),putchar(y)
    
    template <class T> inline T read(const T sample) {
        T x=0; int f=1; char s;
        while((s=getchar())>'9'||s<'0') if(s=='-') f=-1;
        while(s>='0'&&s<='9') x=(x<<1)+(x<<3)+(s^48),s=getchar();
        return x*f;
    }
    template <class T> inline void write(const T x) {
        if(x<0) return (void) (putchar('-'),write(-x));
        if(x>9) write(x/10);
        putchar(x%10^48);
    }
    template <class T> inline T Max(const T x,const T y) {if(x>y) return x; return y;}
    template <class T> inline T Min(const T x,const T y) {if(x<y) return x; return y;}
    template <class T> inline T fab(const T x) {return x>0?x:-x;}
    template <class T> inline T gcd(const T x,const T y) {return y?gcd(y,x%y):x;}
    template <class T> inline T lcm(const T x,const T y) {return x/gcd(x,y)*y;}
    template <class T> inline T Swap(T &x,T &y) {x^=y^=x^=y;}
    
    #include <cmath>
    
    const int maxn=1e5+5,mod=23333333;
    
    int n,m,f[maxn],s[maxn],g[320][maxn],ans;
    
    int main() {
    	n=read(9); m=sqrt(n);
    	f[0]=1;
    	rep(i,1,m) {
    		rep(j,0,i-1) s[j]=f[j];
    		rep(j,i,n) s[j]=(s[j-i]+f[j])%mod;
    		rep(j,0,n) {
    			f[j]=s[j];
    			if(j-i*(i+1)>=0) f[j]=(f[j]-s[j-i*(i+1)]+mod)%mod;
    		}
    	}
    	g[0][0]=1; ans=f[n];
    	rep(i,1,m) {
    		rep(j,i*(m+1),n) {
    			g[i][j]=(g[i-1][j-m-1]+g[i][j-i])%mod;
    			ans=(ans+1ll*g[i][j]*f[n-j]%mod)%mod;
    		}
    	}
    	print(ans,'
    ');
    	return 0;
    } 
    
  • 相关阅读:
    - (id)initWithCoder:(NSCoder *)decoder 的参数的作用
    pch定义的宏,文件引用报错,use of undeclared identifier
    tableView代理方法的调用时间,(主要是heightForRowAtIndexPath和cellForRowAtIndexPath调用时间)
    Receiver 'NSManagedObjectContext' for class message is a forward declaration 错误的解决方案
    delegate、notification、KVO的使用场景总结
    UIGestureRecognizer手势和scrollview冲突的简单解决
    CGAffineTransform中setTransform的几种类型以及注意事项
    使用"vuedraggable"插件实现列表排序功能;
    aliplayer的使用
    自制时间轴组件封装
  • 原文地址:https://www.cnblogs.com/AWhiteWall/p/14043310.html
Copyright © 2011-2022 走看看