首页 技术 正文
技术 2022年11月15日
0 收藏 823 点赞 3,585 浏览 1322 个字

P1831 杠杆数

题目描述

如果把一个数的某一位当成支点,且左边的数字到这个点的力矩和等于右边的数字到这个点的力矩和,那么这个数就可以被叫成杠杆数。

比如4139就是杠杆数,把3当成支点,我们有这样的等式:4 2 + 1 1 = 9 * 1。

给定区间[x,y],求出在[x,y]中有几个杠杆数。

输入输出格式

输入格式:

两个数,表示x,y。

输出格式:

一个输出,表示区间[x,y]中杠杆数的个数。

输入输出样例

输入样例#1:

7604 24324

输出样例#1:

897

说明

对于40%的数据,x<=y<=x+100000

对于100%的数据,1<=x<=y<=10^18

#include<iostream>
#include<cstdio>
using namespace std;
long long l,r,ans;
int a[];
bool check(long long now){
int len=;
while(now){
a[++len]=now%;
now/=;
}
int L=,R=;
for(int i=;i<=len;i++){//枚举支点
L=;R=;
for(int j=;j<i;j++)L+=(i-j)*a[j];
for(int j=i+;j<=len;j++)R+=(j-i)*a[j];
if(L==R)return ;
}
return ;
}
int main(){
scanf("%lld%lld",&l,&r);
for(long long i=l;i<=r;i++){
if(check(i))ans++;
}
cout<<ans;
}

40分 暴力(枚举每个数再枚举支点)

/*
数位dp,写的记忆化搜索
应该算数位dp中的简单题吧,套路都在
*/
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
long long a,b,dp[][][];
int bit[],len;
long long dfs(int pos,int central,int pre,int limit){
if(pos<=)return pre==;
if(pre<)return ;
if(!limit&&dp[pos][central][pre]!=-)return dp[pos][central][pre];
int end=limit?bit[pos]:;
long long ans=;
for(int i=;i<=end;i++){
ans+=dfs(pos-,central,pre+i*(pos-central),limit&&i==end);
}
if(!limit)dp[pos][central][pre]=ans;
return ans;
}
long long solve(long long x){
len=;
while(x){
bit[++len]=x%;
x/=;
}
long long ans=;
for(int i=;i<=len;i++)ans+=dfs(len,i,,);
return ans-len+;
}
int main(){
memset(dp,-,sizeof(dp));
scanf("%lld%lld",&a,&b);
printf("%lld",solve(b)-solve(a-));
}

100分 数位dp

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,077
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,552
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,400
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,812
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,894