1的数量

 

Description

求区间[a,b]包含1的数量。例如区间[111,112], 整个区间包含两个数,分别为111,112,111包含3个1,而112包含2个1,所以区间[111,112]总共包含5个1

Input

多组测试数据。

每组测试数据包含两个整数a, b, 1 <= a <= b <= 10^18.

  Output

每组测试输出一行,表示1的数量,结果mod 10^9+7.

Sample Input

111 112 1 1000

Sample Output

5 301
#include <stdio.h>
#include <string.h>
#define LL long long
const int M = 1000000000 + 7;

LL d[25],a[25];
LL pow(int n)
{
    LL s=1;
    while(n--) s*=10;
    return s;
}
void init()
{
    d[0] = 0;
    //d[i]存储第i位前面有多少个1
    for (int i = 1; i <= 18; i++) d[i] =pow(i-1)*i;
}

LL cal(LL x)
{
    int cnt = 1;
    while (x)
    {
        a[cnt++] = x % 10;//将你得到的数字存起来.
        x /= 10;
    }
    LL ans = 0, temp = 0;
    for (int i = cnt - 1; i >= 1; i--)
    {
        ans += temp * pow( i - 1) * a[i];//表示记录前面已经访问了多少个1了,
        if (a[i] > 1) ans += (LL)a[i] * d[i - 1] + pow(i - 1);//当前位后面有多少个1
        else if (a[i] == 1)
        {
            temp++;//当前位为1加1
            ans += (LL )a[i] * d[i - 1];
        }
    }
    ans = ans + temp;
    return ans;
}

int main()
{
    LL a, b;
    init();
    while (scanf("%lld%lld", &a, &b) != EOF)
    {
        printf("%lld\n", (cal(b) - cal(a - 1)) % M);
    }
    return 0;
}
找规律(数论)

 

 

 

 

 

转载于:https://www.cnblogs.com/contestant/p/3313439.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值