ural 1057 Amount of degrees 【数位dp】
题意:求(x--y)区间转化为 c 进制 1 的个数为 k 的数的出现次数。
分析:发现其满足区间减法,所以能够求直接求0---x 的转化为 c 进制中 1 的个数为k的数的出现次数。
首先用一个数组f【i】【j】:表示前 i 位中有 j 位为 1 的个数。
能够通过方程 f【i】【j】 = f【i-1】【j】 + f【i-1】【j-1】来预处理出来。
对于要求的答案,我们能够借助树来求。
假如13 。2进制,有3个1 。转化为2进制 1101
能够借助于一个二进制的表示的树来求。
AC代码:
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <map>
#include <string>
#include <iostream>
#include <cmath>
using namespace std;
#define Del(a,b) memset(a,b,sizeof(a))
typedef long long LL;
const double esp = 1e-10;
const int N = 50;
int f[N][N]; //f[i][j] 前i个中选j个1的个数
void isit()
{
f[0][0] = 1;
for(int i=1;i<33;i++){
f[i][0] = f[i-1][0];
for(int j=1;j<=i;j++)
f[i][j] = f[i-1][j] + f[i-1][j-1];
}
}
int solve(int x,int k,int c)
{
vector<int> v;
while(x)
{
v.push_back(x%c);
x/=c;
}
int cnt = 0,ans = 0;
for(int i=v.size()-1;i>=0;i--)
{
if(v[i]==1) //为1,则依次求解
{
ans+=f[i][k-cnt];
cnt++;
if(cnt==k)
break;
}
else if(v[i]>1) //假如大于1的话。相当于全部的位有能够为1,所以直接求解跳出
{
ans += f[i+1][k-cnt];
break;
}
}
if(cnt==k)
ans++;
return ans;
}
int main()
{
isit();
int x,y,k,c;
while(~scanf("%d%d%d%d",&x,&y,&k,&c))
{
printf("%d\n",solve(y,k,c)-solve(x-1,k,c));
}
return 0;
}
ural 1057 Amount of degrees 【数位dp】的更多相关文章
- URAL 1057. Amount of Degrees(数位DP)
题目链接 我看错题了...都是泪啊,不存在3*4^2这种情况...系数必须为1... #include <cstdio> #include <cstring> #include ...
- [ACM] ural 1057 Amount of degrees (数位统计)
1057. Amount of Degrees Time limit: 1.0 second Memory limit: 64 MB Create a code to determine the am ...
- Ural 1057 Amount of Degrees
Description 问[L,R]中有多少能表示k个b次幂之和. Sol 数位DP. 当2进制时. 建出一个二叉树, \(f[i][j]\) 表示长度为 \(i\) 有 \(j\) 个1的个数. 递 ...
- Ural1057 - Amount of Degrees(数位DP)
题目大意 求给定区间[X,Y]中满足下列条件的整数个数:这个数恰好等于K个互不相等的B的整数次幂之和.例如,设X=15,Y=20,K=2,B=2,则有且仅有下列三个数满足题意: 输入:第一行包含两个整 ...
- URAL 1057 Amount of Degrees (数位dp)
Create a code to determine the amount of integers, lying in the set [X;Y] and being a sum of exactly ...
- URAL 1057 Amount of Degrees (数位DP,入门)
题意: 求给定区间[X,Y]中满足下列条件的整数个数:这个数恰好等于K个互不相等的,B的整数次幂之和.例如,设X=15,Y=20,K=2,B=2,则有且仅有下列三个数满足了要求: 17 = 24+2 ...
- [ural1057][Amount of Degrees] (数位dp+进制模型)
Discription Create a code to determine the amount of integers, lying in the set [X; Y] and being a s ...
- Timus Online Judge 1057. Amount of Degrees(数位dp)
1057. Amount of Degrees Time limit: 1.0 second Memory limit: 64 MB Create a code to determine the am ...
- ural 1057Amount of Degrees ——数位DP
link:http://acm.timus.ru/problem.aspx?space=1&num=1057 论文: 浅谈数位类统计问题 刘聪 #include <iostream&g ...
随机推荐
- JSP 基础之 JSTL <c:choose>用法 if else
<c:choose> <c:when test="${condition1}"> condition1为true </c:when> <c ...
- shell里面比较大小
#!/bin/bashif [ $1 -gt $2 ]then echo "$1>$2"else echo "$2>$1"fi# 数字判断一些命令 ...
- java 单元测试框架
@Test:测试方法(A) (expected=XXEception.class)(B) (timeout=xxx)@Ignore: 被忽略的测试方法. //该方法 不会执行@Before: 每一个测 ...
- mysql主从怎么样使主为innodb辅为myisam
MySQL主从复制(linux主+windows从) http://blog.csdn.net/qq_20032995/article/details/54380290 mysql主从怎么样使主为in ...
- [BZOJ2555]SubString LCT+后缀自动机
2555: SubString Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 3253 Solved: 975[Submit][Status][Di ...
- 【cocos2d-js官方文档】十七、事件分发机制
简介http://blog.csdn.net/qinning199/article/details/41951517 游戏开发中一个很重要的功能就是交互,如果没有与用户的交互,那么游戏将变成动画,而处 ...
- C++-二维vector初始化大小方法-备忘
来源: C++——二维vector初始化大小方法 1.直接用初始化方法 名字为vec,大小为n*m,初始值为0的二维vector. vector<vector<)); 2.用resize( ...
- 链式前向星写法下的DFS和BFS
Input 5 7 1 2 2 3 3 4 1 3 4 1 1 5 4 5 output 1 5 3 4 2 #include<bits/stdc++.h> using namespace ...
- POJ2342 Anniversary party(动态规划)(树形DP)
Anniversary party Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6635 Accepted: 3827 ...
- ( 转 ) .net 操作 JWT
GitHub: https://github.com/jwt-dotnet/jwt 1.JWT定义 JWT(Json Web Token)是一种用于双方之间传递安全信息的简洁的.URL安全的表述性声明 ...