题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4611

  先求出循环节,然后比较A和B的大小模拟过去。。。

 //STATUS:C++_AC_15MS_436KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
#pragma comment(linker,"/STACK:102400000,102400000")
using namespace std;
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=,M=;
const int INF=0x3f3f3f3f;
const int MOD=,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e15;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End int T;
LL n,A,B,t,late; LL slove(LL upa,LL upb,int flag)
{
int i,j,a,b,la,lb,ok=;
LL ret=;
for(i=a=b=;;){
la=upa-a+;lb=upb-b+;
if(la<lb){
if(ok && i+la>=late){
t=ret+(late-i)*abs(a-b);
ok=;
if(!flag)break;
}
ret+=(LL)la*abs(a-b);
a=;b=(b+la)%B;
}
else{
if(ok && i+lb>=late){
t=ret+(late-i)*abs(a-b);
ok=;
if(!flag)break;
}
ret+=(LL)lb*abs(a-b);
b=;a=(a+lb)%A;
}
i+=Min(la,lb);
if(a==b)break;
}
return ret;
} int main()
{
// freopen("in.txt","r",stdin);
int i,j;
LL ans,cir;
scanf("%d",&T);
while(T--)
{
scanf("%I64d%I64d%I64d",&n,&A,&B); cir=lcm(A,B);
late=n-n/cir*cir;
ans=slove(A-,B-,n/cir)*(n/cir); printf("%I64d\n",ans+t);
}
return ;
}

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 从A中向B中移动和从B中向A中移动的效果是一样的,我们假设从B中向A中移动 而且A>B 我们先求出所 ...

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

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

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

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

  6. hdu 4291 矩阵幂 循环节

    http://acm.hdu.edu.cn/showproblem.php?pid=4291 凡是取模的都有循环节-----常数有,矩阵也有,并且矩阵的更奇妙: g(g(g(n))) mod 109  ...

  7. hdu 4794 FIb求循环节

    很容易看出来这道题是求模n意义下fib数列的最小循环节 对于fib数列的最小循环节的求法,我们可以这样: 1.令n=p1^m1 * p2^m2 * p3^m3…… 2.分别计算fib数列在模p1^m1 ...

  8. Cyclic Nacklace - HDU 3746(next求循环节)

    题目大意:给你一些串,问如果想让这个串里面的循环节至少循环两次,需要添加几个字符(只能在最前面或者最后面添加).比如ababc 需要添加5个就是添加ababc. 分析:其实字符串的长度len-next ...

  9. HDU 6740 kmp最小循环节

    题意:给一个无线循环小数的前几位,给n,m 选择其中一种出现在前几位的循环节方式(a个数),循环节的长度b 使得n*a-m*b最大 样例: 2 1 12.1212 输出 6 选择2,2*1-1*1=1 ...

随机推荐

  1. 用 OneAPM Cloud Insight 监控 Docker 性能

    Docker 是构建和部署软件的一个新兴的轻量级的平台,也是一个减轻替代虚拟机的容器.Docker 通过给开发者提供兼容不同环境的镜像,成为解决现代基础设施的持续交付的一个流行的解决方案. 和虚拟机一 ...

  2. linux 文件比对总结

    1. 过滤a.log的重复数据 #统计 cat datatest.log|sort|uniq -d |wc -l #放入b.log cat datatest.log|sort|uniq -d > ...

  3. linux 磁盘读写性能测试

    1. 测试读取速度 haparm -Tt /dev/xxx 1.1 获取硬盘设备名称: fdisk -l Disk /dev/xvdf: 365.0 GB, 365041287168 bytes 25 ...

  4. http://www.ibm.com/developerworks/cn/java/j-lo-hotswapcls/

    http://www.ibm.com/developerworks/cn/java/j-lo-hotswapcls/

  5. android 安全需要关注

    1.通过签名校验保护,能有效避免应用被二次打包,杜绝盗版应用的产生2.对内存数据进行变换处理和动态跟踪,有效防止数据被获取和修改3.对代码进行加密压缩,可防止破解者还原真实代码逻辑,避免被复制4.多重 ...

  6. [itint5]最大子矩阵和

    http://www.itint5.com/oj/#39 最大子矩阵和,复杂度O(n^3).利用了最大子段和的方法. int maxRectSum(vector<vector<int> ...

  7. delphi 反射(原理)

    关于反射的用途是『降低模块间的耦合度』这个倒未必尽然 单就delphi来说,从实现上看,它的所谓反射是基于RTTI,而RTTI的出现按照官方的说法是为了实现RAD中窗体文件DFM的持久化而产生的,其实 ...

  8. Android:控件布局(线性布局)LinearLayout

    LinearLayout是线性布局控件:要么横向排布,要么竖向排布 决定性属性:必须有的! android:orientation:vertical (垂直方向) .horizontal(水平方向) ...

  9. 219. Contains Duplicate II

    题目: Given an array of integers and an integer k, find out whether there are two distinct indices i a ...

  10. 113. Path Sum II

    题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...