数位dp HDU 3709 Balanced Number
来源:互联网 发布:淘宝韩国服装代购 编辑:程序博客网 时间:2024/05/17 22:03
Balanced Number
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 1591 Accepted Submission(s): 696
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. When a pivot is placed at some digit of the number, the distance from a digit to the pivot is the offset between it and the pivot. Then the torques of left part and right part can be calculated. It is balanced if they are the same. A balanced number must be balanced with the pivot at some of its digits. For example, 4139 is a balanced number with pivot fixed at 3. The torqueses are 4*2 + 1*1 = 9 and 9*1 = 9, for left part and right part, respectively. It's your job
to calculate the number of balanced numbers in a given range [x, y].
to calculate the number of balanced numbers in a given range [x, y].
Input
The input contains multiple test cases. The first line is the total number of cases T (0 < T ≤ 30). For each case, there are two integers separated by a space in a line, x and y. (0 ≤ x ≤ y ≤ 1018).
Output
For each case, print the number of balanced numbers in the range [x, y] in a line.
Sample Input
20 97604 24324
Sample Output
10897
Author
GAO, Yuan
Source
2010 Asia Chengdu Regional Contest
题意:要你找出[x,y]的平衡数的数目。
思路:我们用 dp[i][j][k]表示i位数,对称中心为j,平衡情况为k的数有多少个,那么我们能得出递推式dp[i+1][j][k+(j-(i+1)*val]+=dp[i][j][k] 这个我用bfs来推的。因为k并不一定不是每一个都有,所以用for循环可能会有很多无用的状态。然后输出的话,套个dfs的模板就行了,最后要注意的是0被我们算了多次,要减去重复的。
代码:
#include<iostream>#include<cstdio>#include<cstring>#include<string.h>#include<queue>#include<algorithm>#include<cmath>using namespace std;const int maxn=2*2000+5;const int zero=2000;#define LL long longLL dp[20][20][maxn];char digit[20];int n;struct State{ int ps,pv,bal; State(int ps,int pv,int bal) :ps(ps),pv(pv),bal(bal) {}};bool vis[20][20][maxn];void pre_init(){ memset(dp,0,sizeof(dp)); queue<State> q; for(int p=0;p<=19;p++) { dp[0][p][zero]=1; q.push(State(0,p,zero)); } memset(vis,0,sizeof(vis)); while(q.size()) { State tmp=q.front(); q.pop(); int i=tmp.ps,j=tmp.pv,k=tmp.bal; // printf("%d %d %d: %lld\n",i,j,k,dp[i][j][k]); if(i==19) continue; for(int val=0;val<=9;++val) { int kk=k+(j-(i+1))*val; dp[i+1][j][kk]+=dp[i][j][k]; if(!vis[i+1][j][kk]) q.push(State(i+1,j,kk)); vis[i+1][j][kk]=true; } }}LL dfs(int cur,int pivot,int bal,bool limit){ if(!limit || cur==0) return dp[cur][pivot][2*zero-bal]; int tail=limit?digit[cur]-'0':9; LL ret=0; for(int val=0;val<=tail;++val) ret+=dfs(cur-1,pivot,bal+(pivot-cur)*val,limit&&val==tail); return ret;}LL Cal_Num(){ LL ret=0; for(int i=1;i<=n;++i) ret+=dfs(n,i,zero,true); return ret-(n-1);}void Test(){ int i,j,k; while(cin>>i>>j>>k) { k+=zero; cout<<dp[i][j][k]<<endl; }}int bal_cnt[100000];bool balance(int p,char * s,int n){ int val=0; for(int i=1;i<=n;++i) val+=(p-i)*(s[i]-'0'); return val==0;}bool is_bal(int x){ char s[1000]; sprintf(s+1,"%d",x); n=strlen(s+1); for(int pivot=1;pivot<=n;++pivot) if(balance(pivot,s,n)) return true; return false;}void Brute(){ bal_cnt[0]=1; for(int i=1;i<100000;++i) { bal_cnt[i]=bal_cnt[i-1]+(is_bal(i)); // printf("%d: %d\n",i,bal_cnt[i]); } int x; freopen("out.txt","w",stdout); for(int i=0;i<100000;++i) printf("%d: %d\n",i,bal_cnt[i]);}void GetData(){ freopen("in.txt","w",stdout); printf("1000000\n"); for(int i=0;i<1000000;++i) printf("0 %d\n",i);}int main(){ // GetData(); return 0;// freopen("in.txt","r",stdin);// freopen("out.txt","w",stdout); // Brute(); return 0; pre_init(); // Test(); int T;cin>>T; int cnt=0; while(T--) { LL x; scanf("%I64d",&x); LL ans=0; if(x>0) { --x; sprintf(digit+1,"%I64d",x); n=strlen(digit+1); reverse(digit+1,digit+1+n); ans=Cal_Num(); } scanf("%s",digit+1); n=strlen(digit+1); reverse(digit+1,digit+1+n); ans = Cal_Num()-ans; printf("%I64d\n",ans); }}
0 0
- HDU 3709 Balanced Number(数位DP)
- 数位dp HDU 3709 Balanced Number
- hdu 3709 Balanced Number (数位dp)
- hdu 3709 Balanced Number(数位dp)
- Balanced Number - HDU 3709 数位dp
- [数位dp] hdu 3709 Balanced Number
- 【HDU】3709 Balanced Number 数位DP
- HDU 3709 Balanced Number (数位DP)
- HDU 3709 Balanced Number(数位DP)
- 【数位DP】 HDU 3709 Balanced Number
- hdu 3709Balanced Number(数位dp)
- HDU 3709 Balanced Number (简单数位DP)
- HDU 3709 Balanced Number (数位DP)
- HDU 3709 Balanced Number (数位dp)
- 数位DP-HDU-3709-Balanced Number
- HDU 3709 Balanced Number(数位dp)
- HDU 3709 Balanced Number(数位dp)
- HDU 3709Balanced Number 数位dp
- 浙江发布一批人事任免名单
- Android四大组件之BroadcastReceiver(2)
- JavaScript HTML DOM - 改变 CSS
- android :webview 解决遇到的跨域问题
- linux 资源竞争 引起的 阻塞 休眠 以及 驱动部分的工作
- 数位dp HDU 3709 Balanced Number
- poj 4588 Count The Carries(数论)
- 软件开发过程中的浪费——详细设计
- iOS ARC详解
- ReferenceEquals(), static Equals(), instance Equals(), 和运算行符==之间的关系
- 当我路过你的忧伤
- 大学计算机软件专业生应该学什么?(转)
- 余凯 在清华的讲座笔记
- vs2010打包系统必备选择.net framework 3.5sp1编译错误的解决方法