CF div3 582 C. Book Reading

CF div3 582 C. Book Reading

Meaning of the questions:

To the two numbers n and m, in seeking 0 ~ n, a multiple of the sum of m bits

Ideas:

Hit table, outputs 0 ~ n, the number of multiple m of what features and will find that these numbers, one every 10 cycles

And this cycle festival, the largest not more than 10, and certainly about the number 10, then we will assume his cycle length is 10

  • 证明:
    \[ 0,1m,2m,3m,4m,5m,6m,7m,8m,9m,\quad,10m,11m,12m,---n\\ 0,1,2,3,4,5,6,7,8,9,\quad\quad\quad\quad\quad\quad\quad,10,11,12---------[n/m]\\ 0,1,2,3,4,5,6,7,8,9\quad\quad\quad\quad\quad,0,1,2,--------[n/m/10] \]

According to this property, we can pre-festival out of this cycle, multiplied by the number of cycles, then add the remaining remnants of the cycle, to get the answer

Code:

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
typedef unsigned long long ULL;
ULL n , m, ans ,k ;
int t;
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    cin >> t;
    while(t--){
        vector<int> v;
        ans = 0;
        cin >> n >> m;
        k = n / m; // m的倍数在 n 的范围中出现的次数
        for(int i = 1;i <= 10; ++i) {  // 求循环节
            int x = i * m % 10;
            ans += x; // 预处理循环节的总和
            v.push_back(i * m % 10);
        }
        ans *= k/10;  // ans当前代表 每个循环节的和,再乘以 k/10 (循环次数),因为每个循环节长度为10,等到循环次数k
        k %= 10; //  最后剩余的 ,不满足 一个循环节的数字
        for(int i = 0;i < k; ++i) ans += v[i]; // 把剩余的数字加上去
        cout << ans;
        if(t != 0) cout << endl;
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/317zhang/p/11466740.html