Educational Codeforces Round 50 (Rated for Div. 2) C. Classy Numbers

看到数论就懵了,题目有两个解法,第一个解法是组合数学的内容,解析用英文写的,看不懂;第二个解法是暴力搜索的,找了网上用同样方法的中文注释代码才看明白。首先如果直接枚举肯定玩完,要找些其他办法。题目中用到了dfs,或者叫暴力搜索,但是思路很巧妙。函数设计了三个参数,分别是搜索层数lev,代表10的次方数;当前处理的数字cu,为了不越界,用long long型;非0位数的个数cnt。如果层数超过了18,就应该返回。设置一个vector,存long long型数据,每次如果没返回,就把这个数加进vector。之后用循环控制接下来的搜索过程。

代码如下。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<ll> v;
void brute(int lev,ll cu,int cnt)    {
    if (lev==18)    {
        v.push_back(cu);
        return;
    }
    brute(lev+1,cu*10,cnt);
    if (cnt<3)  {
        for (int i=1;i<=9;i++)
            brute(lev+1,cu*10+i,cnt+1);
    }
}

int main()  {
    ios::sync_with_stdio(false);
    cin.tie(0);
    brute(0,0,0);
    v.push_back(1e18);
    ll l,r;
    int n;
    cin>>n;
    for (int i=0;i<n;i++)   {
        cin>>l>>r;
        int ans=upper_bound(v.begin(),v.end(),r)-lower_bound(v.begin(),v.end(),l);
        cout<<ans<<endl;
    }
    return 0;
}

貌似这样会有重复搜索的内容,可能会爆栈或TLE,但是每次搜索时的X10,会让这一分支不断加深,到最深后,两侧的递归树会不断伸展,直到覆盖整个10^18的所有数。这个搜索才18层,但是已经足以达到所有数字了,时间复杂度可以保证。

注意:

1.C++中的两个二分查找标准库函数:upper_bound(iterator begin,iterator end,elem)——从升序排列的顺序存储结构中寻找大于elem的第一个元素,返回指向这个元素的迭代器。如果未找到,返回指向elelm的迭代器

2.C++中的两个二分查找标准库函数:lower_bound(iterator begin,iterator end,elem)——从升序排列的顺序存储结构中寻找大于等于elem的第一个元素,返回指向这个元素的迭代器。如果未找到,返回指向elelm的迭代器

原文地址:https://www.cnblogs.com/yichuan-sun/p/9623461.html

时间: 2024-07-31 23:59:39

Educational Codeforces Round 50 (Rated for Div. 2) C. Classy Numbers的相关文章

Educational Codeforces Round 50 (Rated for Div. 2)的A、B、C三题AC代码

A题链接:https://codeforces.com/contest/1036/problem/A A题AC代码: 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <math.h> 5 #include <malloc.h> 6 #include <stdbool.h> 7 #include <ctype.h> 8 9

Educational Codeforces Round 36 (Rated for Div. 2)

Educational Codeforces Round 36 (Rated for Div. 2) F. Imbalance Value of a Tree You are given a tree T consisting of n vertices. A number is written on each vertex; the number written on vertex i is ai. Let's denote the function I(x,?y) as the differ

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars

Educational Codeforces Round 69 (Rated for Div. 2) B - Pillars There are n pillars aligned in a row and numbered from 1 to n. Initially each pillar contains exactly one disk. The i-th pillar contains a disk having radius ai. You can move these disks

Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations

原文链接:https://www.cnblogs.com/xwl3109377858/p/11405773.html Educational Codeforces Round 71 (Rated for Div. 2) D - Number Of Permutations You are given a sequence of n pairs of integers: (a1,b1),(a2,b2),…,(an,bn). This sequence is called bad if it is

Educational Codeforces Round 85 (Rated for Div. 2)

Educational Codeforces Round 85 (Rated for Div. 2) A. Level Statistics 签到题, 要求第一位维单增, 第二维变化比第一维慢, 直接模拟即可 B. Middle Class 题目大意 每个人有一些财富, 财富值超过x的人称为富人, 由于实行共产主义, 你可以将某些人的财产重新分配使得富人最多 解题思路 直接按财富值排序, 从大到小加入富人集合, 如果当前富人集合财富平均值小于x就break掉 C. Circle of Monst

Educational Codeforces Round 36 (Rated for Div. 2) 题解

Educational Codeforces Round 36 (Rated for Div. 2) 题目的质量很不错(不看题解做不出来,笑 Codeforces 920C 题意 给定一个\(1\)到\(n\)组成的数组,只可以交换某些相邻的位置,问是否可以将数组调整为升序的 解题思路 首先如果每个数都能通过交换到它应该到的位置,那么就可以调整为升序的. 但实际上交换是对称的,如果应该在的位置在当前位置前方的数都交换完成,那么整体就是排好序的,因为不可能所有不在相应位置的数都在相应位置的后方.

Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes

Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://codeforces.com/contest/985/problem/E Description Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome w

Educational Codeforces Round 55 (Rated for Div. 2)

Educational Codeforces Round 55 (Rated for Div. 2) 链接 A Vasya and Book 傻逼题..注意判边界. #include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<set> #include<map> #include<vector> #include<cm

Educational Codeforces Round 57 (Rated for Div. 2)

get人生第二场CF! 成绩:(exACM) rank858 AC3/7 Penalty57 rating1648(+52) 题目:Educational Codeforces Round 57 (Rated for Div. 2) 错题题解: D. Easy Problem E. The Top Scorer F. Inversion Expectation G. Lucky Tickets 原文地址:https://www.cnblogs.com/xht37/p/10198321.html