2016 大连 K Guess the number HDU 5981 DP 打表找规律
2017-04-02 01:42
323 查看
弄清楚n^2 DP之后,打表能发现l与r随着n的增大是有规律的。用sum维护一下f的前缀和,就可以很快求出答案。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int mod=1e8+73;
const int maxn=5e6+1;
int a,b,dp[maxn],n,f[maxn],sum[maxn];
int main()
{
dp[0]=0;
dp[1]=f[1]=f[0]=sum[0]=1;
sum[1]=2;
/*
// 找规律部分
memset(dp,0x3f,sizeof dp);
for (int i=2;i<maxn;++i) {
printf("i=%d\n",i);
for (int j=1;j<=i;++j)
dp[i]=min(dp[i],1+max(j-1,dp[i-j]));
int cnt=0;
for (int j=1;j<=i;++j)
if (dp[i]==1+max(j-1,dp[i-j])) {
++cnt;
printf("%d ",i-j);
f[i]=(f[i]+f[i-j])%mod;
}
puts("");
}
*/
int l=1,r=2;
for (int i=2;i<maxn;++i) {
dp[i]=1+max(r-1,dp[i-r]);
f[i]=(sum[i-l]-sum[i-r-1]+mod)%mod;
sum[i]=(sum[i-1]+f[i])%mod;
++l;
if (l>r) {
++r;
l=1;
}
}
while (scanf("%d%d",&a,&b)==2)
printf("%d %d\n",dp[b-a+1],f[b-a+1]);
return 0;
}
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int mod=1e8+73;
const int maxn=5e6+1;
int a,b,dp[maxn],n,f[maxn],sum[maxn];
int main()
{
dp[0]=0;
dp[1]=f[1]=f[0]=sum[0]=1;
sum[1]=2;
/*
// 找规律部分
memset(dp,0x3f,sizeof dp);
for (int i=2;i<maxn;++i) {
printf("i=%d\n",i);
for (int j=1;j<=i;++j)
dp[i]=min(dp[i],1+max(j-1,dp[i-j]));
int cnt=0;
for (int j=1;j<=i;++j)
if (dp[i]==1+max(j-1,dp[i-j])) {
++cnt;
printf("%d ",i-j);
f[i]=(f[i]+f[i-j])%mod;
}
puts("");
}
*/
int l=1,r=2;
for (int i=2;i<maxn;++i) {
dp[i]=1+max(r-1,dp[i-r]);
f[i]=(sum[i-l]-sum[i-r-1]+mod)%mod;
sum[i]=(sum[i-1]+f[i])%mod;
++l;
if (l>r) {
++r;
l=1;
}
}
while (scanf("%d%d",&a,&b)==2)
printf("%d %d\n",dp[b-a+1],f[b-a+1]);
return 0;
}
相关文章推荐
- HDU 5981 && 2016ICPC大连 K: Guess the number(推理)
- HDU-5981-Guess the number-规律
- hdu 1028 Ignatius and the Princess III(打表,后期进阶补->找规律和dp)
- hdu 5981 Guess the number
- HDU 4588 Count The Carries 数位DP || 打表找规律
- HDU 4588 Count The Carries 数位DP || 打表找规律
- hdu 5981:Guess the number 动态规划
- HDU 1562 Guess the number
- HDU 5810 Balls and Boxes(打表找规律)——2016 Multi-University Training Contest 7 1002
- The Number of Paths - HDU 1293 DP
- hdu.1562.Guess the number
- HDU 1562 Guess the number
- HDU 1562 Guess the number 水题 DFS
- HDU 1562 Guess the number
- hdu 4151 The Special Number (DP)
- HDU 1058 Humble Number(打表dp)
- HDU-3337 Guess the number 测试输入数据
- hdu 1562 Guess the number
- hdu 2147 kiki's game(DP(SG)打表找规律)
- HDOJ(HDU) 1562 Guess the number(水题,枚举就行)