栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

zoj 3416 Balanced Number

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

zoj 3416 Balanced Number

#include <iostream>#include <iomanip>#include <sstream>#include <cstdio>#include <string>#include <vector>#include <algorithm>#include <complex>#include <cstring>#include <cstdlib>#include <cmath>#include <cassert>#include <climits>#include <queue>#include <set>#include <map>#include <valarray>#include <bitset>#include <stack>using namespace std;#define REP(i,n) for(int i=0;i<(int)n;++i)#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)#define ALL(c) (c).begin(), (c).end()#define chmax(a,b) (a<(b)?(a=b,1):0)#define chmin(a,b) (a>(b)?(a=b,1):0)#define valid(y,x,h,w) (0<=y&&y<h&&0<=x&&x<w)typedef long long ll;typedef pair<int,int> pii;const int INF = 1<<29;const double PI = acos(-1);const double EPS = 1e-8;ll dp[19][19][2][2][2000];char str[20];int n;ll dfs(int keta, int center, bool less, bool zero, int sum) { if (keta == n) { if (zero) return 0; return sum == 0; } ll &r = dp[keta][center][less][zero][sum]; if (r >= 0) return r; ll res = 0; REP(i,10) { if (!less && i>str[keta]-'0') break; bool nl = less; if (i != str[keta]-'0') nl = 1; int nsum = sum + (center-keta)*i; bool nz = zero; if (i) nz = 0; res += dfs(keta+1, center, nl, nz,nsum); } return r = res;}ll solve(ll a) { if (a == -1) return 0; memset(dp,-1,sizeof(dp)); sprintf(str, "%lld", a); n = strlen(str); ll res = 1;  REP(i,n) { res += dfs(0,i,0,1,0); } return res;}int main() { int T; scanf("%d", &T); while(T--) { ll a, b; cin >> a >> b; cout << solve(b) - solve(a-1) << endl; }}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/374982.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号