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. MYSQL强制使用索引和禁止使用索引

    mysql强制索引和禁止某个索引 1.mysql强制使用索引:force index(索引名或者主键PRI) 例如: select * from table force index(PRI) limi ...

  2. 【网络协议】IP协议、ARP协议、RARP协议

    IP数据报 IP是TCP/IP协议族中最核心的协议,全部的TCP.UDP.ICMP.IGMP数据都以IP数据报的格式传输.IP仅提供尽力而为的传输服务.假设发生某种错误.IP会丢失该数据.然后发送IC ...

  3. nodejs什么值得买自动签到自动评论定时任务

    本项目是基于nodejs开发,实现的功能是,什么值得买自动签到,自动评论功能,自动发邮件,支持多人多账号运行 目的是为了,解放双手,轻松获取什么值得买的经验和积分,得到更高的等级,从而突破很会员等级限 ...

  4. Tomcat 安装与配置规范

    Tomcat 安装 演示版本:8.5.32 安装版 JDK推荐版本:jdk1.8 下载地址:https://tomcat.apache.org/download-80.cgi 安装教程 注意:tomc ...

  5. VUE 之 JS指令

    1.v-text的用法: 2.v-html 3.v-for 4.v-if , v-else if ,v-else v-if 每次生成都只有一个标签,即符合条件的标签. 5.v-show v-show ...

  6. 关于chroot

    1 chroot做了什么 chroot只是修改了所有的path resolution过程,也就是说,chroot之后,所有的命令和库的根目录都是chroot到的目录. 2 chroot使用的条件 目标 ...

  7. Remove FileUtil#copyMerge

    [HADOOP-12967] Remove FileUtil#copyMerge - ASF JIRA https://issues.apache.org/jira/browse/HADOOP-129 ...

  8. 在Java中如何正确地终止一个线程

    1.使用Thread.stop()? 极力不推荐此方式,此函数不安全且已废弃,具体可参考Java API文档 2.设置终止标识,例如: import static java.lang.System.o ...

  9. 程序中引入库文件的头文件 编译时并不需要显示的用gcc去链接他的库文件 why?

    拿一个苹果系统下的c文件为例: testArr.c #include <stdio.h> int main() { , , , , }; printf(]); } 当我们编译的时候  一般 ...

  10. ⭐驱动之module_init/module_exit与系统启动关系

    在前面helloworld的编写里面,我们使用了两个宏分别是module_init和module_exit,这里分析下为什么使用这两个宏. 在写模块的时候有两个特殊的函数,分别是init_module ...