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/ ...
随机推荐
- mybaties中的selectKey和useGeneratedKeys=true
<!-- 账户创建 --><insert id="create" parameterType="Account"> <select ...
- NHibernate系列文章六:NHibernate数据类型映射
摘要 NHibernate支持所有的数据库数据类型. 以SQL Server数据库为例,下表是NHibernate支持的SQL Server数据库最常见的数据类型对照表. 第一列是NHibernate ...
- delphi XE Berlin ReadProcessMemory WriteProcessMemory
delphi XE,Berlin [dcc32 Error] Unit9.pas(93): E2033 Types of actual and formal var parameters must ...
- CSS盒子模型学习记录1
http://www.blueidea.com/tech/web/2007/4545.asp 代码试验: html代码: <!DOCTYPE html PUBLIC "-//W3C// ...
- Linxu学习之03_LInux文件与目录管理
同样只介绍相关命令 这节相关主要的命令有这些: 1.目录的相关操作 cd----切换目录 pwd----显示当前目录 mkdir----新建一个新的目录 rmdir----删除一个空的目录
- Win7 64位系统 VS2010连接Oracle报错的问题
1,异常现象: TNS无法识别 2,异常分析:VS2010启动调试的时候,出现异常.但是使用IIS作为服务器的时候,是不会出现异常,也就是可以正常访问到数据库,后来分析是由于VS自带服务器ASP.NE ...
- GUI开发者桌面搜索文件工具
# - *- coding:utf-8-*-from Tkinter import *import tkMessageBoximport tkFileDialogimport osimport fnm ...
- rutime中动态调用类的方法
Dynamically invoke a class method in Objective C 代码 #import <Foundation/Foundation.h> #import ...
- C语课设心得分享(三)
调试. 以前咱们写课后习题,一般也不需要使用调试,如果程序编译error,根据错误信息就可以改好:如果是结果错误,那么在稿纸上过几遍基本也可以得出结果. 但咱们这个课设比较大,就需要很多调试的过程,尤 ...
- java.io.Serializable 序列化问题
java.io.Serializable 序列化问题 Person.java package a.b.c; public class Person implements java.io.Seriali ...