Comet OJ - Contest #4 D求和 思维题

Code: 

#include <bits/stdc++.h> 
#define setIO(s) freopen(s".in","r",stdin) 
#define ll long long 
#define maxn 100000 
using namespace std;      
int arr[30],brr[maxn];   
ll calc(ll t) {   
    int n=0,m=0; 
    while(t) arr[++n]=t%10,t/=10;                 
    while(n>=1) {    
        for(int i=2;i<=n;++i) brr[++m]=(arr[i]+arr[i-1])%10;    
        while(brr[m]==0 && m>=2) --m;                                
        for(int i=1;i<=m;++i) arr[i]=brr[i]; 
        n=m,m=0;    
    } 
    return arr[1];      
}
ll solve(ll k) {    
    if(k<10) return k*(k+1)/2;            
    ll re=(k/10)*45,t=calc((k/10)*10); 
    int o=k%10;      
    for(int i=0;i<=o;++i) re+=1ll*(t+i)%10;  
    return re;    
}
int main() {
    // setIO("input"); 
    int T; 
    scanf("%d",&T); 
    while(T--) {
        ll l,r; 
        scanf("%lld%lld",&l,&r);   
        printf("%lld\n",solve(r) - solve(l-1));     
    }
    return 0;  
}   

  

posted @ 2019-07-24 13:13  EM-LGH  阅读(196)  评论(0编辑  收藏  举报