以前好像是在UVa上貌似做过类似的,mod的剩余,今天比赛的时候受baofeng指点,完成了此道题

此题题意:求sum(|i%A-i%B|)(0<i<N-1)

A、B的循环节不同时,会有重叠,重叠后的区间里的数的值相等(可以证明,这里不给出了),然后压缩区间端点值,直接求区间和即可

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#define LL long long
using namespace std;
LL abs(LL a,LL b)
{
return (a-b)>(b-a)?(a-b):(b-a);
}
LL gcd(LL a,LL b)
{
return b==0?a:gcd(b,a%b);
}
LL lcm(LL a,LL b)
{
return a/gcd(a,b)*b;
}
LL t[200005];
LL d[200005];
LL f[200005];
int main()
{
int T;
LL n,a,b;
scanf("%d",&T);
while(T--)
{
scanf("%I64d%I64d%I64d",&n,&a,&b);
LL x=lcm(a,b);
LL A=(x/a);
LL B=(x/b);
LL ct=0;
for(LL i=1;i<=A;i++)
{
t[i]=i*a;
}
for(LL i=1;i<=B;i++)
{
t[A+i]=i*b;
}
sort(t+1,t+A+B+1);
d[ct++]=0;
d[ct++]=t[1];
for(LL i=2;i<=A+B;i++)
{
if(t[i]!=t[i-1])
d[ct++]=t[i];
}
for(LL i=1;i<ct;i++)
{
f[i]=abs(d[i-1]%a,d[i-1]%b)*(d[i]-d[i-1]);
}
for(LL i=1;i<ct;i++)
f[i]+=f[i-1];
LL ans=0,INT=n/x,MOD=n%x;
ans+=INT*f[ct-1];
for(LL i=1;i<ct;i++)
{
if(k<=d[i])
{
ans+=f[i-1]+abs(d[i-1]%a,d[i-1]%b)*(MOD-d[i-1]);
break;
}
}
printf("%I64d\n",ans);
}
return 0;
}

HDU 4611 - Balls Rearrangement(2013MUTC2-1001)(数学,区间压缩)的更多相关文章

  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

    http://acm.hdu.edu.cn/showproblem.php?pid=4611 从A中向B中移动和从B中向A中移动的效果是一样的,我们假设从B中向A中移动 而且A>B 我们先求出所 ...

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

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

  6. hdu 4710 Balls Rearrangement()

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

  7. hdu 4710 Balls Rearrangement

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

  8. hdu 4710 Balls Rearrangement 数论

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

  9. HDU 5810 Balls and Boxes ——(数学,概率,方差)

    官方题解看不太懂,参考了一些人的博客以后自己证明如下: 其中D(X)和E(X)的公式如下(参考自百度百科): 其中 p = 1 / m .(这是每一个单独事件发生的概率期望,在这里单独事件指的是一个球 ...

随机推荐

  1. Points

    CF#19D:http://codeforces.com/contest/19/problem/D 题意:给你一个点,add x,y表示向集合中添加一个点,remove x,y,表示删除集合中的一个点 ...

  2. ajax+json数据传输

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  3. POJ3295 Tautology(枚举)

    题目链接. 分析: 最多有五个变量,所以枚举所有的真假值,从后向前借助于栈验证是否为永真式. #include <iostream> #include <cstring> #i ...

  4. 【转】Android中的内存管理--不错不错,避免使用枚举类型

    原文网址:http://android-performance.com/android/2014/02/17/android-manage-memory.html 本文内容翻译自:http://dev ...

  5. Spark SQL Table Join(Python)

    示例   Spark SQL注册“临时表”执行“Join”(Inner Join.Left Outer Join.Right Outer Join.Full Outer Join)   代码   fr ...

  6. 【模拟】XMU 1062 山东煎饼

    题目链接: http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1062 题目大意: 已知金钱,和各面额钞票张数,问最少可以换成几张. 题目思路: [模拟 ...

  7. HDOJ 1014 Uniform Generator(公约数问题)

    Problem Description Computer simulations often require random numbers. One way to generate pseudo-ra ...

  8. HDU 4627 There are many unsolvable problem in the world.It could be about one or about zero.But this time it is about bigger number.

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82974#problem/E 解题思路:数论,从一个数的中间开始往两边找,找到两 ...

  9. Android中bitmap的相关处理

    加载大图片 Options options=new Options(); options.inJustDecodeBounds=true;//不加载图片,只加载文件信息 //加载图片,获取到配置信息 ...

  10. 把struts2-convention-plugin丢进太平洋

    struts2-convention-plugin是一个插件,通过这个插件可以实现对于struts2的零配置,基本用法可以参考这个博客(http://javeye.iteye.com/blog/358 ...