PAT (Advanced Level) 1049. Counting Ones (30)
数位DP。dp[i][j]表示i位,最高位为j的情况下总共有多少1.
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<map>
#include<queue>
#include<string>
#include<vector>
using namespace std; long long dp[][];
char s[]; void init()
{
memset(dp,,sizeof dp); long long num=; for(int i=; i<=; i++)
{
for(int j=; j<=; j++)
{
if(j==) dp[i][j]=num;
for(int s=; s<=; s++)
dp[i][j]=dp[i][j]+dp[i-][s];
}
num=num*;
} } int main()
{
init();
while(~scanf("%s",s))
{
int len=strlen(s);
long long n=;
for(int i=; s[i]; i++) n=n*+s[i]-'';
long long ans=; for(int i=; i<s[]-''; i++) ans=ans+dp[len][i];
for(int i=; s[i]; i++)
{
int d=len-i;
for(int j=; j<s[i]-''; j++) ans=ans+dp[d][j];
}
long long x=;
for(int i=; s[i]; i++)
{
x=x*+s[i]-'';
if(s[i]=='')
{
long long tmp=x;
for(int j=i+; s[j]; j++) tmp=tmp*;
ans=ans+n-tmp+;
}
}
printf("%lld\n",ans);
}
return ;
}
PAT (Advanced Level) 1049. Counting Ones (30)的更多相关文章
- PAT (Advanced Level) 1004. Counting Leaves (30)
简单DFS. #include<iostream> #include<cstring> #include<cmath> #include<algorithm& ...
- PAT 解题报告 1049. Counting Ones (30)
1049. Counting Ones (30) The task is simple: given any positive integer N, you are supposed to count ...
- PAT (Advanced Level) 1115. Counting Nodes in a BST (30)
简单题.统计一下即可. #include<cstdio> #include<cstring> #include<cmath> #include<vector& ...
- 【PAT甲级】1049 Counting Ones (30 分)(类似数位DP思想的模拟)
题意: 输入一个正整数N(N<=2^30),输出从1到N共有多少个数字包括1. AAAAAccepted code: #define HAVE_STRUCT_TIMESPEC #include& ...
- PAT (Advanced Level) 1111. Online Map (30)
预处理出最短路再进行暴力dfs求答案会比较好.直接dfs效率太低. #include<cstdio> #include<cstring> #include<cmath&g ...
- PAT (Advanced Level) 1107. Social Clusters (30)
简单并查集. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...
- PAT (Advanced Level) 1103. Integer Factorization (30)
暴力搜索. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...
- PAT (Advanced Level) 1072. Gas Station (30)
枚举一下选的位置,每次算一下就可以了. #include<cstdio> #include<cstring> #include<cmath> #include< ...
- PAT (Advanced Level) 1091. Acute Stroke (30)
BFS求连通块.递归会爆栈. #include<cstdio> #include<cstring> #include<cmath> #include<algo ...
随机推荐
- find排除目录
在linux find 进行查找的时候,有时候需要忽略某些目录不查找,可以使用 -prune 参数来进行过滤,但必须要注意要忽略的路径参数必须紧跟着搜索的路径之后,否则该参数无法起作用. 命令语法: ...
- 第3章 Java语言基础----成员变量与局部变量
在对局部变量进行赋值时,不能对非静态字段age进行静态引用,图1错误,加上static后图二正确,图3与图4类似,如下图所示: 图1图2 图3图4 2.成员变量times在类中定义,局部变量times ...
- FUSE
FUSE is particularly useful for writing [ vritual ] file system. Unlike traditional filesystem that ...
- php---数组序列化
有两种选择:serialize,json_encode. 需求:对数组进行序列化后保存在文件中,以便爬虫来抓取文件.并且序列化后的字符串只有一行,不希望在该字符串中出现换行,即使数组中某个元素中有换行 ...
- 客户端 HttpUtils.java
package com.http.post; import java.io.ByteArrayOutputStream; import java.io.IOException; import java ...
- JSP内置对象--4种属性范围 (pageContext,request,session,application)
pageContext: javax.servlet.jsp.JspContext抽象类的父类JspContext 中有以下方法. request:javax.servlet.http.的接口Http ...
- 快速批量插入sqlserver方法之我见
---------------------------------------------------------------------------------------------------- ...
- label不换行的问题
除了numberOfLines属性label有一个preferredMaXLayoutWidth属性.设置试试
- mvc动态生成a标签,多个属性,多个querystring
1*服务端 客户端 跳转的url 2*服务端 客户端 跳转的url 3*服务端 客户端 跳转的url 4*服务端 客户端 跳转的url
- 禁止root远程登录 sshd问题:A protocol error occurred. Change of username or service not allowed
在研究Linux安全的时候遇到一个问题,原本打算修改linux直接远程root登陆,修改为sshd的配置文件后 Nano /etc/ssh/sshd_config 把#PermitRootLogin ...