hdu 4611 Balls Rearrangement
来源:互联网 发布:js导航条代码 编辑:程序博客网 时间:2024/06/05 10:24
找规律,模拟,计算压缩;
最大公因数,最小公倍数
#include <iostream>#include <cstdio>#include <cmath>#include <cstring>#include <algorithm>using namespace std;typedef long long ll;/** ∑abs(i%A - i%B) 0<=i<N*/ll getGCD ( ll a, ll b ){ for ( int t; b; ) { t = a % b; a = b; b = t; } return a;}ll getLCM ( ll a, ll b ){ return a * b / getGCD ( a, b );}ll mabs ( ll a ){ return a > 0 ? a : -a;}ll solve ( ll lcm , ll a, ll b ){ ll res = 0; ll lasta = 0, lastb = 0, last ; ll numa = 0, numb = 0; for ( last = 0; last < lcm; ) { if ( lasta + a <= lastb + b ) { lasta = min(a+lasta, lcm) ; res += ( lasta - last ) * mabs ( numa - numb ); numa = 0; numb = ( lasta - last + numb ) % b; last = lasta; } else { lastb = min(b+lastb, lcm) ; res += ( lastb - last ) * mabs ( numa - numb ); numb = 0; numa = ( numa + lastb - last ) % a; last = lastb; } } return res;}int main(){#ifdef __GNUC__ freopen ( "in.txt", "r", stdin );#endif // __GNUC__ int t; ll n, a, b, lcm, res; scanf ( "%d", &t ); while ( t-- ) {#ifdef __GNUC__ scanf ( "%lld%lld%lld", &n, &a, &b );#else scanf ( "%I64d%I64d%I64d", &n, &a, &b );#endif // __GNUC__ if ( a < b ) { swap ( a, b ); } lcm = getLCM ( a, b ); res = solve ( lcm, a, b ); res = res * ( n / lcm ); lcm = n % lcm; res += solve ( lcm, a, b );#ifdef __GNUC__ printf("%lld\n", res);#else printf("%I64d\n", res);#endif // __GNUC__ } return 0;}
- hdu 4611 Balls Rearrangement
- hdu 4611 Balls Rearrangement
- hdu-4611-Balls Rearrangement
- HDU 4611 Balls Rearrangement
- HDU 4611 Balls Rearrangement
- hdu-4611-Balls Rearrangement
- hdu 4611 Balls Rearrangement
- 4611 hdu Balls Rearrangement
- HDU 4611 Balls Rearrangement
- HDU 4611 Balls Rearrangement
- hdu 4611Balls Rearrangement
- HDU 4611Balls Rearrangement(思维)
- hdu - 4710/4611 - Balls Rearrangement
- [hdu 4611]Balls Rearrangement 数论
- HDU 4611 Balls Rearrangement 解题报告
- hdu 4611/4710 Balls Rearrangement 数论
- hdu 4611 Balls Rearrangement(数学:推理+模拟)
- hdu 4611 Balls Rearrangement(规律)
- js裁剪字符串
- C语言中的堆区和栈区
- Jquery 实现表格颜色交替变化,点击选中行,鼠标移过颜色变化。
- UVM实战指南——第1部分
- debian下simics安装过程和出错总结
- hdu 4611 Balls Rearrangement
- 帧内预测代码分析
- LeetCode - Gray Code
- 字符串消除
- PHP 生成js 数组 和保存为JS文件
- chrome extension 调试方式
- POJ 1163 最佳路径简单DP
- IOS 很多控件的讲解
- 【D3D11游戏编程】学习笔记二十:第一人称摄像机的实现