数码

数码

Problem:

给定两个整数 l 和 r ,对于所有满足1 ≤ l ≤ x ≤ r ≤ 10^9 的 x ,把 x 的所有约数全部写下来。对于每个写下来的数,只保留最高位的那个数码。求1~9每个数码出现的次数。

Input:

一行,两个整数 l 和 r (1 ≤ l ≤ r ≤ 10^9)。

Output:

输出9行。
第 i 行,输出数码 i 出现的次数。

Example:

Input

1 4

Output

4
2
1
1
0
0
0
0
0

题解:

枚举优化,直接统计因子是不可能的,所以我们可以枚举因子,然后求范围内有多少包含因子,对于x=a*b,枚举a,求解b,再[1,r]内可能包含a因子的数存在于[1,b],两区间取交集为[1,min(r,b)],其实就是计算区间长度,因为因子最高位有多种情况,所以再枚举因子位数,然后枚举最高位值,这样就可以确定可以存在的因子值的范围,然后再与上述范围相交,取区间长度即为给定a因子条件下,所有b因子对应不同最高位的数量,最后求总和即可。

Code:

/**********************************************************
* @Author: 			   Kirito
* @Date:   			   2020-07-29 08:00:31
* @Last Modified by:   Kirito
* @Last Modified time: 2020-07-29 08:49:31
* @Remark: 
**********************************************************/
#include <bits/stdc++.h>
#define lowbit(x) (x&(-x))
#define CSE(x,y) memset(x,y,sizeof(x))
#define INF 0x3f3f3f3f
#define Abs(x) (x>=0?x:(-x))
#define FAST ios::sync_with_stdio(false);cin.tie(0);
using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll , ll> pll;

const int maxn=111;
ll cnt1[maxn],cnt2[maxn];

int getHigh(int x)
{
	int nn;
	while(x){
		nn=x%10;
		x/=10;
	}
	return nn;
}

void getAns(int up,ll cnt[])
{
	for(int i=1;i*i<=up;i++){
		for(int sp=1;sp<=up;sp*=10){
			for(int k=1;k<=9;k++){
				int mx=min(up/i,(k+1)*sp-1);
				int mn=max(k*sp,i+1);//不计算a
				if(mx>=mn) cnt[k]+=mx-mn+1;
			}
		}
		cnt[getHigh(i)]+=up/i-i+1;//计算a
	}
	return;
}

int main()
{
	#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	#endif
	int l,r;
	cin>>l>>r;
	getAns(r,cnt1);
	getAns(l-1,cnt2);
	for(int i=1;i<=9;i++){
		if(cnt1[i]>cnt2[i])
			cout<<cnt1[i]-cnt2[i]<<endl;
		else
			cout<<"0"<<endl;
	}
	return 0;
}
原文地址:https://www.cnblogs.com/LeafLove/p/13395099.html