1.題目
題目描述 Alice喜歡n位數,Bob喜歡能被m整除的數,請問被Alice和Bob都喜歡的數有多少個?
輸入
第一行是一個整數K(K≤10000),表示樣例的個數, 每個樣例是一行,兩個整數n(1≤n≤18),m(2≤m≤1000000),
輸出
每行輸出一個樣例的結果, 樣例輸入 4 1 2 3 2 3 3 3 10000 樣例輸出 5 450 300 0
2.解法
數學問題,n位數最大數是10n-1,這個數除m向下取整得到的商再減去n-1位數最大的數除以m得到的商就是他們都喜歡的數,注意數的范圍別溢位就好,
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int main(int argc, char const **argv) {
int K, n, m;
unsigned long long temp;
cin >> K;
while(K--) {
cin >> n >> m;
temp = (unsigned long long)pow(10, n) - 1;
unsigned long long cnt = temp/m - temp / 10 / m;
if(n==1) {
cnt++;
}
cout << cnt << endl;
}
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/262198.html
標籤:其他
上一篇:為了你們不錯過金三銀四,我真是操碎了心!RabbitMQ面試真題來了,你不會不想看吧?
下一篇:C++: 指標空值nullptr
