SSL 2386 序列#线性动态规划#
程序员文章站
2024-03-19 08:16:40
...
比赛
题目
分析
dp,状态转移方程:
逆推代码
#include <cstdio>
#include <cstring>
using namespace std;
struct node{int x,y,next;}e[100001];
int f[2002][2002],n,m,k,ans,ls[2001];
int main(){
scanf("%d%d",&n,&k);
for (int i=1;i<=n;i++) f[i][1]=1;
for (int i=1;i<=n;i++)
for (int j=1;j<=i;j++)
if (i%j==0) e[++m].x=i,e[m].y=j,e[m].next=ls[e[m].x],ls[e[m].x]=m;//邻接表优化
for (int j=2;j<=k;j++)
for (int i=1;i<=n;i++){
int t=ls[i];
while (t){
f[i][j]=(f[i][j]+f[e[t].y][j-1])%1000000007;
t=e[t].next;
}
}
for (int i=1;i<=n;i++) ans=(ans+f[i][k])%1000000007;
printf("%d",ans);
return 0;
}
顺推代码
#include <cstdio>
#include <cstring>
#define p 1000000007
using namespace std;
int f[2002][2002],n,m,ans;
int main(){
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++) f[i][1]=1;
for (int j=2;j<=m;j++)
for (int i=1;i<=n;i++)
for (int k=1;k<=n/i;k++) f[i*k][j]=(f[i][j-1]+f[i*k][j])%p;
for (int i=1;i<=n;i++) ans=(ans+f[i][m])%p;
printf("%d",ans);
return 0;
}
下一篇: Matlab fmincon函数