hdu - 3959 Board Game Dice(数学)
来源:互联网 发布:沙迪克火花机怎样编程 编辑:程序博客网 时间:2024/05/17 07:20
这道题比赛中没做出来,赛后搞了好久才出来的,严重暴露的我薄弱的数学功底,
这道题要推公式的,,,有类似于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 15using 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 3959 Board Game Dice
- 阿里巴巴公开赛 HDU 3959 Board Game Dice(数学)
- nyoj 408 Board Game Dice
- NYOJ--408题Board Game Dice
- 2011阿里巴巴程序设计公开赛 / 1009 Board Game Dice
- HDUBoard Game Dice (数学期望)
- HDU 4586 Play the Dice 数学期望
- HDU-4586 Play the Dice (数学)
- HDU 4586 Play the dice (数学期望)
- HDU 4586:Play the Dice (数学)
- HDU 4518 Play the Dice(数学 概率 期望)
- Play the Dice hdu 4586 数学期望的问题
- SOJ.Dice Game
- Sicily 13907. Dice Game
- CSU1577: Dice Game
- csu 1577: Dice Game
- ACTF Dice Game Writeup
- linux下静态库
- TS流分析
- sql server 表结构修改方法
- Android Project 调用jar包形式的其他 Android Activity
- linux下查看进程的几种常用方法
- hdu - 3959 Board Game Dice(数学)
- 从waittype看sql server性能
- HDU 1301 Jungle Roads 最小生成树(简单)
- 树状数组
- 转载:STM32菜鸟成长记录---RS485通讯协议的应用
- Opencv 简单的图片显示
- shell_unset
- WPF DataGrid 获取选中行的选中列 (多选)
- mp3文件分析