hdu5185(背包)

这题思路还是有点巧的。。

首先要意识到每个数的平均值是1,那么只有首位取了0才有很多方案。。

然后考虑到这题的状态参数实在是太多了。。要好好选取。。不然tle。。

参数大概有这些。。非0区间的长度,区间总长度n,某个子区间的最后一位数以及子区间和。。。

其中除了子区间的最后一位数之外,其他参数均可达到n。。

分析一下最后一位数t最大的情况。。即直接单调递增。。其和显然为t*(t+1)/2。。这样可以看出这个t是在sqrt(n)级别的。。

考虑到n的范围。。所以要尽量取这个sqrt(n)。。

然后其实如果把区间和限制在n以内,把最后一位数也限制好了,那么区间长度是不可能超过n的。。这样就确定了状态

设d[i][j]为以i为尾数,j为区间长度和的方案数

d[i][j]=d[i-1][j-i]+d[i][j-i]

其实这个本质上就是个背包。。只是进入包的顺序有点小小的限制。。

实现上用滚动数组。。然后别开longlong。。会T。。




/**
 *        ┏┓    ┏┓
 *        ┏┛┗━━━━━━━┛┗━━━┓
 *        ┃       ┃  
 *        ┃   ━    ┃
 *        ┃ >   < ┃
 *        ┃       ┃
 *        ┃... ⌒ ...  ┃
 *        ┃       ┃
 *        ┗━┓   ┏━┛
 *          ┃   ┃ Code is far away from bug with the animal protecting          
 *          ┃   ┃   神兽保佑,代码无bug
 *          ┃   ┃           
 *          ┃   ┃        
 *          ┃   ┃
 *          ┃   ┃           
 *          ┃   ┗━━━┓
 *          ┃       ┣┓
 *          ┃       ┏┛
 *          ┗┓┓┏━┳┓┏┛
 *           ┃┫┫ ┃┫┫
 *           ┗┻┛ ┗┻┛
 */ 
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-12
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y>>1)
#define NM 50005
#define nm 1000005
#define pi 3.1415926535897931
using namespace std;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}



int n,inf,d[NM],f[NM],ans;

int main(){
    int _=read();inc(p,1,_){
	n=read();inf=read();
	inc(i,1,n)f[i]=1;ans=1;
	for(int i=2;i*(i+1)<=2*n;i++){
	    mem(d);
	    inc(j,i*(i+1)/2,n){
		d[j]=(f[j-i]+d[j-i])%inf;
	    }
	    inc(j,1,n)f[j]=d[j];
	    (ans+=f[n])%=inf;
	}
	printf("Case #%d: %d\n",p,ans);
    }
    return 0;
}





Equation

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 744    Accepted Submission(s): 272


Problem Description
Gorwin is very interested in equations. Nowadays she gets an equation like this
x1+x2+x3++xn=n, and here
0xinfor1inxixi+1xi+1for1in1

For a certain n, Gorwin wants to know how many combinations of xi satisfies above condition.
For the answer may be very large, you are expected output the result after it modular m.
 

Input
Multi test cases. The first line of the file is an integer T indicates the number of test cases.
In the next T lines, every line contain two integer n,m.

[Technical Specification]
1T<20
1n50000
1m1000000000
 

Output
For each case output should occupies one line, the output format is Case #id: ans, here id is the data number starting from 1, ans is the result you are expected to output.
See the samples for more details.
 

Sample Input
 
 
2 3 100 5 100
 

Sample Output
 
 
Case #1: 2 Case #2: 3
 

Source
 

Recommend
hujie   |   We have carefully selected several similar problems for you:   6286  6285  6284  6283  6282 
 

Statistic |  Submit |  Discuss | Note
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值