[HBUE 月赛第一场] 7-6 起风了

  思路:

       模拟一下样例,就可以看出数学/规律,看尾数,每 $10$ 个一循环(偶数每 $5$ 个一循环),先统计前 $10$ 个数的尾数之和,再按每 $10$ 个进行分段处理,加上剩余尾数即可。

       公式:

       设前十个尾数之和为 $s$,答案则为:$\frac{\frac{n}{m}}{10} \times  s + \frac{n}{m} \% 10$。 

#include <iostream>
using namespace std;

typedef long long LL;

int main()
{
    int T;
    cin >> T;
    while (T--) {
        LL n, m;
        LL ans = 0, s = 0;
        cin >> n >> m;
        for (LL i = m, j = 0; i <= n && j < 10; i += m, ++j) { // 从 m 开始,统计前 10 元素之和
            s += i % 10; // 取尾数
        }
        ans += n / m / 10 * s; // s / m / 10 可分为几段
        // 统计剩下的数字 n / m % 10
        LL k = n / m % 10;
        for (LL i = m, j = 0; j < k; i += m, ++j) { // 剩下 k 个
            ans += i % 10;        
        }
        cout << ans << endl;
    }
    return 0;
}

 

posted @ 2021-03-27 22:35  Fool_one  阅读(48)  评论(0编辑  收藏  举报