AtCoder ABC 154E Almost Everywhere Zero

题目链接:https://atcoder.jp/contests/abc154/tasks/abc154_e

题目大意

  给定一个整数N($1 \leq N \leq 10^{100}$)和K($1 \leq K \leq 3$),求[1, N]区间内数位上只有K个非零数的整数个数。

分析

  找一下规律即可,详情见代码注释。

代码如下

  1 #include <bits/stdc++.h>
  2 using namespace std;
  3
  4 /*-------------------Define Start-------------------*/
  5 typedef bool BL;                        // 布尔类型
  6 typedef char SB;                        // 有符号1字节,8位
  7 typedef unsigned char UB;                // 无符号1字节,8位
  8 typedef short SW;                        // 有符号短整型,16位
  9 typedef unsigned short UW;                // 无符号短整型,16位
 10 typedef long SDW;                        // 有符号整型,32位
 11 typedef unsigned long UDW;               // 无符号整型,32位
 12 typedef long long SLL;                    // 有符号长整型,64位
 13 typedef unsigned long long ULL;            // 无符号长整型,64位
 14 typedef char CH;                        // 单个字符
 15 typedef float R32;                        // 单精度浮点数
 16 typedef double R64;                        // 双精度浮点数
 17
 18 #define Rep(i, n) for (register SDW i = 0; i < (n); ++i)
 19 #define For(i, s, t) for (register SDW i = (s); i <= (t); ++i)
 20 #define rFor(i, t, s) for (register SDW i = (t); i >= (s); --i)
 21 #define foreach(i, c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
 22 #define ms0(a) memset(a,0,sizeof(a))
 23 #define msI(a) memset(a,0x7f,sizeof(a))
 24 #define LOWBIT(x) ((x)&(-x))
 25
 26 #define MP make_pair
 27 #define PB push_back
 28 #define ft first
 29 #define sd second
 30
 31 #define pr(x) cout << #x << " = " << x << "  "
 32 #define prln(x) cout << #x << " = " << x << endl
 33
 34 const ULL mod = 1e9 + 7;                //常用模数(可根据题目需要修改)
 35 const ULL inf = 0x7fffffff;                //用来表示无限大
 36 const ULL infLL = 0x7fffffffffffffffLL;    //用来表示无限大
 37 /*-------------------Define End-------------------*/
 38
 39 const UDW maxN = 1e6 + 7;
 40 string N;
 41 SLL len;
 42 SDW K;
 43 SLL ans;
 44
 45 void input(){
 46     cin >> N >> K;
 47     len = N.size();
 48 }
 49
 50 void do1() {
 51     SDW a = N[0] - ‘0‘;
 52     SLL posA = len; // 从1开始从右往左数a在第几个
 53
 54     // 1 ~ len - 1位数的情况
 55     ans += 9 * (posA - 1);
 56     // len位数的情况
 57     ans += a;
 58 }
 59
 60 void do2() {
 61     SDW a = N[0] - ‘0‘;
 62     SLL posA = len; // 从1开始从右往左数a在第几个
 63
 64     SDW b = 0; // b为从左往右数第二个非零的数,不存在就置为0,然后位置设为1
 65     SLL posB = 1;
 66
 67     For(i, 1, len - 1) {
 68         if(N[i] != ‘0‘) {
 69             b = N[i] - ‘0‘;
 70             posB = len - i;
 71             break;
 72         }
 73     }
 74
 75     // 1 ~ len - 1位数的情况
 76     ans += 9 * (posA - 1) * (posA - 2) * 9 / 2;
 77     // len位数的情况
 78     ans += (a - 1) * (posA - 1) * 9;            // posA上置 1 ~ a-1
 79     ans += (posB - 1) * 9;                        // posA上置 a, posB上置 0
 80     ans += b;                                    // posA上置 a, posB上置 1 ~ b
 81 }
 82
 83 void do3() {
 84     SDW a = N[0] - ‘0‘;
 85     SLL posA = len; // 从1开始从右往左数a在第几个
 86
 87     SDW b = 0; // b为从左往右数第二个非零的数,不存在就置为0,然后位置设为1
 88     SLL posB = 1;
 89
 90     For(i, 1, len - 1) {
 91         if(N[i] != ‘0‘) {
 92             b = N[i] - ‘0‘;
 93             posB = len - i;
 94             break;
 95         }
 96     }
 97
 98     SDW c = 0; // c为从左往右数第三个非零的数,不存在就置为0,然后位置设为1
 99     SLL posC = 1;
100
101     For(i, len - posB + 1, len - 1) {
102         if(N[i] != ‘0‘) {
103             c = N[i] - ‘0‘;
104             posC = len - i;
105             break;
106         }
107     }
108
109     // 1 ~ len - 1位数的情况 (朱世杰恒等式)
110     ans += 9 * 9 * 9 * (posA - 1) * (posA - 2) * (posA - 3) / 6;
111     // len位数的情况
112     ans += (a - 1) * 9 * 9 * (posA - 1) * (posA - 2) / 2;     // posA上置 1 ~ a-1
113     ans += 9 * 9 * (posB - 1) * (posB - 2) / 2;             // posA上置a, posB上置 0
114     ans += (b - 1) * (posB - 1) * 9;                         // posA上置a, posB上置 1 ~ b-1
115     ans += (posC - 1) * 9;                                    // posA上置a, posB上置 b, posC上置0
116     ans += c;                                                // posA上置a, posB上置 b, posC上置1~c
117 }
118
119 void solve(){
120     switch(K) {
121         case 1:{
122             do1();
123             break;
124         }
125         case 2:{
126             do2();
127             break;
128         }
129         case 3:{
130             do3();
131             break;
132         }
133         default:{
134             assert(false);
135         }
136     }
137 }
138
139 void output(){
140     cout << ans << endl;
141 }
142
143 int main() {
144     input();
145     solve();
146     output();
147     return 0;
148 }

原文地址:https://www.cnblogs.com/zaq19970105/p/12298255.html

时间: 2024-07-31 12:36:37

AtCoder ABC 154E Almost Everywhere Zero的相关文章

AtCoder ABC 129F Takahashi&#39;s Basics in Education and Learning

题目链接:https://atcoder.jp/contests/abc129/tasks/abc129_f 题目大意 给定一个长度为 L ,首项为 A,公差为 B 的等差数列 S,将这 L 个数拼起来,记作 N,求 N % M. 分析 设 bit(i) 为第 i 项所需要进行的十进制位移. 则 $N = S_0 * 10^{bit(0)} + S_1 * 10^{bit(1)} + \dots + S_{L - 1} * 10^{bit(L - 1)}$. 一项一项地算是肯定要超时的,不过注意

Atcoder ABC 141

Atcoder ABC 141 A - Weather Prediction SB题啊,不讲. #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; char ch[50]; int main() { scanf("%s",ch+1); if(ch[1] == 'S') puts("Cloudy

题解 [Atcoder ABC 161] A,B,C

题解 [Atcoder ABC 161] A,B,C A: 水题,按题意模拟即可. code: #include<bits/stdc++.h> #define ft(i,l,r) for(register int i=l;i<=r;i++) #define fd(i,r,l) for(register int i=r;i>=l;i--) using namespace std; int a,b,c; int main() { cin>>a>>b>>

AtCoder ABC 127F Absolute Minima

题目链接:https://atcoder.jp/contests/abc127/tasks/abc127_f 题目大意 初始状态下$f(x) = 0$,现在有 2 种模式的询问,第一种以“1 a b”的形式,需要进行操作$f(x) = f(x) + |x - a| + b$:第二种以“2”的形式,求使得 f(x) 取得最小值的 x 取值和 f(x) 值,如果有多个 x,输出任意一个即可. 分析 考虑第一种询问已经出现了 k 次,现在遇到第二种询问.此时$f(x) = \sum_{i = 1}^k

AtCoder ABC 130F Minimum Bounding Box

题目链接:https://atcoder.jp/contests/abc130/tasks/abc130_f 题目大意 给定地图上 N 个点的坐标和移动方向,它们会以每秒 1 个单位的速度移动,设 Ans(t) 为在 t 时刻,$(x_{max} - x_{min}) * (y_{max} - y_{min})$的值,求 Ans(t) 的最小值.(最小值可能不是一个整数) 分析 稍加思考可以发现,不是所有点的所有坐标都对答案有影响,很多点完全可以忽略不计,下面以 Y 坐标为例,讨论影响$(y_{

AtCoder ABC 155D Pairs

题目链接:https://atcoder.jp/contests/abc155/tasks/abc155_d 题目大意 给定$N$个整数$A_1, A_2, \dots, A_N$, 求集合$S = \{A_i * A_j | 1 \leq i, j \leq N 且 i \neq j\}$的第$K$小. 分析 首先,通过把$N$个数分为正数,负数,零,再排下序,我们可以计算$S$中所有的正数,负数以及零的数目,进而可以判断出第$K$小的数是正数,是负数,还是零. 如果第$K$小的数是零,无需多

AtCoder ABC 155F Perils in Parallel

题目链接:https://atcoder.jp/contests/abc155/tasks/abc155_f 题目大意 分析 代码如下 原文地址:https://www.cnblogs.com/zaq19970105/p/12340031.html

AtCoder ABC 157E Simple String Queries

题目链接:https://atcoder.jp/contests/abc157/tasks/abc157_e 题目大意 给定一串全由小写英文字母组成的字符串,然后顺序给出$Q$个操作,一种为替换字符串中的某个字符:另一种为查询字符串某个区间里面有多少个不同的字符.要求顺序输出第二种操作的结果. 分析 线段树单点更新,每个节点存一个32位整数,其中26位代表26个英文字母,更新的话只要节点求或就行了. 其他方法1:用26个树状数组,这个原理是一样的,但总感觉没有只用一棵线段树简明. 其他方法2:把

AtCoder ABC 158F Removing Robots

题目链接:https://atcoder.jp/contests/abc158/tasks/abc158_f 题目大意 有$N$个机器人分布在数轴上不同的位置,初始为未激活状态,作为上帝,你可以手动激活任意数量的机器人,当第$i$个机器人被激活时,它会向前走$D_i$个单位长度然后自爆,并且坐标区间在$[X_i,~Di)$上的机器人也会被激活往前走,从而触发连锁激活. 当你选定一些机器人激活后,最后剩下的机器人按编号组成集合$S$,问一共有多少个不同的集合$S$? 分析 对于每一个机器人,无非有