2021牛客多校1 F Find 3-friendly Integers
2021-7-18
·
hexer

F Find 3-friendly Integers

链接:https://ac.nowcoder.com/acm/contest/11166/F 来源:牛客网

时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K 64bit IO Format: %lld

题目描述

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

思路与代码

根据鸽笼原理我们可以知道超过99的数一定有包含3的倍数的子串,所以只需要判定1-99就可以了

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <iostream>
#include <sstream>
#include <set>
#include <map>
#include <queue>
#include <bitset>
#include <vector>
#include <limits.h>
#include <assert.h>
#include <functional>
#include <numeric>
#include <ctime>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define pb          push_back
#define ppb         pop_back
#define lbnd        lower_bound
#define ubnd        upper_bound
#define endl        '\n'
#define mll         map<ll,ll>
#define msl         map<string,ll>
#define mls         map<ll, string>
#define rep(i,a,b)    for(ll i=a;i<b;i++)
#define repr(i,a,b) for(ll i=b-1;i>=a;i--)
#define trav(a, x)  for(auto& a : x)
#define pll         pair<ll,ll>
#define vl          vector<ll>
#define vll         vector<pair<ll, ll>>
#define vs          vector<string>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll)x.size()
#define hell        1000000007
#define DEBUG       cerr<<"/n>>>I'm Here<<</n"<<endl;
#define display(x)  trav(a,x) cout<<a<<" ";cout<<endl;
#define what_is(x)  cerr << #x << " is " << x << endl;
#define ini(a)      memset(a,0,sizeof(a))
#define ini2(a,b)   memset(a,b,sizeof(a))
#define rep(i,a,b)  for(int i=a;i<=b;i++)
#define sc          ll T;cin>>T;for(ll Q=1;Q<=T;Q++)
#define lowbit(x)   x&(-x)
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define DBG(x) \
    (void)(cout << "L" << __LINE__ \
    << ": " << #x << " = " << (x) << '\n')
#define TIE \
    cin.tie(0);cout.tie(0);\
    ios::sync_with_stdio(false);

//using namespace __gnu_pbds;

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const int maxn = 2000;
const int N = 100010;


ll cb[N];


void solve(){
	
	ll l,r,ans;
	cin>>l>>r;
	l--;//记得
	if(l<100&&r>=100){
		ans = r-99+cb[99]-cb[l];
	}else if(l<100&&r<100){
		ans = cb[r] - cb[l];
	}else{
		ans = r-l;
	}

	cout<<ans<<endl;

}

int main()
{
	cb[0] = 0;
	ll q;
	for(ll i=1;i<100;i++){
		if(i%10==0||i%10==3||i/10==3||i%10==6||i/10==6
		||i%10==9||i/10==9||(i%10+i/10)%3==0){
			q = 1;
		}
		else q = 0;
		cb[i] = cb[i-1] + q;
	}
//    #ifndef ONLINE_JUDGE
//    freopen ("input.txt","r",stdin);
//    #else
//    #endif
//	solve();
    sc{solve();}
//    sc{cout<<"Case "<<Q<<":"<<endl;solve();}
}