Comet OJ C1076 [Contest #4]求和
题目
首先我们可以通过经典容斥转化为计算\([1,x]\)的答案。
现在我们考虑一个数的个位对答案的贡献。
每做一次操作都会让个位加上十位然后取模,直到只有个位为止。
我们发现这个过程中,个位数前的系数永远都是\(1\),也就是个位数对答案的贡献系数为\(1\)。
这意味着我们对于一个固定的只有个位没确定的数,我们枚举其个位\(0\sim9\),其答案也是\(0\sim9\),所以我们可以直接求出\([1,\lfloor\frac n{10}\rfloor*10-1]\)的答案为\(\lfloor\frac n{10}\rfloor*45\)。
然后剩下\(n\mod10+1\)个数,直接计算很不优秀。
我们先暴力计算\(t=f(\lfloor\frac n{10}\rfloor)\),那么\(f(\lfloor\frac n{10}\rfloor)+i=t+i(i\in[0,9])\),这样只用暴力算一次,非常优秀。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int a[19];
int f(ll x)
{
int len=0,i;
while(x) a[++len]=x%10,x/=10;
while(len^1)
{
for(i=1;i<len;++i) a[i]=(a[i]+a[i+1])%10;
for(--len;!a[len]&&len^1;--len);
}
return a[len];
}
ll cal(ll x)
{
if(x<10) return x*(x+1)/2;
ll s=x/10*45;int o=x%10,i,t=f(x/10*10);
for(i=0;i<=o;++i) s+=(t+i)%10;
return s;
}
int main()
{
int T;ll l,r;
for(scanf("%d",&T);T;--T) scanf("%lld%lld",&l,&r),printf("%lld\n",cal(r)-cal(l-1));
}
Comet OJ C1076 [Contest #4]求和的更多相关文章
- Comet OJ - Contest #2 简要题解
Comet OJ - Contest #2 简要题解 cometoj A 模拟,复杂度是对数级的. code B 易知\(p\in[l,r]\),且最终的利润关于\(p\)的表达式为\(\frac{( ...
- Comet OJ - Contest #2简要题解
Comet OJ - Contest #2简要题解 前言: 我没有小裙子,我太菜了. A 因自过去而至的残响起舞 https://www.cometoj.com/contest/37/problem/ ...
- Comet OJ - Contest #4--前缀和
原题:Comet OJ - Contest #4-B https://www.cometoj.com/contest/39/problem/B?problem_id=1577传送门 一开始就想着暴力打 ...
- Comet OJ - Contest #11 题解&赛后总结
Solution of Comet OJ - Contest #11 A.eon -Problem designed by Starria- 在模 10 意义下,答案变为最大数的最低位(即原数数位的最 ...
- Comet OJ - Contest #8
Comet OJ - Contest #8 传送门 A.杀手皇后 签到. Code #include <bits/stdc++.h> using namespace std; typede ...
- Comet OJ - Contest #13-C2
Comet OJ - Contest #13-C2 C2-佛御石之钵 -不碎的意志-」(困难版) 又是一道并查集.最近做过的并查集的题貌似蛮多的. 思路 首先考虑,每次处理矩形只考虑从0变成1的点.这 ...
- Comet OJ - Contest #13 「火鼠的皮衣 -不焦躁的内心-」
来源:Comet OJ - Contest #13 芝士相关: 复平面在信息学奥赛中的应用[雾 其实是道 sb 题??? 发现原式貌似十分可二项式定理,然后发现确实如此 我们把 \(a^i\) 替换成 ...
- Comet OJ - Contest #13 「佛御石之钵 -不碎的意志-」(hard)
来源:Comet OJ - Contest #13 一眼并查集,然后发现这题 tmd 要卡常数的说卧槽... 发现这里又要用并查集跳过访问点,又要用并查集维护联通块,于是开俩并查集分别维护就好了 一开 ...
- Comet OJ - Contest #5
Comet OJ - Contest #5 总有一天,我会拿掉给\(dyj\)的小裙子的. A 显然 \(ans = min(cnt_1/3,cnt_4/2,cnt5)\) B 我们可以感性理解一下, ...
随机推荐
- 【leetcode】1178. Number of Valid Words for Each Puzzle
题目如下: With respect to a given puzzle string, a word is valid if both the following conditions are sa ...
- 9. ClustrixDB主从复制
一.在线添加从库 主集群: 10.1.1.23:5306 从集群: 10.1.3.88:5306 主库开启binlog MySQL [(none)]> CREATE BINLOG 'clustr ...
- CDOJ 1069 秋实大哥去打工 单调栈 下标处理
E - 秋实大哥去打工 Time Limit:1000MS Memory Limit:65535KB 64bit IO Format:%lld & %llu Submit St ...
- sh_06_break
sh_06_break i = 0 while i < 10: # break 某一条件满足时,退出循环,不再执行后续重复的代码 # i == 3 if i == 3: break print( ...
- 【Leetcode】位1的个数
解题方案:位操作的技巧 整数 n 和 n-1(n>0) 做与运算,从其二进制形式来看,可以消掉 n 的二进制数值中最后1个 “1” .循环进行,每次消掉1个 “1” .整数 n 的二进制数值中有 ...
- 关于如何解决TeamViewer限制时间问题
最近在弄一个项目,我们是乙方,甲方离我们比较远,所以需要用到远程操作软件.也就是TeamViewer. 这个软件一开始运行还行,后来时间用久了,很容易被限制时间.在网上查了大部分资料,都是一些修改MA ...
- SpringMVC前端控制器以.html后缀拦截,访问接口返回406问题
原因: spring监测到是.html来访问,它就会认为需要返回的是html页面.如果返回的不是html,会报406错误 解决: 提供多种后缀拦截方式,工程里web.xml配置 分析: HTTP 40 ...
- What is 'typeof define === 'function' && define['amd']' used for?
What is 'typeof define === 'function' && define['amd']' used for? This code checks for the p ...
- [论文理解] LFFD: A Light and Fast Face Detector for Edge Devices
LFFD: A Light and Fast Face Detector for Edge Devices 摘要 从微信推文中得知此人脸识别算法可以在跑2K图片90fps,仔细一看是在RTX2070下 ...
- Windows Server系统定时任务备份ORACLE数据库
Windows Server系统定时任务备份ORACLE数据库 一.编辑备份脚本 RMAN备份数据库 1.在备份脚本目录下,创建bat文件db_rman.bat set ORACLE_SID=orcl ...