码迷,mamicode.com
首页 > 其他好文 > 详细

F(x)

时间:2017-02-11 23:57:46      阅读:380      评论:0      收藏:0      [点我收藏+]

标签:i++   ima   cal   algo   with   string   others   ++   make   

F(x)

Time Limit: 1000/500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Problem Description
For a decimal number x with n digits (AnAn-1An-2 ... A2A1), we define its weight as F(x) = An * 2n-1 + An-1 * 2n-2 + ... + A2 * 2 + A1 * 1. Now you are given two numbers A and B, please calculate how many numbers are there between 0 and B, inclusive, whose weight is no more than F(A).
 
Input
The first line has a number T (T <= 10000) , indicating the number of test cases.
For each test case, there are two numbers A and B (0 <= A,B < 109)
 
Output
For every case,you should output "Case #t: " at first, without quotes. The t is the case number starting from 1. Then output the answer.
 
Sample Input
3 0 100 1 10 5 100
 
Sample Output
Case #1: 1 Case #2: 2 Case #3: 13
分析:数位dp,注意算和时先把F(A)加上,这样可以永久化记忆;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <bitset>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define sys system("pause")
const int maxn=1e5+10;
const int N=5e4+10;
const int M=N*10*10;
using namespace std;
inline ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
inline ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
inline void umax(ll &p,ll q){if(p<q)p=q;}
inline void umin(ll &p,ll q){if(p>q)p=q;}
inline ll read()
{
    ll x=0;int f=1;char ch=getchar();
    while(ch<0||ch>9){if(ch==-)f=-1;ch=getchar();}
    while(ch>=0&&ch<=9){x=x*10+ch-0;ch=getchar();}
    return x*f;
}
int n,m,k,t,num[20],pos,cas;
ll dp[10][5000];
ll dfs(int pos,int x,int y)
{
    if(pos<0)return x>=0;
    if(x<0)return 0;
    if(y&&dp[pos][x]!=-1)return dp[pos][x];
    int now=y?9:num[pos],i;
    ll ret=0;
    rep(i,0,now)
    {
        ret+=dfs(pos-1,x-qpow(2,pos)*i,y||i<num[pos]);
    }
    return y?dp[pos][x]=ret:ret;
}
ll gao(int p)
{
    pos=0;
    while(p)num[pos++]=p%10,p/=10;
    return dfs(pos-1,n,0);
}
int main()
{
    int i,j;
    memset(dp,-1,sizeof(dp));
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        k=j=0;
        while(n)j+=qpow(2,k++)*(n%10),n/=10;
        n=j;
        printf("Case #%d: %lld\n",++cas,gao(m));
    }
    return 0;
}

F(x)

标签:i++   ima   cal   algo   with   string   others   ++   make   

原文地址:http://www.cnblogs.com/dyzll/p/6390259.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!