HDU 4710 Balls Rearrangement
来源:互联网 发布:kcf跟踪算法原理 编辑:程序博客网 时间:2024/05/19 02:02
Bob has N balls and A boxes. He numbers the balls from 0 to N-1, and numbers the boxes from 0 to A-1. To find the balls easily, he puts the ball numbered x into the box numbered a if x = a mod A.
Some day Bob buys B new boxes, and he wants to rearrange the balls from the old boxes to the new boxes. The new boxes are numbered from 0 to B-1. After the rearrangement, the ball numbered x should be in the box number b if x = b mod B.
This work may be very boring, so he wants to know the cost before the rearrangement. If he moves a ball from the old box numbered a to the new box numbered b, the cost he considered would be |a-b|. The total cost is the sum of the cost to move every ball, and it is what Bob is interested in now.
Some day Bob buys B new boxes, and he wants to rearrange the balls from the old boxes to the new boxes. The new boxes are numbered from 0 to B-1. After the rearrangement, the ball numbered x should be in the box number b if x = b mod B.
This work may be very boring, so he wants to know the cost before the rearrangement. If he moves a ball from the old box numbered a to the new box numbered b, the cost he considered would be |a-b|. The total cost is the sum of the cost to move every ball, and it is what Bob is interested in now.
Input
The first line of the input is an integer T, the number of test cases.(0<T<=50)
Then T test case followed. The only line of each test case are three integers N, A and B.(1<=N<=1000000000, 1<=A,B<=100000).
Then T test case followed. The only line of each test case are three integers N, A and B.(1<=N<=1000000000, 1<=A,B<=100000).
Output
For each test case, output the total cost.
Sample Input
31000000000 1 18 2 411 5 3
Sample Output
0816
打表可知 求出a b的最小公倍数 为一个周期
i%a-i%b 如果分成一段一段的话 跟i的大小可以无关 下面用的是tmp记录 然后now更新走了多长 x,y也不段更新 就可求出答案
#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>using namespace std;long long gcd(long long x,long long y){ return y==0?x:gcd(y,x%y);}long long Count(long long n,long long a,long long b){ long long now=0,ans=0,tmp,x=0,y=0; while(now<n) { tmp=min(a-x,b-y); if (now+tmp>n) tmp=n-now; ans+=tmp*abs(x-y); x=(x+tmp)%a; y=(y+tmp)%b; now+=tmp; } return ans;}int main(){ int i,j,T; long long a,b,n,l,ans; scanf("%d",&T); while(T--) { cin>>n>>a>>b; l=a*b/gcd(a,b); if (l>=n) ans=Count(n,a,b); else ans=Count(l,a,b)*(n/l)+Count(n%l,a,b); cout<<ans<<endl; } return 0;}
0 0
- hdu 4710 Balls Rearrangement
- HDU 4710 Balls Rearrangement
- HDU - 4710 Balls Rearrangement
- hdu 4710 Balls Rearrangement
- hdu 4710 Balls Rearrangement 数论
- hdu - 4710/4611 - Balls Rearrangement
- hdu(4710) Balls Rearrangement
- HDU 4710 Balls Rearrangement(数论)
- 【瞎搞】 HDU 4710 Balls Rearrangement
- hdu 4611/4710 Balls Rearrangement 数论
- hdu 4710 Balls Rearrangement (数学思维)
- 数学 之 hdu 4710 Balls Rearrangement
- HDU 4710 Balls Rearrangement (纯数学)
- hdu 4611 Balls Rearrangement
- hdu 4611 Balls Rearrangement
- hdu-4611-Balls Rearrangement
- HDU 4611 Balls Rearrangement
- HDU 4611 Balls Rearrangement
- 远程执行命令的psexec工具的基本使用——简单、强大
- HDU 2094 产生冠军(拓扑排序)
- spring quartz定时器,及其动态处理方式
- 仿淘宝购物车demo---增加和减少商品数量
- javascript字符串
- HDU 4710 Balls Rearrangement
- 个人对深度优先搜索和广(宽)度优先搜索的理解
- BPM那些事儿——BPM的产生缘由
- UVA 10055 -Hashmat the Brave Warrior
- iOS申请开发者账号问题总结
- Android性能优化(一),总纲
- Nginx发布1.9.0版本,新增支持TCP代理和负载均衡的stream模块
- spring如何使用多个xml配置文件
- Cognos筛选条件增加全选选项