您的位置:首页 > 其它

HDU 4611 Balls Rearrangement (数学-思维逻辑题)

2013-08-25 20:49 447 查看
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4611

题意:给你一个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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: