HDU 4611 Balls Rearrangement (数学-思维逻辑题)
2013-08-25 20:49
447 查看
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4611
题意:给你一个N、A、B,要你求
AC代码:
题意:给你一个N、A、B,要你求
AC代码:
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <cstdlib> #include <cmath> #include <vector> #include <list> #include <deque> #include <queue> #include <iterator> #include <stack> #include <map> #include <set> #include <algorithm> #include <cctype> #include <ctime> #pragma comment(linker, "/STACK:16777216") using namespace std; typedef __int64 LL; const int N=10005; const int INF=0x3f3f3f3f; const double PI=acos(-1.0); LL gcd(LL a,LL b) { if(b==0) return a; return gcd(b,a%b); } LL lcm(LL a,LL b) { return a/gcd(a,b)*b; } LL cost(LL n,LL a,LL b) { LL ans=0,x=0,y=0,tmp=0,now=0; while(now<n) { tmp=min(a-x,b-y);//找到最近达到0的有几个数,也就是当前有几个相同的数字 if(tmp+now>n) tmp=n-now; ans+=tmp*(abs(x-y)); x=(x+tmp)%a; y=(y+tmp)%b; now+=tmp; } return ans; } int main() { int t; cin>>t; LL n,a,b; while(t--) { cin>>n>>a>>b; LL l=lcm(a,b); if(l<=n) cout<<cost(l,a,b)*(n/l)+cost(n%l,a,b)<<endl; else cout<<cost(n,a,b)<<endl; } return 0; }
相关文章推荐
- HDU 4611 - Balls Rearrangement(2013MUTC2-1001)(数学,区间压缩)
- hdu 4710 Balls Rearrangement (数学思维)
- hdu 4611 Balls Rearrangement(数学:推理+模拟)
- HDU 4611 - Balls Rearrangement(2013MUTC2-1001)(数学,区间压缩)
- HDU 4611 Balls Rearrangement 数学 循环节优化
- HDU 4611 Balls Rearrangement 数学
- hdu 4710 Balls Rearrangement (数学思维)
- hdu 4611 Balls Rearrangement
- HDU 4611 Balls Rearrangement
- HDU 4611 Balls Rearrangement
- hdu 4611 Balls Rearrangement (13多校#2-A)
- 2013 多校第二场 hdu 4611 Balls Rearrangement
- hdu-4611-Balls Rearrangement
- hdu 4611 Balls Rearrangement (13多校#2-A)
- hdu 4611 Balls Rearrangement (13多校#2-A)
- HDU 4611--Balls Rearrangement--数论找出循环节+跳跃求值
- HDU 4710 && HDU 4611 Balls Rearrangement (热身赛第五题)
- HDU 4611 Balls Rearrangement
- hdu 4611 Balls Rearrangement(规律)
- 数学 之 hdu 4710 Balls Rearrangement