1009 数字1的数量
- 1 秒
- 131,072 KB
- 20 分
- 3 级题
给定一个十进制正整数N,写下从1开始,到N的所有正数,计算出其中出现所有1的个数。
例如:n = 12,包含了5个1。1,10,12共包含3个1,11包含2个1,总共5个1。
收起
输入
输入N(1 <= N <= 10^9)
输出
输出包含1的个数
输入样例
12
输出样例
5
题解:套用DP板子,dp[i][j] 表示:在位置 i 的前边有 j 个 1 的数量。
#include<set>
#include<map>
#include<list>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<bitset>
#include<iomanip>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define eps (1e-8)
#define MAX 0x3f3f3f3f
#define u_max 1844674407370955161
#define l_max 9223372036854775807
#define i_max 2147483647
#define re register
#define pushup() tree[rt]=tree[rt<<1]+tree[rt<<1|1]
#define nth(k,n) nth_element(a,a+k,a+n); // 将 第K大的放在k位
#define ko() for(int i=2;i<=n;i++) s=(s+k)%i // 约瑟夫
#define ok() v.erase(unique(v.begin(),v.end()),v.end()) // 排序,离散化
#define Catalan C(2n,n)-C(2n,n-1) (1,2,5,14,42,132,429...) // 卡特兰数
using namespace std;
inline int read(){
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' & c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
typedef long long ll;
const double pi = atan(1.)*4.;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fLL;
const int M=63;
const int N=1e5+5;
ll dp[20][20];
int a[20],b[20],p=0;
ll dfs(int pos,int num,int leap,int flag){
if(pos==-1) return num;
if(!flag&&dp[pos][num]!=-1) return dp[pos][num];
int up=flag?a[pos]:9;
ll ans=0;
for(int i=0;i<=up;i++)
ans+=dfs(pos-1,num+(i==1),leap&&i==0,flag&&(a[pos]==i));
if(!flag) dp[pos][num]=ans;
return ans;
}
ll fun(ll n){
while(n){
int h=n%10;
a[p++]=h;
n/=10;
}
return dfs(p-1,0,1,1);
}
int main(){
ll n;
memset(dp,-1,sizeof(dp));
scanf("%lld",&n);
printf("%lld\n",fun(n));
return 0;
}
不要62Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 65286 Accepted Submission(s): 26000 Problem Description 杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer)。 Input 输入的都是整数对n、m(0<n≤m<1000000),如果遇到都是0的整数对,则输入结束。 Output 对于每个整数对,输出一个不含有不吉利数字的统计个数,该数值占一行位置。 Sample Input 1 100 0 0 Sample Output 80 Author qianneng Source Recommend lcy | We have carefully selected several similar problems for you: 2091 2093 2085 2082 2080 |
#include<set>
#include<map>
#include<list>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<bitset>
#include<iomanip>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define eps (1e-8)
#define MAX 0x3f3f3f3f
#define u_max 1844674407370955161
#define l_max 9223372036854775807
#define i_max 2147483647
#define re register
#define pushup() tree[rt]=tree[rt<<1]+tree[rt<<1|1]
#define nth(k,n) nth_element(a,a+k,a+n); // 将 第K大的放在k位
#define ko() for(int i=2;i<=n;i++) s=(s+k)%i // 约瑟夫
#define ok() v.erase(unique(v.begin(),v.end()),v.end()) // 排序,离散化
#define Catalan C(2n,n)-C(2n,n-1) (1,2,5,14,42,132,429...) // 卡特兰数
using namespace std;
inline int read(){
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' & c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
typedef long long ll;
const double pi = atan(1.)*4.;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fLL;
const int M=63;
const int N=1e5+5;
int dp[20][2],a[20];
int dfs(int pos,int st,int per,int flag){
if(pos==-1) return 1;
if(!flag&&dp[pos][st]!=-1) return dp[pos][st];
int up=flag?a[pos]:9;
int ans=0;
for(int i=0;i<=up;i++){
if(i==4) continue;
else if(per==6&&i==2) continue;
ans+=dfs(pos-1,i==6,i,flag&&(a[pos]==i));
}
if(!flag) dp[pos][st]=ans;
return ans;
}
int fun(int n){
int p=0;
while(n){
a[p++]=n%10;
n/=10;
}
return dfs(p-1,0,0,1);
}
int main(){
int n,m;
while(scanf("%d %d",&n,&m)!=EOF){
if(n==0&&m==0) break;
memset(dp,-1,sizeof(dp));
printf("%d\n",fun(m)-fun(n-1));
}
return 0;
}