Balanced Number HDU - 3709 数位dp

题意: 给出范围 算出 满足  选取一个数中任一一个 树作为支点  两边的数分别乘以到中心的距离和 左和等于右和   的数有多少个

数位DP题 状态转移方程为dp[pos][x][state]=dp[pos-1][x][state-(pos-x)*i]  表示为pos位上的数字为 i    以x为支点  则  以中心点左为负右为正   pos左右的数乘以权值的 和为state pos-1位就是 把pos位的 i乘以权值减去   即 state-(pos-x)*i 如果枚举到最后一位的时候 state==0就说明平衡了 其中 0 00  000  000 ......也是平衡的  出了0其他都是非法的要减去

#include <cstdio>
#include <cmath>
#include <algorithm>
#include<vector>
#include<iostream>
#include<cstring>
using namespace std;
typedef long long ll;
const int MOD=2520;
int cnt;
ll a[100];
int t[300];
ll dp[20][20][3000];

ll dfs(int pos,int x,int state,bool limit ){
    if(pos==-1)return !state;
    if(!limit&&dp[pos][x][state]!=-1)return  dp[pos][x][state];
    int up=limit?a[pos]:9;
    ll ans=0;
    for(int i=0;i<=up;i++){
        ans+=dfs(pos-1,x,state+i*(pos-x),limit&&up==i);
    }
    if(!limit) dp[pos][x][state]=ans;
    return ans;
}

ll solve(ll x){
  int pos=0;
  while(x){
      a[pos++]=x%10;
      x/=10;
  }
  ll ans=0;
  for(int i=0;i<pos;i++)ans+=dfs(pos-1,i,0,1);
return ans-pos+1;//  减去00 000 000 其中单0算 所以加一
}

int main()
{
ll a,b;
int t;
cin>>t;
    memset(dp,-1,sizeof(dp));
    while(t--){
        cin>>a>>b;
        printf("%I64d\n",solve(b)-solve(a-1));
    }

    return 0;
}

原文地址:https://www.cnblogs.com/ttttttttrx/p/9749037.html

时间: 2024-11-03 20:31:34

Balanced Number HDU - 3709 数位dp的相关文章

hdu 3709 数位dp(小思维)

http://acm.hdu.edu.cn/showproblem.php?pid=3709 Problem Description A balanced number is a non-negative integer that can be balanced if a pivot is placed at some digit. More specifically, imagine each digit as a box with weight indicated by the digit.

HDU 3709 数位dp

Balanced Number Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 5276    Accepted Submission(s): 2523 Problem Description A balanced number is a non-negative integer that can be balanced if a pi

[zoj 3416/hdu 3709]数位DP

题意:求从区间[L, R]内有多少个数是平衡数,平衡数是指以10进制的某一位为中心轴,左右两边的每一位到中心轴的距离乘上数位上的值的和相等.0<=L<=R<=1e18 思路:由于任何非0正数最多只有1个位置作为中心轴使得它是平衡数.于是可以按中心轴的位置分类统计答案.令dp[p][i][j]表示中心轴在p位(p>=0)前i位且左边比右边的加权和已经多j的方案数,枚举当前第i位放的数k,那么dp[p][i][j]=∑dp[p][i-1][j+(p-i+1)*k]. 求出dp值后,只需

hdu 4734 数位dp

http://acm.hdu.edu.cn/showproblem.php?pid=4734 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, plea

hdu 4352 数位dp(最长上升子序列的长度为k的个数)

http://acm.hdu.edu.cn/showproblem.php?pid=4352 Problem Description #define xhxj (Xin Hang senior sister(学姐)) If you do not know xhxj, then carefully reading the entire description is very important. As the strongest fighting force in UESTC, xhxj grew

hdu 4352 数位dp + 状态压缩

XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2265    Accepted Submission(s): 927 Problem Description #define xhxj (Xin Hang senior sister(学姐)) If you do not know xhxj, then careful

hdu 4507 数位dp(求和,求平方和)

http://acm.hdu.edu.cn/showproblem.php?pid=4507 Problem Description 单身! 依然单身! 吉哥依然单身! DS级码农吉哥依然单身! 所以,他生平最恨情人节,不管是214还是77,他都讨厌! 吉哥观察了214和77这两个数,发现: 2+1+4=7 7+7=7*2 77=7*11 最终,他发现原来这一切归根到底都是因为和7有关!所以,他现在甚至讨厌一切和7有关的数! 什么样的数和7有关呢? 如果一个整数符合下面3个条件之一,那么我们就说

SPOJ BALNUM Balanced Numbers 状压+数位DP

一开始想了一个用二进制状压的方法,发现空间需要的太大,光光memset都要超时 = = 其实不用每次都memset 也可以用三进制,一开始直接打表出所有的状态转移就好 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream&g

hdu 5179 数位dp

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5179 beautiful number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 198    Accepted Submission(s): 116 Problem Description Let A=∑ni=1ai?10n?i(1