题目链接:

http://codeforces.com/problemset/problem/258/B

B. Little Elephant and Elections

time limit per test2 seconds
memory limit per test256 megabytes
#### 问题描述
> There have recently been elections in the zoo. Overall there were 7 main political parties: one of them is the Little Elephant Political Party, 6 other parties have less catchy names.
>
> Political parties find their number in the ballot highly important. Overall there are m possible numbers: 1, 2, ..., m. Each of these 7 parties is going to be assigned in some way to exactly one number, at that, two distinct parties cannot receive the same number.
>
> The Little Elephant Political Party members believe in the lucky digits 4 and 7. They want to evaluate their chances in the elections. For that, they need to find out, how many correct assignments are there, such that the number of lucky digits in the Little Elephant Political Party ballot number is strictly larger than the total number of lucky digits in the ballot numbers of 6 other parties.
>
> Help the Little Elephant Political Party, calculate this number. As the answer can be rather large, print the remainder from dividing it by 1000000007 (109 + 7).
#### 输入
> A single line contains a single positive integer m (7 ≤ m ≤ 109) — the number of possible numbers in the ballot.
#### 输出
> In a single line print a single integer — the answer to the problem modulo 1000000007 (109 + 7).

样例输入

7

样例输出

0

样例输入

8

样例输出

1440

题意

求[1,m]之间选7个数,保证其中一个数数位中包含4,7的个数比其他6个数的4,7的个数都加起来的都多。求满足条件的组合有多少种。

题解

先用数位dp求出包含k个4或7的数有多少个,然后再枚举最大的那个数有多少个4或7,去深搜回溯所有满足条件的情况。

代码

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf typedef __int64 LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII; const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0); //start---------------------------------------------------------------------- const int mod=1e9+7; int m;
int Num[11]; int arr[11],tot;
LL dp[11][11];
///ismax标记表示前驱是否是边界值
///ser标记前驱是否是前导零
LL dfs(int len,int k, bool ismax) {
if(k<0) return 0;
if (len == 0) {
///递归边界,这说明前驱都合法了
return k==0;
}
if (!ismax&&dp[len][k]>=0) return dp[len][k];
LL res = 0;
int ed = ismax ? arr[len] : 9; for(int i=0;i<=ed;i++){
res+=dfs(len-1,(i==4||i==7)?k-1:k,ismax&&i==ed);
} return ismax ? res : dp[len][k] = res;
} LL ans;
void dfs2(int num,int ma,int cnt,LL sum){
if(cnt>=ma) return;
if(num==6){
ans=(ans+sum)%mod;
return ;
}
for(int i=0;i<=9;i++){
Num[i]--;
dfs2(num+1,ma,cnt+i,sum*(Num[i]+1)%mod);
Num[i]++;
}
} LL solve(LL x,int k) {
tot = 0;
while (x) { arr[++tot] = x % 10; x /= 10; }
return dfs(tot,k,true);
} int main() {
clr(dp,-1);
scf("%d",&m);
for(int i=0;i<=9;i++) Num[i]=solve(m,i);
Num[0]--; ans=0;
for(int i=0;i<=9;i++){
Num[i]--;
dfs2(0,i,0,Num[i]+1);
Num[i]++;
} prf("%I64d\n",ans); return 0;
} //end-----------------------------------------------------------------------

Codeforces Round #157 (Div. 1) B. Little Elephant and Elections 数位dp+搜索的更多相关文章

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

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

  2. Codeforces Round #235 (Div. 2) D. Roman and Numbers (数位dp、状态压缩)

    D. Roman and Numbers time limit per test 4 seconds memory limit per test 512 megabytes input standar ...

  3. Codeforces Round #597 (Div. 2) F. Daniel and Spring Cleaning 数位dp

    F. Daniel and Spring Cleaning While doing some spring cleaning, Daniel found an old calculator that ...

  4. Codeforces Round #460 (Div. 2) B Perfect Number(二分+数位dp)

    题目传送门 B. Perfect Number time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  5. Codeforces Round #157 (Div. 2)

    A. Little Elephant and Chess 模拟. B. Little Elephant and Magic Square 枚举左上角,计算其余两个位置的值,在\(3\times 3\) ...

  6. Codeforces Round #396 (Div. 2) A B C D 水 trick dp 并查集

    A. Mahmoud and Longest Uncommon Subsequence time limit per test 2 seconds memory limit per test 256 ...

  7. Codeforces Round #136 (Div. 1)C. Little Elephant and Shifts multiset

    C. Little Elephant and Shifts Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/pro ...

  8. 字符串(后缀自动机):Codeforces Round #129 (Div. 1) E.Little Elephant and Strings

    E. Little Elephant and Strings time limit per test 3 seconds memory limit per test 256 megabytes inp ...

  9. Codeforces Round #136 (Div. 1) B. Little Elephant and Array

    B. Little Elephant and Array time limit per test 4 seconds memory limit per test 256 megabytes input ...

随机推荐

  1. 在windows上安装不同(两个)版本的Mysql数据库

    1.起因: 需要导入一个sql文件,发现死活导不进去.当执行到这一句时,就有问题.经过一番搜索,原来是我的数据库版本(原先Mysql版本5.5)低了,而支持该语句的版本应该是至少要5.7.那我索性就去 ...

  2. Mysql5.7登录错误1045和1130的解决方法,亲测有用,希望能帮助到你们。

    Mysql (针对Mysql5.7版本,其他版本可能略有不同) 错误:1045 解决方法: 以管理员身份运行cmd(win8系统:win+x 键 ,再按 A键 ),进入Mysql安装目录下的bin目录 ...

  3. 跨域(Cross-Domain) AJAX for IE8 and IE9

    1.有过这样一段代码,是ajax $.ajax({ url: "http://127.0.0.1:9001", type: "POST", data: JSON ...

  4. TinkerPop简述

    ThinkerPop Apache 顶级项目 概述 TinkerPop是一个面向实时事务处理(OLAP)以及批量.分析型(OLTP)的开源的图计算框架.TinkerPop是一个可以应用于不同图形数据库 ...

  5. PCIE_DMA实例二:xapp1052的EDK仿真

    一:前言 这篇博客是我应一位网友之约写的,他想要学习基于FPGA的PCIe DMA控制器设计,但是手上没有合适的Xilinx开发板,而且xapp1052又没有提供仿真代码,让他的学习陷入了困境.所以我 ...

  6. LaTeX宏包TikZ绘图示例——Go语言起源图

      本例所绘图形选自<Go语言程序设计>(作者:Alan A. A. Donovan与Brian W. Kernighan)一书的前言部分. 完整代码 \documentclass{art ...

  7. P4249 [WC2007]剪刀石头布

    有一个竞赛图,要给一些边定向,求三元环最多的数量 反过来考虑最少的不是环的三个点(称为不好的环),一定有一个点有2条入边,一个点有2条出边,一个点1入边1出边 可以对每一个不好的环只记录入边为2的点, ...

  8. 应用.NET控制台应用程序开发批量导入程序。

    一.最近一直在调整去年以及维护去年开发的项目,好久没有在进行个人的博客了.每天抽了一定的时间在研究一些开源的框架,Drapper 以及NHibernate以及当前比较流行的SqlSuper框架 并进行 ...

  9. selenium无法正常运行 Chrome浏览器,cannot find Chrome binary的问题

    有些同学在运行selenium-chrome时会遇到这个问题, System.setProperty("webdriver.chrome.driver","files/c ...

  10. canvas反向裁剪技巧

    我们都知道在canvas 可以通过clip来实现剪裁功能,其步骤一般是先设置要裁剪的区域(路径),然后通过ctx.clip()的实现裁剪,裁剪之后,后续的绘制只能在裁剪的区域显示效果,比如如下一段代码 ...