CodeM资格赛E 数码 题解
来源:互联网 发布:养羊软件 编辑:程序博客网 时间:2024/05/16 01:28
题意
给定两个整数 l 和 r ,对于所有满足1 ≤ l ≤ x ≤ r ≤ 10^9 的 x ,把 x 的所有约数全部写下来。对于每个写下来的数,只保留最高位的那个数码。求1~9每个数码出现的次数。
思路
首先我们只需要能求出从1到x的答案,然后减就好了,对于不超过x√ 的数,就要在这个的最高位的计数上加x/x√ ,并记录下x/x√ ,放到栈中,对于超过x√ 的数,就会有一段数作为约数出现在1到x中的次数都相等,而这个界线就是之前存在栈里的那些数,此时我们需要做的是能够求出1到y中对每个数取最高位那么1到9出现的次数,然后相减即可,经过这两步就可以得到题目要的结果
代码
#include <cstdio>#include <cmath>#include <stack>using namespace std;long long ans1[10],ans2[10],ad1[10],ad2[10];long long hdigit(long long x){ while(x>=10) x/=10; return x;}void calc2(long long x,long long *d){ long long xx=x,cnt=0,temp; for(long long i=0;i<=9;i++) d[i]=0; if(x==0) return; while(xx>0) { xx/=10; cnt++; } temp=1; for(long long i=0;i<cnt-1;i++) { for(int j=1;j<=9;j++) d[j]+=temp; temp*=10; } for(long long i=1;i<hdigit(x);i++) d[i]+=temp; d[hdigit(x)]+=x-hdigit(x)*temp+1; return;}void calc(long long x,long long* d){ for(long long i=0;i<=9;i++) d[i]=0; if(x==0) return; long long e=sqrt(x),ee; ee=e; stack<long long> st; for(long long i=1;i<=e;i++) { d[hdigit(i)]+=x/i; st.push(x/i); } while(!st.empty()) { if(e==st.top()) { st.pop(); ee--; continue; } if(st.empty()) break; calc2(st.top(),ad1); calc2(e,ad2); //printf("e:%lld sttop:%lld\n",e,st.top()); for(long long i=1;i<=9;i++) { //printf("%lld\n",ad1[i]-ad2[i]); d[i]+=(ad1[i]-ad2[i])*ee; } e=st.top(); } return;}int main(){ long long l,r; scanf("%lld%lld",&l,&r); calc(r,ans1); calc(l-1,ans2); for(long long i=1;i<=9;i++) printf("%lld\n",ans1[i]-ans2[i]); return 0;}
阅读全文
0 0
- CodeM资格赛E 数码 题解
- 美团codeM资格赛 数码
- CodeM资格赛B 锦标赛 题解
- CodeM资格赛C 优惠券 题解
- 美团Codem编程比赛资格赛:数码
- 2017年codeM资格赛 数码 思维
- CodeM资格赛A 音乐研究 题解
- CodeM资格赛D 送外卖 题解
- 美团CodeM编程大赛资格赛题解
- CodeM资格赛
- CodeM 数码
- CodeM资格赛第一题
- CodeM资格赛第二题
- CodeM资格赛1
- 美团CODEM 数码
- CodeM编程资格赛第三题
- 美团codeM资格赛 优惠券
- 美团codeM资格赛 锦标赛
- Zcash零币挖矿教程Windows系统N卡
- Android 获取系统内的应用信息
- python文件操作 API整理
- A Famous Music Composer
- 一个简单却很炫的LoadingView
- CodeM资格赛E 数码 题解
- 关于手机数据库的访问过程中遇到的一个坑
- [LeetCode] 166. Fraction to Recurring Decimal
- oracle创建新用户
- 神经网络基础知识
- 语音识别关键技术公开,人机交互这么做就对了!
- 答案自己写的
- 51nod 1237 最大公约数之和 V3
- 【性能测试】-使用loadrunner对APP进行性能测试