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 ...
随机推荐
- 使用CSS灵活的盒子
CSS3灵活的盒子,或flexbox,是一个布局模式提供页面上的元素的安排这样的元素表现可以预见当页面布局必须适应不同屏幕大小和不同的显示设备.对于许多应用程序,灵活的块盒模型提供了一个改进模型,它不 ...
- Chapter 1 First Sight——28
"Which one is the boy with the reddish brown hair?" 那个红褐色头发的男孩是谁? I asked. I peeked at him ...
- hdu_2227_Find the nondecreasing subsequences_树状数组,离散化
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 题意:给你一个集合,让你求递增子序列有多少个,和树状数组求逆序对差不多,不过数据比较大,要离散化 ...
- hdu_3562_B-number(记忆化搜索|数位DP)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3652 题意:给你一个n,为比n小的能整除13并数字中有13的数有多少个 题解:记忆化搜索:记dp[i] ...
- Entity Framework技巧系列之十三 - Tip 51 - 55
提示51. 怎样由任意形式的流中加载EF元数据 在提示45中我展示了怎样在运行时生成一个连接字符串,这相当漂亮. 其问题在于它依赖于元数据文件(.csdl .ssdl .msl)存在于本地磁盘上. 但 ...
- Entity Framework技巧系列之三 - Tip 9 – 12
提示9. 怎样直接删除一个对象而无需检索它 问题 最常见的删除Entity Framework中实体的方式是将你要删除的实体传入Context中并像如下这样删除: 1 // 按ID查找一个类别 2 / ...
- 使用jquery的js的页面实现例子
var validate;var nodeId="";var modifyappId=""; addExportTaskURL = ctx + "/x ...
- Note over Chinese Encodings
I been confused years ago. Till recently I collected my thoughts together, and now I am clear about ...
- div.2/D. As Fast As Possible<数学题,二分>
题目连接 题意: n个学生出去玩,要前进一段距离,租了一辆可以载k个人的车,问到达到目的地的最短时间. cin: n,l,v1,v2,k. £:所有人一起到达终点的时候时间最短. £:所有人走路和坐车 ...
- UIViewContentMode 图文解说
在iOS应用开发中我们常常要对视图的contentMode属性进行设置,尤其在使用UIImageView视图时设置这个属性的概率很高.我们知道contentMode的类型是UIViewContentM ...