hdu 4710 Balls Rearrangement
题意就不说了,刚开始做我竟然傻傻地去模拟,智商捉急啊~~超时是肯定的
求出 a ,b 的最小公倍数,因为n够长的话,就会出现循环,所以就不要再做不必要的计算了。如果最小公倍数大于n的话,就直接计算n吧。
除了可以应用循环来节省计算,还可以成段计算来节省。
- #include<stdio.h>
- #include<string.h>
- #define i64 __int64
- i64 abs(i64 x)
- {
- if(x<)
- return -x;
- return x;
- }
- i64 gcd(i64 a,i64 b)
- {
- if(b==) return a;
- return gcd(b,a%b);
- }
- i64 Deal(i64 len,i64 a,i64 b)
- {
- int i=;
- i64 ans=;
- while(i<len)
- {
- int tmp = (a-i%a)>(b-i%b)?(b-i%b):(a-i%a);
- if(tmp+i>len)
- tmp=len-i;
- ans+=abs(i%a-i%b)*tmp;
- i+=tmp;
- }
- return ans;
- }
- int main()
- {
- i64 N,A,B;
- int T;
- i64 ans;
- scanf("%d",&T);
- while(T--)
- {
- scanf("%I64d%I64d%I64d",&N,&A,&B);
- if(A==B)
- {
- printf("0\n");
- continue;
- }
- i64 tmp = A/gcd(A,B)*B;
- if(tmp>N) ans=Deal(N,A,B);
- else ans=Deal(tmp,A,B)*(N/tmp)+Deal(N%tmp,A,B);
- printf("%I64d\n",ans);
- }
- return ;
- }
hdu 4710 Balls Rearrangement的更多相关文章
- hdu 4710 Balls Rearrangement()
http://acm.hdu.edu.cn/showproblem.php?pid=4710 [code]: #include <iostream> #include <cstdio ...
- hdu 4710 Balls Rearrangement 数论
这个公倍数以后是循环的很容易找出来,然后循环以内的计算是打表找的规律,规律比较难表述,自己看代码吧.. #include <iostream> #include <cstdio> ...
- hdu 4710 Balls Rearrangement (数学思维)
意甲冠军:那是, 从数0-n小球进入相应的i%a箱号.然后买一个新的盒子. 今天的总合伙人b一个盒子,Bob试图把球i%b箱号. 求复位的最小成本. 每次移动的花费为y - x ,即移动前后盒子编号 ...
- 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 ...
- HDU 4611 Balls Rearrangement 数学
Balls Rearrangement 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4611 Description Bob has N balls ...
- HDU 4611 Balls Rearrangement(2013多校2 1001题)
Balls Rearrangement Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Othe ...
- hdu 4611 Balls Rearrangement
http://acm.hdu.edu.cn/showproblem.php?pid=4611 从A中向B中移动和从B中向A中移动的效果是一样的,我们假设从B中向A中移动 而且A>B 我们先求出所 ...
- HDU 4611 Balls Rearrangement (数学-思维逻辑题)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4611 题意:给你一个N.A.B,要你求 AC代码: #include <iostream> ...
- HDU 4611 - Balls Rearrangement(2013MUTC2-1001)(数学,区间压缩)
以前好像是在UVa上貌似做过类似的,mod的剩余,今天比赛的时候受baofeng指点,完成了此道题 此题题意:求sum(|i%A-i%B|)(0<i<N-1) A.B的循环节不同时,会有重 ...
随机推荐
- Spring 入门 AOP
通过一个小例子演视怎么使用 Spring 现实面向切面编程. 导入 Spring 所需要的包 spring-framework-2.5.6 版需要导入以下包: 1.----- spring.jar 2 ...
- 百度地图坐标转换API和地图API
利用百度地图的服务将经纬度转换为米单位坐标 using System; using System.Collections.Generic; using System.Linq; using Syste ...
- HtmlParser基础教程
1.相关资料 官方文档:http://htmlparser.sourceforge.net/samples.html API:http://htmlparser.sourceforge.net/jav ...
- tcl/tk demo
环境及版本说明: OSX10.9 tclsh -> tclsh8.5 wish -> wish8.5 查看本机运行环境: which wish; 2 /usr/bin/wish which ...
- ACboy needs your help again!--hdu1702
ACboy needs your help again! Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- codeforces 518C. Anya and Smartphone
C. Anya and Smartphone time limit per test 1 second memory limit per test 256 megabytes input standa ...
- Problem 1008 Hay Points
Problem Description Each employee of a bureaucracy has a job description - a few paragraphs that des ...
- Nginx Upload Module 上传模块
传统站点在处理文件上传请求时,普遍使用后端编程语言处理,如:Java.PHP.Python.Ruby等.今天给大家介绍Nginx的一个模块,Upload Module上传模块,此模块的原理是先把用户上 ...
- Java Script 中 ==(Equal) 和 === (Identity Equal) 的区别和比较算法逻辑
判断两个变量是否相等在任何编程语言中都是非常重要的功能. JavaScript 提供了 == 和 === 两种判断两个变量是否相等的运算符,但我们开始学习的时候 JavaScript 的时候,就被一遍 ...
- Checkbutton 和 Radiobutton
The Checkbutton widget is used to display a number of options to a user as toggle buttons. The user ...