EOJ Monthly 2019.2 (based on February Selection) D.进制转换

题目链接:

  https://acm.ecnu.edu.cn/contest/140/problem/D/

题目:

思路:

  我们知道一个数在某一个进制k下末尾零的个数x就是这个数整除kx,这题要求刚好末尾有m个0,还需要除去高位为0的情况,因此这题答案就是r / kx-(l-1)/kx-(r/kx+1-(l-1)/kx+1)。

代码实现如下:

 

 1 #include <set>
 2 #include <map>
 3 #include <deque>
 4 #include <queue>
 5 #include <stack>
 6 #include <cmath>
 7 #include <ctime>
 8 #include <bitset>
 9 #include <cstdio>
10 #include <string>
11 #include <vector>
12 #include <cstdlib>
13 #include <cstring>
14 #include <iostream>
15 #include <algorithm>
16 using namespace std;
17 
18 typedef long long LL;
19 typedef pair<LL, LL> pLL;
20 typedef pair<LL, int> pLi;
21 typedef pair<int, LL> pil;;
22 typedef pair<int, int> pii;
23 typedef unsigned long long uLL;
24 
25 #define lson rt<<1
26 #define rson rt<<1|1
27 #define lowbit(x) x&(-x)
28 #define name2str(name) (#name)
29 #define bug printf("*********\n")
30 #define debug(x) cout<<#x"=["<<x<<"]" <<endl
31 #define FIN freopen("D://code//in.txt","r",stdin)
32 #define IO ios::sync_with_stdio(false),cin.tie(0)
33 
34 const double eps = 1e-8;
35 const int mod = 1000000007;
36 const int maxn = 2e5 + 7;
37 const double pi = acos(-1);
38 const int inf = 0x3f3f3f3f;
39 const LL INF = 0x3f3f3f3f3f3f3f3fLL;
40 
41 int t;
42 LL l, r, k, m;
43 
44 int main(){
45     scanf("%d", &t);
46     while(t--) {
47         scanf("%lld%lld%lld%lld", &l, &r, &k, &m);
48         int flag = 1;
49         LL ans = 0, tmp = 1;
50         for(int i = 1; i <= m; i++) {
51             if(r / tmp < k) {
52                 flag = 0;
53                 break;
54             }
55             tmp = tmp * k;
56         }
57         if(!flag) {
58             printf("0\n");
59             continue;
60         }
61         ans = r / tmp - (l - 1) / tmp;
62         if(r / tmp >= k) {
63             tmp = tmp * k;
64             ans -= r / tmp - (l - 1) / tmp;
65         }
66         printf("%lld\n", ans);
67     }
68     return 0;
69 }

 

posted @ 2019-02-25 11:59  Dillonh  阅读(211)  评论(0编辑  收藏  举报