【第22期】观点:IT 行业加班,到底有没有价值?

Ural - 1057. Amount of Degrees

原创 2015年07月07日 12:01:22

1057. Amount of Degrees

Create a code to determine the amount of integers, lying in the set [X;Y] and being a sum of exactly K different integer degrees of B.
Example. Let X=15, Y=20, K=2, B=2. By this example 3 numbers are the sum of exactly two integer degrees of number 2:
17 = 24+20,
18 = 24+21,
20 = 24+22.
Input
The first line of input contains integers X and Y, separated with a space (1XY2311). The next two lines contain integers K and B (1K20;2B10).
Output
Output should contain a single integer — the amount of integers, lying between X and Y, being a sum of exactly K different integer degrees of B.
Sample
input output

15 20
2
2

3
思路:dp[cur][s][K]表示处理到当前状态,前面有s个不是1,一共需要K个不是1的,有多少

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=100;
int X,Y;
int K,B;
int dig[maxn];
int dp[maxn][20][25];
int dfs(int cur,int e,int s)
{
    if(cur<0)return s==K;
    if(s>K)return 0;
    if(!e&&dp[cur][s][K]!=-1)
        return dp[cur][s][K];
    int end=(e?min(1,dig[cur]):1);
    int ans=0;
    for(int i=0;i<=end;i++)
        ans+=dfs(cur-1,e&&i==dig[cur],s+(i!=0));//这里一定注意要是i==dig[cur],因为前面的end是变化过的
    if(!e)dp[cur][s][K]=ans;
    return ans;
}
int solve(int x)
{
    int len=0;
    memset(dp,-1,sizeof(dp));
    while(x)
    {
        dig[len++]=x%B;
        x/=B;
    }
    return dfs(len-1,1,0);
}
int main()
{
    while(scanf("%d%d",&X,&Y)!=EOF)
    {
        scanf("%d%d",&K,&B);
        printf("%d\n",solve(Y)-solve(X-1));
    }
    return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

Amount of Degrees URAL - 1057 数位dp

题意:求(x--y)区间转化为b进制以后, 1 的个数为 k 的数的出现次数,并且其余位上的数必须为0。 原题要求的是k个不同b次幂相加得到的不同的数的个数,所以每一位上的的数不能出现除1和0以外的...

Customize magento native captcha appearance. Change amount of lines and dots

1.rewrite core model in

URAL 1057 Amount of Degrees 数位dp

原题链接:URAL 1057 Amount of Degrees 做了前两道数位dp,作者就在想:“数位dp原来这么简单啊,连状态都是一个模式,都是dp[i][j]表示以j开头的i位数字,看来再刷一道...

URAL 1057 Amount of Degrees (数位统计)

转载请注明出处,谢谢<a href="http://blog.csdn.net/ACM_cxlove?

ural1057 Amount of Degrees 【数位dp】论文例题

URAL - 1057 Amount of Degrees Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO ...
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)