C 求调,主要看看思路对否
  • 板块题目总版
  • 楼主array2022
  • 当前回复2
  • 已保存回复2
  • 发布时间2025/1/4 22:03
  • 上次更新2025/1/5 11:29:39
查看原帖
C 求调,主要看看思路对否
690315
array2022楼主2025/1/4 22:03

思路:分类讨论+递推

记录

#include<bits/stdc++.h>
#define int long long
using namespace std;
int l,r;
int qpow(int a,int b){
	int res=1;
	while (b>0){
		if (b&1) res*=a;
		b/=2;
		a*=a;
	}
	return res;
}
int digi[25];
int get(int x){
	int t=0,ans=0;
	while (x>0){
		digi[++t]=x%10;
		x/=10;
	}
	for (int i=2;i<t;i++){
		for (int j=1;j<=9;j++){
			ans+=qpow(j,i-1);
		}
	}
	for (int j=1;j<digi[t];j++){
		ans+=qpow(j,t-1);
	}
	int res=1;
	for (int i=t-1;i>0;i--){
		res=(res-1)*digi[t]+min(digi[i],digi[t]-1)+1;
	}
	return ans+res-1;
}
signed main(){
	ios::sync_with_stdio(0);
	cin>>l>>r;
	cout<<get(r)-get(l-1);
	return 0;
}
2025/1/4 22:03
加载中...