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的更多相关文章

  1. HDU 4611 Balls Rearrangement 数学

    Balls Rearrangement 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4611 Description Bob has N balls ...

  2. HDU 4611 Balls Rearrangement(2013多校2 1001题)

    Balls Rearrangement Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Othe ...

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

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4611 题意:给你一个N.A.B,要你求 AC代码: #include <iostream> ...

  4. HDU 4611 - Balls Rearrangement(2013MUTC2-1001)(数学,区间压缩)

    以前好像是在UVa上貌似做过类似的,mod的剩余,今天比赛的时候受baofeng指点,完成了此道题 此题题意:求sum(|i%A-i%B|)(0<i<N-1) A.B的循环节不同时,会有重 ...

  5. hdu 4710 Balls Rearrangement()

    http://acm.hdu.edu.cn/showproblem.php?pid=4710 [code]: #include <iostream> #include <cstdio ...

  6. hdu 4710 Balls Rearrangement

    题意就不说了,刚开始做我竟然傻傻地去模拟,智商捉急啊~~超时是肯定的 求出 a ,b 的最小公倍数,因为n够长的话,就会出现循环,所以就不要再做不必要的计算了.如果最小公倍数大于n的话,就直接计算n吧 ...

  7. hdu 4710 Balls Rearrangement 数论

    这个公倍数以后是循环的很容易找出来,然后循环以内的计算是打表找的规律,规律比较难表述,自己看代码吧.. #include <iostream> #include <cstdio> ...

  8. hdu 4710 Balls Rearrangement (数学思维)

    意甲冠军:那是,  从数0-n小球进入相应的i%a箱号.然后买一个新的盒子. 今天的总合伙人b一个盒子,Bob试图把球i%b箱号. 求复位的最小成本. 每次移动的花费为y - x ,即移动前后盒子编号 ...

  9. 2013 多校联合 2 A Balls Rearrangement (hdu 4611)

    Balls Rearrangement Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Othe ...

随机推荐

  1. [转载] LinkedIn架构这十年

    原文: http://colobu.com/2015/07/24/brief-history-scaling-linkedin/ 原文: A Brief History of Scaling Link ...

  2. Javascript链式调用案例

    jQuery用的就是链式调用.像一条连接一样调用方法. 链式调用的核心就是return this;,每个方法都返回对象本身. 下面是简单的模拟jQuery的代码, <script> win ...

  3. 如何查看与刷新DNS本地缓存

    如何查看与刷新DNS本地缓存 一.查看DNS本地缓存 在cmd窗口输入:ipconfig/displaydns 二.刷新DNS本地缓存 在cmd窗口输入:ipconfig/flushdns 之后输入: ...

  4. Python 调用自定义包

    创建包 # mkdir -p /python/utils # touch /python/utils/__init__.py # vi /python/utils/Log.pyimport timed ...

  5. -bash: jps: command not found

    linux安装了jdk之后,打jps命令发现找不到这个命令: -bash: jps: command not found 查看java版本java -version,正常. java version ...

  6. Hbase之更新单条数据

    import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; impo ...

  7. 抛弃vboot不格盘用grub4dos+firadisk安装Ghost版XP到VHD,轻松RAMOS!

    http://bbs.wuyou.net/forum.php?mod=viewthread&tid=363198&extra=抛弃vboot不格盘用grub4dos+firadisk安 ...

  8. JQUERY学习(壹)

    一.jQuery的引言 1.jQuery框架:对JavaScript的封装,简化js开发 2.jQuery框架的好处: 1)语法简单 js中:document.getElementById(" ...

  9. python 练习24

    Python for循环可以遍历任何序列的项目,如一个列表或者一个字符串. 语法: for循环的语法格式如下: for iterating_var in sequence: statements(s) ...

  10. Compound Interest Calculator4.0

    Compound Interest Calculator4.0 1.团队协作准备:每个同学在github上完成FORK,COMMENT(学号后三位+姓名),PR,MERGE的过程. 2.你的RP由你的 ...