Little Elephant and Elections CodeForces - 258B

题意:给出m,在1-m中先找出一个数x,再在剩下数中找出6个不同的数y1,...,y6,使得y1到y6中数字4和7出现的总次数严格小于x中数字4和7出现的总次数。求方案数。

方法:先数位dp分别预处理出:1到m之间,数字4和7出现的总次数为0到9的数。(总共最多10个数字,第一个最大1,因此4和7出现的总次数最多9次)然后枚举x,再暴力dfs枚举6个数,统计方案数。

问题(细节):

1.(13行)数位dp时,如果p<0则需要直接return0,否则导致数组越界WA。

2.(55-61行)并非只需要枚举x,然后在出现4和7总次数小于x的数中任意取6个即可。要求不是6个数的最大值小于x,而是6个数的和小于x。

3.(62-70行)逻辑错误,对比72-78行

还可以写成

 for(i=;i<=;i++)
{
if(ans1[i]==) continue;
nowmax=i;
dfs(,,ans1[i]);
}

4.失败的dfs(不能按照从小到大的顺序取)

 void dfs(LL num,LL maxn,LL maxnum,LL sum,LL nowans)
{
if(sum>=nowmax) return;
if(num==)
{
anss=(anss+nowans)%md;
return;
}
if(maxnum+<=ans1[maxn])
{
dfs(num+,maxn,maxnum+,sum+maxn,nowans*(ans1[maxn]-maxnum)%md);
}
LL i;
for(i=maxn+;i<nowmax;i++)
if(ans1[i]>)
{
dfs(num+,i,,sum+i,nowans*ans1[i]%md);
}
}

程序:

 #include<cstdio>
#include<cstring>
#define md 1000000007
typedef long long LL;
LL ans[][][];
LL w[];
LL ans1[];
LL m,ttt,anss,low,anst;
LL a[];
LL nowmax;
LL dp(LL p,LL pos,bool pre0,bool limit)
{
if(p<) return ;//曾经忘记,导致下面15行数组越界以及无用的dfs,导致WA
if(pos<) return p==&&!pre0;
if(!limit&&ans[p][pos][pre0]!=-)
return ans[p][pos][pre0];
LL i,res=,end=limit?w[pos]:;
for(i=;i<=end;i++)
res+=dp(p-(i==||i==),pos-,pre0&&i==,limit&&i==w[pos]);
return limit?res:(ans[p][pos][pre0]=res);
}
LL get(LL x,LL tt)
{
LL g;
for(g=;x>;x/=) w[++g]=x%;
return dp(tt,g,,);
}
void dfs(LL num,LL sum,LL nowans)
{
if(sum>=nowmax) return;
if(num==)
{
anss=(anss+nowans)%md;
return;
}
LL i;
for(i=;i<nowmax;i++)
{
if(ans1[i]==) continue;
ans1[i]--;
dfs(num+,sum+i,nowans*(ans1[i]+)%md);
ans1[i]++;
}
}
int main()
{
LL i;
memset(ans,-,sizeof(ans));
scanf("%I64d",&m);
for(i=;i<=;i++)
{
//printf("%I64d %I64d\n",i,get(m,i));
ans1[i]=get(m,i);
}
// for(i=1;i<=10;i++)
// {
// low+=ans1[i-1];
// ttt=low*(low-1)%md*(low-2)%md*(low-3)%md*(low-4)%md*(low-5)%md;
// if(ttt<0) ttt=0;
// anss=(anss+ttt*ans1[i])%md;//此版本错误
// }
// for(i=1;i<=10;i++)
// {
// if(ans1[i]==0) continue;
// nowmax=i;
// //ans1[i]--;
// dfs(0,0,1);
// //ans1[i]++;
// anss=(anss*ans1[i])%md;//此版本错误
// }
for(i=;i<=;i++)
{
if(ans1[i]==) continue;
nowmax=i;
anss=;
dfs(,,);
anst=(anst+anss*ans1[i])%md;
}
//printf("%I64d",anss);
printf("%I64d",anst);
return ;
}

Little Elephant and Elections CodeForces - 258B的更多相关文章

  1. Codeforces Round #157 (Div. 1) B. Little Elephant and Elections 数位dp+搜索

    题目链接: http://codeforces.com/problemset/problem/258/B B. Little Elephant and Elections time limit per ...

  2. Codeforces Round #157 (Div. 2) D. Little Elephant and Elections(数位DP+枚举)

    数位DP部分,不是很难.DP[i][j]前i位j个幸运数的个数.枚举写的有点搓... #include <cstdio> #include <cstring> using na ...

  3. CF 258B Little Elephant and Elections [dp+组合]

    给出1,2,3...m 任取7个互不同样的数a1,a2,a3,a4,a5,a6,a7 一个数的幸运度是数位上4或7的个数 比方244.470幸运度是2. 44434,7276727.4747,7474 ...

  4. AC日记——Little Elephant and Shifts codeforces 221e

    E - Little Elephant and Shifts 思路: 一次函数线段树(疯狂debug): b不断循环左移,判断每次最小的|i-j|,a[i]=b[j]: 仔细观察发现,每个bi移动时, ...

  5. AC日记——Little Elephant and Array codeforces 221d

    221D - Little Elephant and Array 思路: 莫队: 代码: #include <cmath> #include <cstdio> #include ...

  6. AC日记——Little Elephant and Numbers codeforces 221b

    221B - Little Elephant and Numbers 思路: 水题: 代码: #include <cmath> #include <cstdio> #inclu ...

  7. AC日记——Little Elephant and Function codeforces 221a

    A - Little Elephant and Function 思路: 水题: 代码: #include <cstdio> #include <iostream> using ...

  8. 【Codeforces 258B】 Sort the Array

    [题目链接] http://codeforces.com/contest/451/problem/B [算法] 模拟 在序列中找到一段单调递增的子序列,将这段序列反转,然后判断序列是否变得单调递增,即 ...

  9. Little Elephant and Array CodeForces - 220B (莫队)

    The Little Elephant loves playing with arrays. He has array a, consisting of npositive integers, ind ...

随机推荐

  1. sanic官方文档解析之logging和request Data

    1,sanic的logging: Sanic允许有做不同类型的日志(通过的日志,错误的日志),在基于Python3的日志API接口请求,你必须具备基本的Python3的日志知识,在你如果想创建一个新的 ...

  2. LiberOJ #6210. 「美团 CodeM 决赛」tree 树形DP

    题目链接:点这里 题解: 需要证明,所求的路径一定是全部权值都为1或者,路径上权值至多有一个为2其余为1且权值2在路径中央. 然后树形DP 设定dp[i][0/1] 以1为根的情况下,以i 节点下子树 ...

  3. SequenceFileInputFormat区别TextInputFormat

    通过InputFormat,Hadoop可以: l           检查MapReduce输入数据的正确性: l           将输入数据切分为逻辑块InputSplit,这些块会分配给Ma ...

  4. mongodb与sql聚合对应图 M

    mongodb与sql聚合对应图 M - CSDN博客 http://blog.csdn.net/u011930016/article/details/49422425 SQL Terms, Func ...

  5. Hive JOIN的基本操作 及 内部实现

    1.HIVE基本操作: [一起学Hive]之十一-Hive中Join的类型和用法 注:HIve不支持非等值连接: 什么是等值连接: //Oracle SQL 不等值连接 //通过不等值连接查找7788 ...

  6. (linux)main.c中的初始化

    main.c中的初始化 head.s在最后部分调用main.c中的start_kernel() 函数,从而把控制权交给了它. 所以启动程序从start_kernel()函数继续执行.这个函数是main ...

  7. ES6的相关新属性

    ES6  引入了类这个概念. 1.class……extends es6中的class与es5 中的function差不多: class Student extends People , student ...

  8. HDU3374 String Problem —— 最小最大表示法 + 循环节

    题目链接:https://vjudge.net/problem/HDU-3374 String Problem Time Limit: 2000/1000 MS (Java/Others)    Me ...

  9. windwo访问linux文件夹方法

    windwo访问linux文件夹:是通过linux的samba来实现的: 安装samba需要安装samba-client.samba-common.smaba3个包. 一:安装rpm 现有一个服务器l ...

  10. bzoj2973石头游戏——矩阵乘法

    题目:权限题! 写了一下,但提交不了,先放着吧. 代码如下: #include<iostream> #include<cstdio> #include<cstring&g ...