SRM 510 2 250TheAlmostLuckyNumbersDivTwo(数位dp)
SRM 510 2 250TheAlmostLuckyNumbersDivTwo
Problem Statement
John and Brus believe that the digits 4 and 7 are lucky and all others are not. According to them, an almost lucky number is a number that contains at most one non-lucky digit in its decimal representation. Return the total number of almost lucky numbers between a and b, inclusive.
Definition
- ClassTheAlmostLuckyNumbersDivTwo
- Methodfind
- Parametersint , int
- Returnsint
- Method signatureint find(int a, int b)
Limits
- Time limit (s)2.000
- Memory limit (MB)64
Constraints
- a will be between 1 and 1,000,000, inclusive.
- b will be between a and 1,000,000, inclusive.
Test cases
- a4
- b7
Returns4
All numbers between 4 and 7 are almost lucky.- a8
- b19
Returns4
Numbers 8, 9, 14 and 17 are almost lucky.- a28
- b33
Returns0
No almost lucky numbers here.- a1234
- b4321
Returns36
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <typeinfo>
#include <fstream> using namespace std;
int dp[][] , dp2[][];
int dig[] ;
int vis[] ; void init ()
{
memset (dp , , sizeof(dp)) ;
memset (dp2 , , sizeof(dp2) ) ;
for (int i = ; i < ; i ++) dp[][i] = ;
for (int i = ; i <= ; i ++ ) {
for (int j = ; j < ; j ++) {
dp[i][j] += dp[i-][] + dp[i-][] ;
}
}
int a , b , c = , d = ;
for (int i = ; i <= ; i ++) for (int j = ; j < ; j ++) dp2[i][j] = dp[i][j] ;
for (int i = ; i <= ; i ++) {
a = dp[i][] , b = dp[i][] ;
for (int j = ; j < ; j ++) {
if (!(j == || j == )) {
dp2[i][] += dp[i-][j] ;
dp2[i][] += dp[i-][j] ;
}
else if (j == ) {
dp2[i][] += c ;
dp2[i][] += c ;
}
else if (j == ) {
dp2[i][] += d ;
dp2[i][] += d ;
}
}
// printf ("dp[%d][4]=%d , dp[%d][7]=%d\n" , i , dp[i][4] , i , dp[i][7]) ;
c = dp2[i][] - a , d = dp2[i][] - b ;
}
} int cal (int x)
{
memset (dig , , sizeof(dig)) ;
memset (vis , , sizeof(vis)) ;
int ans = ;
int len = ;
int tmp = x ;
int cnt = ;
while (x) {
dig[len ++] = x % ;
x /= ;
}
for (int i = len - ; i >= ; i --) {
vis[i] = cnt ;
if (dig[i] != && dig[i] != ) cnt ++ ;
}
//for (int i = 0 ; i < dig[1] ; i ++) ans += dp[1][i] ;
// ans += 10 ;
// printf ("hahaha") ;
// printf ("%d " , vis[0]) ;
// for (int i = 1 ; i < len ; i ++) printf ("%d " , vis[i]) ; puts ("") ;
for (int i = ; i < len ; i ++) {
printf ("vis[%d]=%d:\n\n" , i , vis[i] ) ;
if (vis[i] == ) {
for (int j = ; j < dig[i] ; j ++) ans += dp2[i][j] ;
}
else if (vis[i] == ) {
for (int j = ; j < dig[i] ; j ++) if (j == || j == ) ans += dp[i][j] , printf ("dp[%d][%d]=%d\n" , i , j , dp[i][j]) ;
}
if (i == len - ) ans -= dp[i][] ;
}
printf ("ans = %d\n" , ans ) ;
if (len == ) {
ans += dp[][] ;
}
else {
ans += dp[][] ;
for (int i = ; i < len - ; i ++) {
for (int j = ; j < ; j ++) ans += dp2[i][j] ;
}
}
printf ("%d:ans = %d\n" , tmp , ans) ;
printf ("-----------------------------------\n") ;
return ans ;
} class TheAlmostLuckyNumbersDivTwo {
public:
int find(int a, int b) {
puts ("") ;
if (a > b) swap(a,b) ;
init () ;
printf ("%d ~ %d\n" , a , b) ;
// printf ("%d - %d\n" , cal(b) , cal(a-1)) ;
return cal(b+) - cal(a) ;
//return 0 ;
}
}; // CUT begin
ifstream data("TheAlmostLuckyNumbersDivTwo.sample"); string next_line() {
string s;
getline(data, s);
return s;
} template <typename T> void from_stream(T &t) {
stringstream ss(next_line());
ss >> t;
} void from_stream(string &s) {
s = next_line();
} template <typename T>
string to_string(T t) {
stringstream s;
s << t;
return s.str();
} string to_string(string t) {
return "\"" + t + "\"";
} bool do_test(int a, int b, int __expected) {
time_t startClock = clock();
TheAlmostLuckyNumbersDivTwo *instance = new TheAlmostLuckyNumbersDivTwo();
int __result = instance->find(a, b);
double elapsed = (double)(clock() - startClock) / CLOCKS_PER_SEC;
delete instance; if (__result == __expected) {
cout << "PASSED!" << " (" << elapsed << " seconds)" << endl;
return true;
}
else {
cout << "FAILED!" << " (" << elapsed << " seconds)" << endl;
cout << " Expected: " << to_string(__expected) << endl;
cout << " Received: " << to_string(__result) << endl;
return false;
}
} int run_test(bool mainProcess, const set<int> &case_set, const string command) {
int cases = , passed = ;
while (true) {
if (next_line().find("--") != )
break;
int a;
from_stream(a);
int b;
from_stream(b);
next_line();
int __answer;
from_stream(__answer); cases++;
if (case_set.size() > && case_set.find(cases - ) == case_set.end())
continue; cout << " Testcase #" << cases - << " ... ";
if ( do_test(a, b, __answer)) {
passed++;
}
}
if (mainProcess) {
cout << endl << "Passed : " << passed << "/" << cases << " cases" << endl;
int T = time(NULL) - ;
double PT = T / 60.0, TT = 75.0;
cout << "Time : " << T / << " minutes " << T % << " secs" << endl;
cout << "Score : " << * (0.3 + (0.7 * TT * TT) / (10.0 * PT * PT + TT * TT)) << " points" << endl;
}
return ;
} int main(int argc, char *argv[]) {
cout.setf(ios::fixed, ios::floatfield);
cout.precision();
set<int> cases;
bool mainProcess = true;
for (int i = ; i < argc; ++i) {
if ( string(argv[i]) == "-") {
mainProcess = false;
} else {
cases.insert(atoi(argv[i]));
}
}
if (mainProcess) {
cout << "TheAlmostLuckyNumbersDivTwo (250 Points)" << endl << endl;
}
return run_test(mainProcess, cases, argv[]);
}
// CUT end
数位dp,,,,蛮有趣的,写了我三天,还好现在是考试季。数位dp能大大减少复杂度,拿这道题来说。如果用暴力来做要O(1e6),但用数位dp来的话,只需O(70)!!!!!
但同时换来的是复杂的构造。
推荐:http://www.cnblogs.com/archimedes/p/numerical-digit-dp.html
SRM 510 2 250TheAlmostLuckyNumbersDivTwo(数位dp)的更多相关文章
- POJ 3689 Apocalypse Someday [数位DP]
Apocalypse Someday Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 1807 Accepted: 87 ...
- 【BZOJ1662】[Usaco2006 Nov]Round Numbers 圆环数 数位DP
[BZOJ1662][Usaco2006 Nov]Round Numbers 圆环数 Description 正如你所知,奶牛们没有手指以至于不能玩"石头剪刀布"来任意地决定例如谁 ...
- bzoj1026数位dp
基础的数位dp 但是ce了一发,(abs难道不是cmath里的吗?改成bits/stdc++.h就过了) #include <bits/stdc++.h> using namespace ...
- uva12063数位dp
辣鸡军训毁我青春!!! 因为在军训,导致很长时间都只能看书yy题目,而不能溜到机房鏼题 于是在猫大的帮助下我发现这道习题是数位dp 然后想起之前讲dp的时候一直在补作业所以没怎么写,然后就试了试 果然 ...
- HDU2089 不要62[数位DP]
不要62 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 数位DP GYM 100827 E Hill Number
题目链接 题意:判断小于n的数字中,数位从高到低成上升再下降的趋势的数字的个数 分析:简单的数位DP,保存前一位的数字,注意临界点的处理,都是套路. #include <bits/stdc++. ...
- 数位dp总结
由简单到稍微难点. 从网上搜了10到数位dp的题目,有几道还是很难想到的,前几道基本都是模板题,供入门用. 点开即可看题解. hdu3555 Bomb hdu3652 B-number hdu2089 ...
- 数位DP入门
HDU 2089 不要62 DESC: 问l, r范围内的没有4和相邻62的数有多少个. #include <stdio.h> #include <string.h> #inc ...
- 数位DP之奥义
恩是的没错数位DP的奥义就是一个简练的dfs模板 int dfs(int position, int condition, bool boundary) { ) return (condition ? ...
随机推荐
- TCP/IP详解 笔记十四
TCP/IP协议(二) 连接的建立与终止 tcpdump -S输出TCP报文的格式 格式: 源>目的:标志 (标志就是tcp头部).标识首字符意义如下: 例如:telnet 某服务的输出(包括 ...
- JavaScript 单线程相关
众所周知,Javascript是单线程执行的,这也就是说:JavaScript在同一个时间上只能处理一件事.他不像C,Java等这些多线程的,可以开不同的线程去同时处理多件事情. 那么为什么别的语言都 ...
- AngularJs $interpolate 和 $parse
$interpolate 将一个字符串编译成一个插值函数.HTML编译服务使用这个服务完成数据绑定. 使用:$interpolate(text,[mustHaveExpression],[truste ...
- 重启电脑后,oracle数据库连接不上
oracle10g安装成功后使用正常,重启电脑后,连接不上了,电脑-服务中各个服务都手动重启了,仍然无效 报错信息:ORA-12514 TNS 监听程序当前无法识别连接描述符中请求服务 要手工去操作命 ...
- jboss性能优化
jboss linux jboss 部署时优化设置: 在/conf/web.xml中通过参数指定: <session-config> <session-ti ...
- Chrome控制台 JS调试的一些小技巧
$ $_命令返回最近一次表达式执行的结果,功能跟按向上的方向键再回车是一样的,但它可以做为一个变量使用在你接下来的表达式中. $0~$4则代表了最近5个你选择过的DOM节点.在页面右击选择审查元素,然 ...
- 深入理解css中的margin属性
深入理解css中的margin属性 之前我一直认为margin属性是一个非常简单的属性,但是最近做项目时遇到了一些问题,才发现margin属性还是有一些“坑”的,下面我会介绍margin的基本知识以及 ...
- java中的异常理解
java异常是java提供的用于处理程序中错误的一种机制.所谓错误是指在程序运行的过程中发生的一些异常事件(如:除0溢出,数组下标越界,所要读取的文件不存在).设计良好地程序应该在程序异常发生时提供处 ...
- _mysql.c(42) : fatal error C1083: Cannot open include file: 'config-win.h':问题的解决 mysql安装python
在win7下安装了Python后,想安装python-MySQL,使用pip安装出现如下问题: >pip install MySQL-python _mysql.c(42) : fatal er ...
- Centos6.5安装和使用docker
rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm yum install docke ...