hdu 4614(数学 + 乱搞)
2013-08-12 12:40
253 查看
题目链接
多校第二场的题目, 首先可以确定lcm(a, b)是周期,然后求出一个周期内的结果, 在一个周期内需要分段考虑,
同一个段内的差值都是相同的, 所以直接模拟每段求再累加就可以了
多校第二场的题目, 首先可以确定lcm(a, b)是周期,然后求出一个周期内的结果, 在一个周期内需要分段考虑,
同一个段内的差值都是相同的, 所以直接模拟每段求再累加就可以了
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cstdlib> #include <cmath> using namespace std; typedef long long LL; LL calc(int n, int a, int b) { LL ans = 0; int d = 0; int ta = a, tb = b; int p = 0; while (p < n) { if (tb < ta) { ans += (LL)d * (min(tb, n) - p); p = tb; d = labs((p % a) - (p % b)); tb += b; } else { ans += (LL)d * (min(ta, n) - p); p = ta; d = labs((p % a) - (p % b)); ta += a; } } return ans; } LL gcd(LL a, LL b) { return b == 0 ? a : gcd(b, a % b); } LL lcm(LL a, LL b) { return a / gcd(a, b) * b; } int main() { int test, n, a, b; LL r; scanf("%d", &test); while (test--) { scanf("%d%d%d", &n, &a, &b); if (a == b) { puts("0"); continue; } r = lcm(a, b); LL tmp = calc(r, a, b); LL res = n / r * tmp + calc(n % r, a, b); printf("%I64d\n", res); } return 0; }
相关文章推荐
- HDU 4814 Golden Radio Base 数学 模拟 或者乱搞。。。。
- HDU 5194 DZY Loves Balls(数学组合or各种乱搞)
- HDU-1174--爆头---数学叉积
- BC Round #76 HDU 5646(数学)
- hdu 5605 geometry(几何,数学)
- HDU 1061 Rightmost Digit (数学&三种解法)
- HDU 1030 Delta-wave 数学题解
- HDU 5073 Galaxy ——乱搞
- hdu 1018 Big Number(纯数学公式)
- 【数学 合九法】HDU 1163 Eddy's digital Roots
- hdu 1141 Factstone Benchmark(数学:计算)
- HDU 4586:Play the Dice (数学)
- hdu 2289 Cup(数学题)
- Hdu 4614 线段树
- HDU 5847 Different Sums (构造+数学+打表)
- HDU 1165 Eddy's research II (数学推导)
- hdu 1061(数学)
- hdu 1576 A/B(数学:简单水题)
- hdu 3349 (水,数学)
- HDU1719 Friend (数学推导)