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. openwrt 编译 gmediarender

    output_gstreamer.o: In function `my_bus_callback': output_gstreamer.c:(.text+0xf68): undefined refer ...

  2. php利用cookie防止重复提交解决办法

    原理:如果数据通过了上边的两次验证,说明数据是合法有效的数据,这时候我们把提交的数据串接为一个字符串,并用MD5加密后得到一个MD5的值. 接着我们把这个值通过Cookie放进客户端,当用户下一次提交 ...

  3. (转)OutOfMemory时抓取heap 快照

    转自:https://testerhome.com/topics/579 首先说一下,在程序没有崩溃的时候如何抓取heap快照.这个大家应该都知道,在ddms中自带此功能.   见上图首先我们选中一个 ...

  4. win7 64位安装vs2013 出现'System.AccessViolationException的错误

    用管理员身份运行CMD,输入netsh winsock reset并回车(注意,必须是已管理员身份运行,这个重置LSP连接)

  5. python day- 10 动态参数 函数的嵌套 命名空间和作用域 global和nolocal

    一.动态参数: 动态参数是形参的一类 分为:动态位置参数(* + 函数名)表示 调用后返回的是元祖 动态关键字参数(** + 函数名)表示 形参的排列顺序: 位置参数     >   动态位置参 ...

  6. REST RPC HTTP vs 高性能二进制协议 序列化和通信协议

    edisonchou https://mp.weixin.qq.com/s/-XZXqXawR-NxJMPCeiNsmg .NET Core微服务之服务间的调用方式(REST and RPC) Edi ...

  7. C语言操作SQLite数据库

    SQLite头文件和源文件下载地址http://www.sqlite.org/download.html 以下是示例代码 #include <stdio.h> #include " ...

  8. JavaScript算法题(一) && 数组reduce使用

    可参考Array.reduce用法 1. 请编写getMissingElement函数,返回给定数组中缺少的元素(数组里的元素为0~9,只会缺失一个). Example: getMissingElem ...

  9. ajax 提交所有表单内容及上传图片(文件),以及单独上传某个图片(文件)

    我以演示上传图片为例子: java代码如下(前端童鞋可以直接跳过看下面的html及js): package com.vatuu.web.action; import java.io.File; imp ...

  10. 关于谷歌浏览器默认字体12px的解决方案

    1. * Chrome 中文界面下默认会将小于 12px 的文本强制按照 12px 显示, 可通过加入 CSS 属性 -webkit-text-size-adjust: none; 解决. 超链接访问 ...