hdu - 3959 Board Game Dice(数学)
这道题比赛中没做出来,赛后搞了好久才出来的,严重暴露的我薄弱的数学功底,
这道题要推公式的,,,有类似于1*a+2*a^2+3*a^3+...+n*a^n的数列求和。
最后画了一张纸才把最后的结果推出来。::(x*M^x)/N.
而且通过这道题我发现有些数学函数还不如直接循环来的快,
例如这道题中求x的值的时候。
【我在此收回前面的话,昨天是oj的问题,今天我又交了一遍log的代码,耗时变成了0ms了。。。OMG】
方法一:
int x = int(log(n)/log(m)+0.5);
if(_pow(m,x)<n) x += 1;
耗时15ms
方法二:
int x;
for(x = 1; ; ++x)
if(_pow(m,x)>=n)
break;
耗时0ms
代码如下:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#include <algorithm> #define LL long long
#define M 10005
#define N 15 using namespace std; int n, m;
LL _pow(LL a, LL b)
{
if(b==0) return 1;
LL ans = _pow(a,b/2);
if(b&1) return ans*ans*a;
else return ans*ans;
}
LL gcd(LL a, LL b)
{
return b==0?a:gcd(b,a%b);
}
int main ()
{
int t, k = 0;
scanf("%d",&t);
while(t--)
{
scanf("%d%d", &n, &m);
int x;
for(x = 1; ; ++x)
if(_pow(m,x)>=n)
break;
LL a = _pow(m,x)*x;
LL b = n;
LL g = a>b?gcd(a,b):gcd(b,a);
printf("Case %d: %I64d/%I64d\n", ++k, a/g, b/g);
}
return 0;
}
hdu - 3959 Board Game Dice(数学)的更多相关文章
- HDU 5955 Guessing the Dice Roll
HDU 5955 Guessing the Dice Roll 2016 ACM/ICPC 亚洲区沈阳站 题意 有\(N\le 10\)个人,每个猜一个长度为\(L \le 10\)的由\(1-6\) ...
- hdu 5955 Guessing the Dice Roll 【AC自动机+高斯消元】
hdu 5955 Guessing the Dice Roll [AC自动机+高斯消元] 题意:给出 n≤10 个长为 L≤10 的串,每次丢一个骰子,先出现的串赢,问获胜概率. 题解:裸的AC自动机 ...
- HDU 4586 Play the Dice(数学期望)
Play the Dice Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tot ...
- HDU 4586 Play the Dice (数学,概率,等比公式,极限)
题意:给你一个n面的骰子每个面有一个值,然后其中有不同值代表你能获得的钱,然后有m个特殊的面,当你骰到这一面的时候可以获得一个新的机会 问你能得到钱的期望. 析: 骰第一次 sum/n 骰第二 ...
- HDU 5902 GCD is Funny 数学
GCD is Funny 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5902 Description Alex has invented a ne ...
- 2017"百度之星"程序设计大赛 - 复赛1003&&HDU 6146 Pokémon GO【数学,递推,dp】
Pokémon GO Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 5810 Balls and Boxes 数学
Balls and Boxes 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5810 Description Mr. Chopsticks is i ...
- hdu 1577 WisKey的眼神 (数学几何)
WisKey的眼神 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- hdu 4586 Play the Dice 概率推导题
A - Play the DiceTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/ ...
随机推荐
- Eclipse 和 HSQLDB: 将关系数据库服务器嵌入到 Eclipse 中,第 2 部分
HSQLDB 开发者角色 对 HSQLDB 与 Eclipse 工作台的集成感兴趣的开发者可以很容易地被分为两类: 客户机开发者,他们只是用 HSQLDB 来存储数据. 引擎开发者,他们通过添加新的标 ...
- 將後台的Json數據返回到前台
前台JS代碼 $.post('/Book/GetBookClassIDByName', { BookName: "旅遊手冊" }, function (data) { if (da ...
- WPF的图片操作效果(一):RenderTransform
一.RenderTransform类的成员: 1.TranslateTransform 平移效果 2.RotateTransform 旋转效果 3.ScaleTransform 缩放效果 ...
- no 'object' file generated
c++编译的时候出现如下的错误: no 'object' file generated [解决方法] Go to project properties -> configurations p ...
- mybaits中xml文件大于号和小于号的处理方法
1.转义字符 原符号 < <= > >= & ' " 替换符号 < <= > >= & ' " 2 ...
- linq to entity中遇到的问题
当使用 from m in _db.students从数据库中获取数据时,数据库中的数据类型和C#中的不同,所以可能会出错!先作_db.students.ToList()然后select
- Dobbo的继任者?试用微博RPC框架Motan
从14年开始就陆续看到新浪微博RPC框架Motan的介绍,时隔两年后,微博团队终于宣布开源轻量级RPC框架Motan,项目地址: https://github.com/weibocom/motan/ ...
- 《Linux内核设计与实现》读书笔记 第二章 从内核出发
一.获取内核源码 1. Git git实际上是一种开源的分布式版本控制工具. Linux作为一个开源的内核,其源代码也可以用git下载和管理 - 获取最新提交到版本树的一个副本 - $ git clo ...
- (转载)自定义 setDateFormat 显示格式
转自 http://blog.sina.com.cn/s/blog_67b27b60010130mr.html -(NSString *)getStringFromDate:(NSDate *)aDa ...
- 解决maven依赖传递中的版本冲突问题
通常情况下,我们都比较喜欢使用maven进行项目管理,要加个依赖包也非常简单,不需要到处去下载jar包,当然除了maven之外,也还有一些非常不错的工具.在使用maven进行项目依赖管理的时候,有时候 ...