//  [7/31/2014 Sjm]
/*
看到此题直接先打表找规律(数学很糟糕,不会用公式推,只好打表找规律)
发现循环周期 T = A和B的最小公倍数
于是依靠区间 [min(a, b), T] 去求一个周期内的花费,但是各种优化后依旧TLE。。。。
不过,再次看打出来的表(举一个小例子):
测试用例: 15 5 30 1 2    3 4    5    6 7 8    9   10 11    12 13 14
%5    0 1 2    3 4    0    1 2 3    4    0  1     2  3  4
%3    0 1 2    0 1    2    0 1 2    0    1  2     0  1  2
cost  0 0 0    3 3    2    1 1 1    4    1  1     2  2  2在求cost值时,会发现在一段区间内有重复,而重复是因为 %5 和 %3 的余数递增的差值相同,
而能保持递增的差值相同的区间为: min(A-i%A, B-i%B),由此在求一个周期花费时,便可以跳着求了。。。实现了优化。。。
之前代码有些丑,,,改了一下。。。。
注意:
编写代码时,防止越界。。。。(代码中对此有注释)
*/
 1 #include <iostream>
 2 #include <cstdlib>
 3 #include <cstdio>
 4 typedef __int64 int64;
 5 using namespace std;
 6 int64 A, B;
 7
 8 int64 abs_int64(int64 x) {
 9     if (x >= 0) return x;
10     else return -x;
11 }
12
13 int64 Gcd(int64 a, int64 b) {
14     if (b == 0) return a;
15     else return Gcd(b, a%b);
16 }
17
18 int64 Min(int64 x, int64 y) {
19     if (x <= y) return x;
20     else return y;
21 }
22
23 int64 Solve(int64 lim) {
24     int64 sum = 0;
25     int64 i = 0;
26     int64 len;
27     while (i < lim) {
28         len = Min(A - i%A, B - i%B);
29         if (i + len > lim) {  // 注意此处,防止越界
30             len = lim - i;
31         }
32         sum += abs_int64(i%A - i%B)*len;
33         i += len;
34     }
35     return sum;
36 }
37
38 int main()
39 {
40     //freopen("input.txt", "r", stdin);
41     //freopen("output.txt", "w", stdout);
42     int T;
43     int64 lcm, len, val, sum, N;
44     scanf("%d", &T);
45     while (T--) {
46         scanf("%I64d %I64d %I64d", &N, &A, &B);
47         lcm = A / Gcd(A, B)*B;
48         if (N <= lcm) sum = Solve(N);
49         else sum = N / lcm*Solve(lcm) + Solve(N%lcm);
50         printf("%I64d\n", sum);
51     }
52     return 0;
53 }

转载于:https://www.cnblogs.com/shijianming/p/4140809.html

数学 之 hdu 4710 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 5761 Rower Bo

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5761 题目大意: 船在(0,a),船速v1,水速v2沿x轴正向,船头始终指向(0,0),问到达(0, ...

  3. 2013 ACM/ICPC Asia Regional Online —— Warmup1 1005 Balls Rearrangement

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4706 以前做过的题目: i从0到n-1时,如果一个一个加会很慢,注意到如果mod a的序列 和mod ...

  4. HDU 3636-Dragon Balls

    Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

  5. 数学--数论--HDU - 6395 Let us define a sequence as below 分段矩阵快速幂

    Your job is simple, for each task, you should output Fn module 109+7. Input The first line has only ...

  6. 数学--数论--HDU 12151七夕节 Plus (因子和线性筛)

    Problem Description 七夕节那天,月老来到数字王国,他在城门上贴了一张告示,并且和数字王国的人们说:"你们想知道你们的另一半是谁吗?那就按照告示上的方法去找吧!" ...

  7. 数学--数论--HDU - 6322 打表找规律

    In number theory, Euler's totient function φ(n) counts the positive integers up to a given integer n ...

  8. 数学--数论-- HDU 2601 An easy problem(约束和)

    Problem Description When Teddy was a child , he was always thinking about some simple math problems ...

  9. 数学--数论--HDU 6128 Inverse of sum (公式推导论)

    Description 给nn个小于pp的非负整数a1,-,na1,-,n,问有多少对(i,j)(1≤i<j≤n)(i,j)(1≤i<j≤n)模pp在意义下满足1ai+aj≡1ai+1aj ...

最新文章

  1. 爬虫练成之 analyst 和 engineer 技术与业务
  2. Vue计算属性和监听属性
  3. SQL日期时间和字符串函数
  4. addShutdownHook钩子
  5. qt 中转化图片格式与大小的方法
  6. java equ,Java equals方法详解
  7. MySQL搭建系列之多实例
  8. 【python教程入门学习】零基础想转行学python,过来人提醒大家几点
  9. RocketMQ事务消息从生产到消费原理详解(包括回查过程)
  10. 获得代理ippython_Python学习笔记六(免费获取代理IP)
  11. radius认证服务器无响应,squid radius认证“RADIUS服务器没有响应”
  12. 中国大学MOOC(慕课) 一个不错的学习网站
  13. 一款可以由电脑投屏到手机的软件deskreen[粉丝少于1000的电脑直播]
  14. 店铺DRS评分这样来做|盛天海电商
  15. IOS视频编辑,视频裁剪,视频拼接,音频处理,视频处理
  16. 台式计算机无线接入,台式电脑可以无线连接wifi吗
  17. 策略模式、工厂模式、装饰者模式总结解析
  18. Macbook双系统多分区的解决方案
  19. word2016加载MathType打开时显示“安全警告,宏已被禁用”解决办法
  20. 十大经典排序算法的动图

热门文章

  1. TensorFlow tf.feature_column
  2. 1.8 为什么是人的表现
  3. scrapy.spider
  4. 留学计算机美国硕士,美国硕士留学计算机专业申请详细分析
  5. python格式输出占四列左对齐_python – 打印字符串左对齐,固定宽度和后缀
  6. vhdl语言入门_初学Chisel语言,看这篇就够了:最方便简洁的入门资料整理
  7. AIX操作系统版本小知识(转载)
  8. vSphere 7.0 GA正式版发布
  9. JavaScript学习总结(12)——2016 年 7 个顶级 JavaScript 框架
  10. mybatis批量更新 mysql 报错,Mybatis批量更新报错问题