Little Elephant and Elections CodeForces - 258B
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的更多相关文章
- 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 ...
- Codeforces Round #157 (Div. 2) D. Little Elephant and Elections(数位DP+枚举)
数位DP部分,不是很难.DP[i][j]前i位j个幸运数的个数.枚举写的有点搓... #include <cstdio> #include <cstring> using na ...
- 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 ...
- AC日记——Little Elephant and Shifts codeforces 221e
E - Little Elephant and Shifts 思路: 一次函数线段树(疯狂debug): b不断循环左移,判断每次最小的|i-j|,a[i]=b[j]: 仔细观察发现,每个bi移动时, ...
- AC日记——Little Elephant and Array codeforces 221d
221D - Little Elephant and Array 思路: 莫队: 代码: #include <cmath> #include <cstdio> #include ...
- AC日记——Little Elephant and Numbers codeforces 221b
221B - Little Elephant and Numbers 思路: 水题: 代码: #include <cmath> #include <cstdio> #inclu ...
- AC日记——Little Elephant and Function codeforces 221a
A - Little Elephant and Function 思路: 水题: 代码: #include <cstdio> #include <iostream> using ...
- 【Codeforces 258B】 Sort the Array
[题目链接] http://codeforces.com/contest/451/problem/B [算法] 模拟 在序列中找到一段单调递增的子序列,将这段序列反转,然后判断序列是否变得单调递增,即 ...
- Little Elephant and Array CodeForces - 220B (莫队)
The Little Elephant loves playing with arrays. He has array a, consisting of npositive integers, ind ...
随机推荐
- 转载:用python爬虫抓站的一些技巧总结
原文链接:http://www.pythonclub.org/python-network-application/observer-spider 原文的名称虽然用了<用python爬虫抓站的一 ...
- Unity3D游戏开发之粒子系统实现具体解释
今天为大家分享的是Unity3D中的粒子系统.粒子系统通经常使用来表现烟雾.云等高级效果.是一个十分注重制作技巧的部分.今天我们将以一个气泡的演示实例来一起学习怎样在Unity3D中使用粒子系统 ...
- Spring在3.1版本后的bean获取方法的改变
xml配置不变,如下 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="ht ...
- DOM操作一
1.通过ID选取元素 var section = document.getElementById("section1"); 2.通过ID查找多个元素 function getEle ...
- Shell编程——Shell中的数学运算
在Linux Shell中进行数学运算,通常能够使用的运算符有: 简单运算: let [] (()) 高级运算: expr bc 1.let命令 let命令是bash内置命令.能够实现简单的算术以及逻 ...
- RabbitMQ使用简述
RabbitMQ基于AMQP协议. AMQP:是一个高级抽象层消息通信协议,RabbitMQ是AMQP协议的实现 RabbitMQ使用:Exchange(交换机)根据routing-key(路由选择键 ...
- POJ 3279 Dungeon Master
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 21242 Accepted: 8265 D ...
- html5--6-10 CSS选择器7--伪类选择器
html5--6-10 CSS选择器7--伪类选择器 实例 学习要点 掌握常用的CSS选择器 了解不太常用的CSS选择器 什么是选择器 当我们定义一条样式时候,这条样式会作用于网页当中的某些元素,所谓 ...
- 蒟蒻的HNOI2017滚粗记
蒟蒻的第一次省选,然而并没有RP爆发... Day 1: 8:00开考,(然而密码错误是什么鬼).跌跌撞撞,8:40终于拿到纸质试题. { T1:作为一名没有学过Splay的蒟蒻,考场上真的被出题人感 ...
- CodeForces242D:Connected Components (不错的并查集)
We already know of the large corporation where Polycarpus works as a system administrator. The compu ...