F Find 3-friendly Integers

2024-01-28 15:58
文章标签 find integers friendly

本文主要是介绍F Find 3-friendly Integers,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Find 3-friendly Integers

题目描述

A positive integer is 3-friendly if and only if we can find a continuous substring in its decimal representation, and the decimal integer represented by the substring is a multiple of 333.

For instance:

  1.  104{104}104 is 3-friendly because "0" is a substring of "104" and 0mod  3=00 \mod 3 = 00mod3=0.
  2.  124{124}124 is 3-friendly because "12" is a substring of "124" and 12mod  3=012 \mod 3 = 012mod3=0. "24" is also a valid substring.
  3.  17{17}17 is not 3-friendly because 1mod  3≠0, 7mod  3≠0, 17mod  3≠01 \mod 3 \ne 0, ~7 \mod 3 \ne 0, ~17 \mod 3 \ne 01mod3​=0, 7mod3​=0, 17mod3​=0.


Note that the substring with leading zeros is also considered legal.

Given two integers L{L}L and R{R}R, you are asked to tell the number of positive integers x{x}x such that L≤x≤RL \le x \le RL≤x≤R and x{x}x is 3-friendly.

输入描述:

There are multiple test cases. The first line of the input contains an integer T(1≤T≤10000)T(1 \le T \le 10000)T(1≤T≤10000), indicating the number of test cases. For each test case:The only line contains two integers L,R(1≤L≤R≤1018)L,R(1 \le L \le R \le 10^{18})L,R(1≤L≤R≤1018), indicating the query.

输出描述:

For each test case output one line containing an integer, indicating the number of valid x{x}x.

示例1

输入

复制

3
4 10
1 20
1 100

输出

复制

3
11
76

思路:打表题:首先一个结论:若一个数的每一位数字之和为3的倍数,则这个数能被3整除
 其次,我们发现100及以后的数都符合题目所给条件,所以只需要对前100的数进行判断


代码如下:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int t;
ll l,r,ans;
vector<int> v;//储存不满足要求的数
int main(){
    for(int i=1;i<10;i++){//一位数
        if(i%3!=0) v.push_back(i);
    }
     for(int i=11;i<100;i++){//两位数
         if(i%10%3!=0&&i/10%3!=0&&i%3!=0){
             v.push_back(i);
         }
     }
    cin>>t;
    while(t--){
        cin>>l>>r;
        ans=r-l+1;//所有的数
        for(int i=0;i<v.size();i++){
            if(v[i]>=l&&v[i]<=r){
                ans--;//减去范围内的不满足要求的数
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

这篇关于F Find 3-friendly Integers的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/654102

相关文章

MongoDB学习—(6)MongoDB的find查询比较符

首先,先通过以下函数向BookList集合中插入10000条数据 function insertN(obj,n){var i=0;while(i<n){obj.insert({id:i,name:"bookNumber"+i,publishTime:i+2000})i++;}}var BookList=db.getCollection("BookList")调用函数,这样,BookList

【NodeJS】Error: Cannot find module 'ms'

转载自:http://blog.csdn.net/echo_ae/article/details/75097004 问题: Error: Cannot find module 'ms'at Function.Module._resolveFilename (module.js:469:15)at Function.Module._load (module.js:417:25)at Module

leetCode#448. Find All Numbers Disappeared in an Array

Description Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements of [1, n] inclusive that do not appear in this

访问controller404:The origin server did not find a current representation for the target resource

ider build->rebuild project。Rebuild:对选定的目标(Project),进行强制性编译,不管目标是否是被修改过。由于 Rebuild 的目标只有 Project,所以 Rebuild 每次花的时间会比较长。 参考:资料

mybatis错误——java.io.IOException Could not find resource comxxxxxxMapper.xml

在学习Mybatis的时候,参考网上的教程进行简单demo的搭建,配置的没有问题,然后出现了下面的错误! Exception in thread "main" java.lang.RuntimeException: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause:

Linux 中的 find 命令介绍以及使用

文章目录 Linux 中的 `find` 命令详解及使用示例`find` 命令的基本语法常用的 `find` 命令选项按文件名搜索:`-name`按文件类型搜索:`-type`按文件大小搜索:`-size`按修改时间搜索:`-mtime`按权限搜索:`-perm`按所有者搜索:`-user` 和 `-group` `find` 命令的常见操作删除找到的文件:`-exec` 和 `rm`查找并

mysql中find_in_set()函数

1.场景 假设有一个user用户表,表字段分别为:id(主键),name(姓名),age(年龄),hobby(爱好)。而一个人可能有好几个爱好,游泳啊篮球啊乒乓球啊等等。数据库里hobby字段存的是:游泳,篮球,乒乓球 而要想查所有喜欢游泳的人,就可以用find_in_set函数了 todo:贴图 2.使用 select *from user where find_in_set("游泳"

Failed to find style 'vpiCirclePageIndicatorStyle' in current theme

使用 ViewPagerIndicator 时 , 布局文件报的错 Missing styles. Is the correct theme chosen for this layout? Use the Theme combo box above the layout to choose a different layout, or fix the theme style refe

[LeetCode] 438. Find All Anagrams in a String

题:https://leetcode.com/problems/find-all-anagrams-in-a-string/description/ 题目 Given a string s and a non-empty string p, find all the start indices of p’s anagrams in s. Strings consists of lowerca

最佳优先搜索best-find search

目录 1. 问题 2. 算法 3.代码 1. 问题 考虑下面这个问题:  我们要找到从Arad到Bucharest的路,最好是最短的路: 2. 算法 这是一个无向有环图, 可以采用最佳优先搜索: 最佳优先搜索的算法可以参考维基百科: 伪代码如下: // Pseudocode for Best First SearchBest-First-Search(Gr