hdu6279(思维DP+优化技巧)

感觉这题要想到dp挺难的。。

这个状态选得就比较巧了。。

先考虑序列上的情况。。

设d[i][j]为将前j个数切成i段的和

所以答案只要枚举段数i再乘以总长度就能构成环了orz

然后显然d[i][j]=sum(d[i-1][j-k]*k)  k=1..j-i

直接转移肯定tle。。所以需要优化。。给出2种方法。。

第一种自己想的。。。

其实

sum(d[i-1][j-k]*k)

=sum(d[i-1][k]*(j-k))

=sum(j*d[i-1][k]-d[i-1][k]*k)

=j*sum(d[i-1][k])-sum(d[i-1][k]*k)

所以只要维护d[i-1][k]和d[i-1][k]*k的前缀和即可。。

第二种是别人的。。。

sum(d[i-1][j-k]*k)

=sum(d[i-1][j-1-k]*k)[k=1..j-2]+sum(d[i-1][j-k])[k=1..j-1]

=d[i][j-1]+sum(d[i-1][k])[k=1..j-1]

然后只需要维护d[i-1][k]的前缀和就好了。。orz


不管哪种方法都挺巧的。。值得学习。。毕竟这个方程还挺常见的。。。

然后最后记数的时候。。由于把序列旋转成环。。对每个切好的i个片段来说,可以由i个状态转到当前状态。。所以在计数的时候记得除以i。。当然不是直接除。。要用逆元。。。



/**
 *          ┏┓    ┏┓
 *          ┏┛┗━━━━━━━┛┗━━━┓
 *          ┃       ┃  
 *          ┃   ━    ┃
 *          ┃ >   < ┃
 *          ┃       ┃
 *          ┃... ⌒ ...  ┃
 *          ┃              ┃
 *          ┗━┓          ┏━┛
 *          ┃          ┃ 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 5005
#define nm 1000005
#define pi 3.1415926535897931
const ll inf=1000000007;
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;
}




ll qpow(ll x,ll t){return t==0?1LL:qpow(sqr(x)%inf,t>>1)*(t%2?x:1)%inf;}

int n,m;
ll c[NM],ans,inv[NM];
int d[NM][NM];
int main(){
    inc(i,1,5000)inv[i]=qpow(i,inf-2);
    c[0]=1;
    inc(i,1,5000){
	ll t=c[i-1],s=(i-1)*c[i-1]%inf;
	inc(j,i,5000)d[i][j]=(j*t-s+inf)%inf,t=(t+c[j])%inf,s=(s+j*c[j])%inf;
	inc(j,i-1,5000)c[j]=d[i][j];
    }
//  inc(i,1,4){inc(j,1,4)printf("%d ",d[i][j]);putchar('\n');}
    while(~scanf("%d%d",&n,&m)){
	if(n<m)swap(n,m);ans=0;
	inc(i,1,m)ans=(ans+(ll)d[i][n]*d[i][m]%inf*inv[i])%inf;
	printf("%lld\n",ans*(n+m)%inf);
    }
    return 0;
}



Circular Coloring

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)
Total Submission(s): 44    Accepted Submission(s): 15


Problem Description
Bobo considers (n+m) balls arranged in a circle.
The balls are numbered with 0,1,,(n+m1) where the ball i and the ball (i+1)mod(n+m) are adjacent.

Bobo would like to color n of his balls black and m of his balls white.
Bobo groups adjacent balls with same colors, and he determines the weight of the coloring as the product of the lengths of groups.

He would like to know the sum of the weight of the possible colorings, modulo (109+7).
 

Input
The input consists of several test cases and is terminated by end-of-file.

Each test case contains two integers n and m.
 

Output
For each test case, print an integer which denotes the result.

## Constraint

* 1n,m5000
* The number of test cases does not exceed 5000.
 

Sample Input
 
 
1 2 2 3 5000 5000
 

Sample Output
 
 
6 40 975597525
Hint
For the second sample, there are $10$ possible colorings (listed below). The number followed is the corresponding weight. * `BBWWW` ($6$) * `BWBWW` ($2$) * `BWWBW` ($2$) * `BWWWB` ($6$) * `WBBWW` ($6$) * `WBWBW` ($2$) * `WBWWB` ($2$) * `WWBBW` ($6$) * `WWBWB` ($2$) * `WWWBB` ($6$)
 

Source
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:   6296  6295  6294  6293  6292 
 

Statistic |  Submit |  Discuss | Note
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值