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. Android Menu开源项目整合工程

    本实例整合了关于Android Menu的优秀开源代码,方便有需要用到Menu开源项目的小伙伴使用. 一.整合的项目有: SlidingMenu:https://github.com/jfeinste ...

  2. C# Excel批注“哪种开发语言最好”

    Excel批注经常使用于为个别的单元格加入凝视.读者可以从凝视中获取额外的信息. 批注可隐藏,仅仅会在单元格右上方显示红色三角.加入后不会对单元格的内容喧宾夺主.在日常编程处理Excel中,为个别单元 ...

  3. HDU 6138 Fleet of the Eternal Throne 后缀数组 + 二分

    Fleet of the Eternal Throne Problem Description > The Eternal Fleet was built many centuries ago ...

  4. 【腾讯bugly干货分享】精神哥手把手教你怎样智斗ANR

    上帝说要有ANR,于是Bugly就有了ANR上报.那么ANR究竟是什么? 近期非常多童鞋问起精神哥ANR的问题,那么这次就来聊一下,鸡爪怎么泡才好吃.噢不,是怎样高速定位ANR. ANR是什么 简单说 ...

  5. react Native 踩坑记录

    应用 1 安卓打包 经验 解决方案 ,官方 解决方案 2 调试 用 React-Native-Debugger 教程 3 微信分享和登录 使用 react-native-wechat    地址 设计 ...

  6. spring boot---WebFilter注解 实现自定义登录过滤器

    https://my.oschina.net/wangnian/blog/647976 http://www.jianshu.com/p/05c8be17c80a

  7. hrtimer高精度定时器的简单使用【学习笔记】

    #include <linux/module.h> #include <linux/kernel.h> #include <linux/hrtimer.h> #in ...

  8. YTU 2392: 求各位数字之和

    2392: 求各位数字之和 时间限制: 1 Sec  内存限制: 128 MB 提交: 1253  解决: 292 题目描述 编写一个程序,计算任意输入的正整数的各位数字之和.(输入的位数不要超过10 ...

  9. AJAX 用户验证方法

    JSP <td width="10%" class="main_matter_td">真实姓名</td> <td width=&q ...

  10. angularJS ng-if的用法

    ng-if主要是用来判断是否显示,也可以做为而者选择其中一个的方法,满足判断条件ng-if="变量名" 显示,否者不显示,也可以用ng-if="!变量名"取反, ...