Solution - CF597A
Solution.
我们可以直观地发现 \([a,b]\) 之间能被 \(k\) 整除的个数可以等价于 \([a,b]\) 中第一个能被 \(k\) 整除的数减去最后一个能被 \(k\) 整除的数的差除 \(k\) 加 \(1\)。
我们设 \(x\in[a,b]\) 是第一个能被 \(k\) 整除的数字。
那么 \(\forall x_0\in[a,b] =px\) , 所以能被 \(k\) 整除的数字序列即为:
\[x,2x,3x,...,px
\]
所以能被 \(k\) 整除的数字就有 \(p\) 个。
我们又设 \(x=rk\) 。
所以原序列又可以表示为:
\[rk,2rk,3rk,...,prk
\]
因为是等差数列,公差为 \(k\)
所以项数 \(p=\frac{prk-rk}{k}+1\)
求第一个能被 \(k\) 的数用除法就行了。
Code.
#include <bits/stdc++.h>
#define int long long
#define rep(i, l, r) for (int i = l; i <= r; i++)
#define dep(i, r, l) for (int i = r; i >= l; i--)
using namespace std;
const int inf = 1e9, mod = 1e8;
template <typename T>
void read (T &x) {
x = 0; T f = 1;
char ch = getchar ();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar ();
}
while (ch >= '0' && ch <= '9') {
x = (x << 3) + (x << 1) + ch - '0';
ch = getchar ();
}
x *= f;
}
char For_Print[25];
template <typename T>
void write (T x) {
if (x == 0) { putchar ('0'); return; }
if (x < 0) { putchar ('-'); x = -x; }
int poi = 0;
while (x) {
For_Print[++poi] = x % 10 + '0';
x /= 10;
}
while (poi) putchar (For_Print[poi--]);
}
template <typename T>
void print (T x, char ch) {
write (x); putchar (ch);
}
int k, a, b;
signed main() {
read(k), read(a), read(b);
int t;
if (a % k == 0) t = a;
else {
int x = a / k;
if (k * (x - 1) > a) t = k * (x - 1);
else if (k * x > a) t = k * x;
else t = k * (x + 1);
}
// cout << t << endl;
if (t <= b) write((b - t) / k + 1);
else write(0);
return 0;
}
本文来自博客园,作者:cqbzjyh,转载请注明原文链接:https://www.cnblogs.com/cqbzjyh/p/15839507.html

浙公网安备 33010602011771号