题意就不说了,刚开始做我竟然傻傻地去模拟,智商捉急啊~~超时是肯定的

  求出 a ,b 的最小公倍数,因为n够长的话,就会出现循环,所以就不要再做不必要的计算了。如果最小公倍数大于n的话,就直接计算n吧。

  除了可以应用循环来节省计算,还可以成段计算来节省。

  1. #include<stdio.h>
  2. #include<string.h>
  3. #define i64 __int64
  4.  
  5. i64 abs(i64 x)
  6. {
  7. if(x<)
  8. return -x;
  9. return x;
  10. }
  11. i64 gcd(i64 a,i64 b)
  12. {
  13. if(b==) return a;
  14. return gcd(b,a%b);
  15. }
  16. i64 Deal(i64 len,i64 a,i64 b)
  17. {
  18. int i=;
  19. i64 ans=;
  20. while(i<len)
  21. {
  22. int tmp = (a-i%a)>(b-i%b)?(b-i%b):(a-i%a);
  23. if(tmp+i>len)
  24. tmp=len-i;
  25. ans+=abs(i%a-i%b)*tmp;
  26. i+=tmp;
  27. }
  28. return ans;
  29. }
  30. int main()
  31. {
  32. i64 N,A,B;
  33. int T;
  34. i64 ans;
  35. scanf("%d",&T);
  36. while(T--)
  37. {
  38. scanf("%I64d%I64d%I64d",&N,&A,&B);
  39. if(A==B)
  40. {
  41. printf("0\n");
  42. continue;
  43. }
  44. i64 tmp = A/gcd(A,B)*B;
  45. if(tmp>N) ans=Deal(N,A,B);
  46. else ans=Deal(tmp,A,B)*(N/tmp)+Deal(N%tmp,A,B);
  47. printf("%I64d\n",ans);
  48. }
  49. return ;
  50. }

hdu 4710 Balls Rearrangement的更多相关文章

  1. hdu 4710 Balls Rearrangement()

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

  2. hdu 4710 Balls Rearrangement 数论

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

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

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

  4. hduoj 4710 Balls Rearrangement 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4710 Balls Rearrangement Time Limit: 6000/3000 MS (Java/Ot ...

  5. HDU 4611 Balls Rearrangement 数学

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

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

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

  7. hdu 4611 Balls Rearrangement

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

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

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

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

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

随机推荐

  1. Spring 入门 AOP

    通过一个小例子演视怎么使用 Spring 现实面向切面编程. 导入 Spring 所需要的包 spring-framework-2.5.6 版需要导入以下包: 1.----- spring.jar 2 ...

  2. 百度地图坐标转换API和地图API

    利用百度地图的服务将经纬度转换为米单位坐标 using System; using System.Collections.Generic; using System.Linq; using Syste ...

  3. HtmlParser基础教程

    1.相关资料 官方文档:http://htmlparser.sourceforge.net/samples.html API:http://htmlparser.sourceforge.net/jav ...

  4. tcl/tk demo

    环境及版本说明: OSX10.9 tclsh -> tclsh8.5 wish -> wish8.5 查看本机运行环境: which wish; 2 /usr/bin/wish which ...

  5. ACboy needs your help again!--hdu1702

    ACboy needs your help again! Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  6. codeforces 518C. Anya and Smartphone

    C. Anya and Smartphone time limit per test 1 second memory limit per test 256 megabytes input standa ...

  7. Problem 1008 Hay Points

    Problem Description Each employee of a bureaucracy has a job description - a few paragraphs that des ...

  8. Nginx Upload Module 上传模块

    传统站点在处理文件上传请求时,普遍使用后端编程语言处理,如:Java.PHP.Python.Ruby等.今天给大家介绍Nginx的一个模块,Upload Module上传模块,此模块的原理是先把用户上 ...

  9. Java Script 中 ==(Equal) 和 === (Identity Equal) 的区别和比较算法逻辑

    判断两个变量是否相等在任何编程语言中都是非常重要的功能. JavaScript 提供了 == 和 === 两种判断两个变量是否相等的运算符,但我们开始学习的时候 JavaScript 的时候,就被一遍 ...

  10. Checkbutton 和 Radiobutton

    The Checkbutton widget is used to display a number of options to a user as toggle buttons. The user ...