hdu 4611 Balls Rearrangement
http://acm.hdu.edu.cn/showproblem.php?pid=4611
从A中向B中移动和从B中向A中移动的效果是一样的,我们假设从B中向A中移动 而且A>B
我们先求出所有在B[0]上的点移动到A上的分布情况 可以求出花费
当我们要求B[1]上的点移动到A上的分布情况时 相当于B[0]在A上的分布情况水平右移一个单位
由于B[1]点对于B[0]也向后移动了一个单位 所有相对位置没有移动
但是A中有一个地方需要改动 那就是每次A中最后一个位置的点需要移动到A[0]位置(因为对A进行取模的原因)
还有一个要注意的情况就是 B 在向后移动的时候 点的个数可能会减小 要特殊处理一下
代码:
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<set>
#include<map>
#include<stack>
#include<vector>
#include<algorithm>
#include<queue>
#include<bitset>
#include<deque>
#include<numeric> //#pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; typedef long long ll;
typedef unsigned int uint;
typedef pair<int,int> pp;
const double eps=1e-9;
const int INF=0x3f3f3f3f;
const ll MOD=1000000007;
const int H=210000;
const int K=100001;
ll a[H];
int f(int x)
{
return x+K;
}
ll readya(ll N,ll A,ll B)
{
ll lcm=A*B/(__gcd(A,B));
int n=lcm/B;
int k=0;
ll num=(N/B);
if(N%B) ++num;
for(int i=0;i<n;++i)
{
a[f(k)]+=num/n;
k=(k+B)%A;
}
ll M=num%n;
k=0;
while(M--)
{
a[f(k)]++;
k=(k+B)%A;
}
ll tmp=0;
for(int i=1;i<A;++i)
tmp+=a[f(i)]*i;
return tmp;
}
int main()
{
//freopen("data.in","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
memset(a,0,sizeof(a));
ll N,A,B;
cin>>N>>A>>B;
if(B>A) swap(A,B);
if(A==B)
{printf("0\n");continue;}
ll L=0;
ll R=readya(N,A,B);
int num=N/B;
if(N%B) ++num;
ll ans=L+R;
for(int i=1;i<B;++i)
{
a[f(-i)]=a[f(A-i)];
R-=(a[f(A-i)]*(A-i));
L+=(a[f(-i)]*i);
a[f(A-i)]=0;
int tmp=(N-i)/B;
if((N-i)%B) ++tmp;
if(tmp!=num)
{
int x=tmp*B+i;
a[f(x%A-i)]--;
if(x%A-i>0) {R-=abs(x%A-i);}
if(x%A-i<0) {L-=abs(x%A-i);}
num=tmp;
}
ans+=(L+R);
}
cout<<ans<<endl;
}
return 0;
}
hdu 4611 Balls Rearrangement的更多相关文章
- HDU 4611 Balls Rearrangement 数学
Balls Rearrangement 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4611 Description Bob has N balls ...
- HDU 4611 Balls Rearrangement(2013多校2 1001题)
Balls Rearrangement Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Othe ...
- HDU 4611 Balls Rearrangement (数学-思维逻辑题)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4611 题意:给你一个N.A.B,要你求 AC代码: #include <iostream> ...
- HDU 4611 - Balls Rearrangement(2013MUTC2-1001)(数学,区间压缩)
以前好像是在UVa上貌似做过类似的,mod的剩余,今天比赛的时候受baofeng指点,完成了此道题 此题题意:求sum(|i%A-i%B|)(0<i<N-1) A.B的循环节不同时,会有重 ...
- hdu 4710 Balls Rearrangement()
http://acm.hdu.edu.cn/showproblem.php?pid=4710 [code]: #include <iostream> #include <cstdio ...
- hdu 4710 Balls Rearrangement
题意就不说了,刚开始做我竟然傻傻地去模拟,智商捉急啊~~超时是肯定的 求出 a ,b 的最小公倍数,因为n够长的话,就会出现循环,所以就不要再做不必要的计算了.如果最小公倍数大于n的话,就直接计算n吧 ...
- hdu 4710 Balls Rearrangement 数论
这个公倍数以后是循环的很容易找出来,然后循环以内的计算是打表找的规律,规律比较难表述,自己看代码吧.. #include <iostream> #include <cstdio> ...
- hdu 4710 Balls Rearrangement (数学思维)
意甲冠军:那是, 从数0-n小球进入相应的i%a箱号.然后买一个新的盒子. 今天的总合伙人b一个盒子,Bob试图把球i%b箱号. 求复位的最小成本. 每次移动的花费为y - x ,即移动前后盒子编号 ...
- 2013 多校联合 2 A Balls Rearrangement (hdu 4611)
Balls Rearrangement Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Othe ...
随机推荐
- 【Todo】秒杀系统材料
秒杀系统:Link <一个经验证可落地的秒杀系统实践思路> 主要依赖于Redis进行处理. http://geek.csdn.net/news/detail/59847 淘宝大秒系统设 ...
- iOS开发之 Xcode svn更新代码后,不能打开.xcodeproj,因为该项目文件不能被解析
http://www.cfanz.cn/?c=article&a=read&id=41565 解决方法: 1.对.xcodeproj 文件右键,显示包内容 2.双击打开 proj ...
- PHP 注册树模式
/** * 注册树模式 * 将对象注册到一个类中 * 通过该类实现全局访问操作对象 */ class Tree { private static $treeList = []; private fun ...
- MyBatis——Mybatis缓存
原文:http://www.cnblogs.com/xdp-gacl/p/4270403.html MyBatis学习总结(七)--Mybatis缓存 一.MyBatis缓存介绍 正如大多数持久层框架 ...
- Java troubleshooting guide
http://www.oracle.com/technetwork/java/javase/toc-135973.html --不同的 OutOfMemoryError/内存溢出,以及相关的解决
- samba服务器搭建
为实现windows与linux资源共享,搭建一个samba服务器:这个我也是探索了一段时间. 找到一篇写得比较清楚的博客: http://yangxuejun.blog.51cto.com/6239 ...
- android中的通信机制总结
第一种:使用handler来进行通信 handler 大家可以把它想象成主线程(UI线程)的一个子线程,它可以给主线程(UI线程)发送数据从而更新主线程(UI线程)的UI与逻辑,handler ...
- 例题:打印乘法口诀。可能大家一看有点难,但只要理解for 循环嵌套,两层循环,外层循环行数,里层循环列数,搞清楚行数和列数之间的关系,就可以轻松做出这道题
namespace 打印乘法口诀{ class Program { static void Main(string[] args) { f ...
- is(C# 参考)
检查对象是否与给定类型兼容. 例如,下面的代码可以确定对象是否为 MyObject 类型的一个实例,或者对象是否为从 MyObject 派生的一个类型: if (obj is MyObject ...
- 根据不同的屏幕宽度引入不同的css文件
<link rel="stylesheet" href="css/jl_public.css?v=11"/> <link rel=" ...