ZOJ-The 14th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple-E
来源:互联网 发布:网络售彩最新消息官方 编辑:程序博客网 时间:2024/06/06 03:35
ACM模版
描述
题解
典型的数位 dp,没什么太多可说的,注意前缀 0 不需要剔除。
代码
#include <cstdio>#include <iostream>using namespace std;typedef long long ll;const int LEN = 8;const int MAXN = LEN + 5;const int EC[] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6, 6, 5, 4, 5, 5, 4};const ll INF = 0xFFFFFFFF;const ll POW[] = {1, 16, 256, 4096, 65536, 1048576, 16777216, 268435456};char s[MAXN];ll d[MAXN];ll dp[MAXN]; // 前x位中1出现次数(0~9均是如此)void init(){ for (int i = 1; i < MAXN; i++) { dp[i] = dp[i - 1] * 16 + POW[i - 1]; }}int HtoD(char ch){ if (ch >= '0' && ch <= '9') { return ch - '0'; } else { return ch - 'A' + 10; }}ll count(int place) // 数是x,查询的数位是place{ ll result = 0; ll tail = 0; for (int i = 0; i < LEN; i++) { if (d[i] > place) { result += POW[i] + d[i] * dp[i]; } else if (d[i] == place) { result += tail + 1 + d[i] * dp[i]; } else if (d[i] < place) { result += d[i] * dp[i]; } tail = tail + d[i] * POW[i]; // 头为place个数 } return result;}ll solve(ll x){ if (x < 0) { return 0; } ll res = 0; for (int i = 0; i < LEN; i++) { d[i] = x % 16; x /= 16; } for (int i = 0; i < 16; i++) { res += count(i) * EC[i]; } return res;}int main(){ init(); int T; scanf("%d", &T); int n; while (T--) { scanf("%d%s", &n, s); ll num = 0; for (int i = LEN - 1; i >= 0; i--) { num += HtoD(s[i]) * POW[LEN - i - 1]; } ll num_ = num + n - 1, temp = 0; if (num_ > INF) { temp = solve(INF) - solve(num - 1); num = 0; num_ -= INF + 1; } printf("%lld\n", temp + solve(num_) - solve(num - 1)); } return 0;}
参考
51Nod 1042 数字0-9的数量
0 0
- ZOJ-The 14th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple-E
- ZOJ-The 14th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple-A~B
- ZOJ-The 14th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple-C
- ZOJ-The 14th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple-D
- The 14th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple(A+B+C+D)
- The 14th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple C题What Kind of Fri
- The 14th Zhejiang Provincial Collegiate Programming Contest (待续)
- The 17th Zhejiang University Programming Contest Sponsored by TuSimple 浙大校赛C题 How Many Nines
- ZOJ 146 - The 13th Zhejiang Provincial Collegiate Programming Contest - C Defuse the Bomb
- The 7th Zhejiang Provincial Collegiate Programming Contest
- The 6th Zhejiang Provincial Collegiate Programming Contest
- The 8th Zhejiang Provincial Collegiate Programming Contest / L - Javabeans
- The 8th Zhejiang Provincial Collegiate Programming Contest / M - Median
- The 8th Zhejiang Provincial Collegiate Programming Contest
- The 9th Zhejiang Provincial Collegiate Programming Contest
- The 5th Zhejiang Provincial Collegiate Programming Contest 部分题解
- The 10th Zhejiang Provincial Collegiate Programming Contest Solution
- The 11th Zhejiang Provincial Collegiate Programming Contest---Talented Chef
- IDR机制
- 学习structs2第一天
- MAC下PHP开发环境教程
- 重构-ExtractClass
- Python下字符串的创建和转义字符的使用
- ZOJ-The 14th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple-E
- C语言数组小程序
- java学习笔记
- 嵌入式系统的组成
- Android中使用WebView显示网页
- 新闻列表中如何让前几篇标题颜色和其他标题颜色不一样?
- wangEditer 总结回顾
- SQLServer常用的字符串函数
- pthread_cond_broadcast使用示例