Codeforces1538 F. Interesting Function(十进制按位考虑贡献)

Posted live4m

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces1538 F. Interesting Function(十进制按位考虑贡献)相关的知识,希望对你有一定的参考价值。

题意:

解法:

发现询问是形如[l,r]的一个区间,
那么可以将问题变成[0,r]-[0,l].cal(x)[1,x]的值,
考虑cal(x)如何计算:
按十进制位考虑贡献,
个位变化的次数为x,
十位变化的次数为x/10,
百位变化的次数为x/10/10,
...

code:

#include<bits/stdc++.h>
#define int long long
using namespace std;

int cal(int x){
    int ans=0;
    while(x){
        ans+=x;
        x/=10;
    }
    return ans;
}
void solve(){
    int l,r;cin>>l>>r;
    int ans=cal(r)-cal(l);
    cout<<ans<<endl;
}
signed main(){
    ios::sync_with_stdio(0);cin.tie(0);
    int T;cin>>T;while(T--)
    solve();
    return 0;
}

以上是关于Codeforces1538 F. Interesting Function(十进制按位考虑贡献)的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #725 (Div. 3)Codeforces-1538

Codeforces Round #725 (Div. 3)Codeforces-1538

Codeforces Round #725 (Div. 3)Codeforces-1538

Codeforces Round #725 (Div. 3)Codeforces-1538

CodeForces - 1538G Gift Set(二分)

Codeforces 240 F. TorCoder 线段树