• HDU_4734_F(x)_数位dp


    HDU_4734_F(x)_数位dp

    F(x)

    Time Limit: 1000/500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 15070    Accepted Submission(s): 5761


     

    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
     

    Source

    // 如果对于每一个f(a)都求一遍答案 空间复杂度受不了 O( 9*4600*4600 )

    // dp[f(a)][now][sum]: 在约束==f(a)时 当前数位进行到now位 now+1~pos位的加权和为sum

    // 于是考虑做减法

    // dp[now][re]: 当前进行到了now位 剩余量为re

    // 对于不同的f(a) 只要最后的剩余量re>=0 就说明f(a)>=sum 当前dp遍历的所有数字都是答案

    1. // 确定上限
    2. #include
    3. using namespace std;
    4. int main()
    5. {
    6. int sum=0;
    7. for( int i=0;i<9;i++ )
    8. {
    9. sum+=9*( 1<
    10. }
    11. cout<// 4599
    12. cout<<( 1<<14 )<
    13. return 0;
    14. }
    15. // A,B < 1e9 --> 9个9 --> 4599
    16. // 如果对于每一个f(a)都求一遍答案 空间复杂度受不了 O( 9*4600*4600 )
    17. // dp[f(a)][now][sum]: 在约束==f(a)时 当前数位进行到now位 now+1~pos位的加权和为sum
    18. // 于是考虑做减法
    19. // dp[now][re]: 当前进行到了now位 剩余量为re
    20. // 对于不同的f(a) 只要最后的剩余量re>=0 就说明f(a)>=sum 当前dp遍历的所有数字都是答案
    21. // HDU_4734_F(x)_数位dp
    22. #include
    23. using namespace std;
    24. #define int long long
    25. const int N=11;
    26. int dp[N][11111];
    27. int in[N];
    28. int x,y;
    29. int stdd;
    30. int dfs( int now,int re,bool limit )
    31. {
    32. if( re<0 ) return 0;
    33. if( now<=0 ) return ( re>=0 ) ;
    34. if( limit==0 && ~dp[now][re] ) return dp[now][re];
    35. int up=limit ? in[now] : 9 ;
    36. int ans=0;
    37. for( int i=0;i<=up;i++ )
    38. ans+=dfs( now-1,re-i*( 1<<(now-1) ),limit && i==in[now] );
    39. //
    40. if( limit==0 ) dp[now][re]=ans;
    41. return ans;
    42. }
    43. int f( int n )
    44. {
    45. int pos=0;
    46. while( n ) in[++pos]=n%10,n/=10;
    47. return dfs( pos,stdd,1 );
    48. }
    49. void f_stdd( int n )
    50. {
    51. stdd=0;
    52. int r=1;
    53. while( n ) stdd+=n%10*r,n/=10,r<<=1;
    54. }
    55. signed main()
    56. {
    57. memset( dp,-1,sizeof( dp ) );
    58. int t,i,x,y;
    59. scanf("%lld",&t);
    60. for( i=1;i<=t;i++ )
    61. {
    62. scanf("%lld%lld",&x,&y);
    63. f_stdd( x );
    64. printf("Case #%lld: %lld\n",i,f(y) );
    65. }
    66. return 0;
    67. }

  • 相关阅读:
    【Java 数据结构】栈与OJ题
    数据结构——树形结构
    .Net下的Http请求调用(Post与Get)
    如何在Win系统部署Tomcat服务并实现远程访问内网站点
    uniapp上下滑屏切换支持视频和图片轮播实现,类似抖音效果
    Solidus Labs欢迎香港前金融创新主管赵嘉丽担任战略顾问
    简要归纳UE5 Lumen全局光照原理
    【CV】Reg2Net:一种用于计算机视觉任务的多尺度骨干架构
    第六届“强网杯”全国网络安全挑战赛-青少年专项赛
    6 寻找比目标字母大的最小字母
  • 原文地址:https://blog.csdn.net/qq_63173957/article/details/126892559