LightOj 1090 - Trailing Zeroes (II)---求末尾0的个数
题目链接:http://lightoj.com/volume_showproblem.php?problem=1090
题意:给你四个数 n, r, p, q 求C(n, r) * p^q的结果中末尾0的个数;(1<=n, r, p, q <= 10^6, r ≤ n)
要求末尾0的个数,一定和2和5有关,例如num1 * num2结果中末尾0的个数可以表示成min(num1中2的个数+num2中2的个数, num1中5的个数+num2中5的个数);
对于C(n, r)中0的2的个数可以写成f[n]-f[r]-f[n-r],其中f[i]表示i!中2的个数;打表求出 f 即可
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
typedef long long LL;
#define N 1000001
using namespace std;
const double eps = 1e-; int f[N], g[N]; ///f[i]表示i!中共有多少个2, g[i]表示i!有多少个5; void Init()
{
int x = , y = ;
for(int i=; i<N; i++)
{
int n = i;
while(n% == )
{
x ++;
n /= ;
}
n = i;
while(n% == )
{
y ++;
n /= ;
}
f[i] = x;
g[i] = y;
}
} int main()
{
Init(); int T, t = ;
scanf("%d", &T);
while(T --)
{
int p, q, n, r;
scanf("%d %d %d %d", &n, &r, &p, &q);
int ans1, ans2, ans;
ans1 = f[n]-f[r]-f[n-r] + q*(f[p]-f[p-]);
ans2 = g[n]-g[r]-g[n-r] + q*(g[p]-g[p-]);
ans = min(ans1, ans2);
printf("Case %d: %d\n", t++, ans);
}
return ;
}
LightOj 1090 - Trailing Zeroes (II)---求末尾0的个数的更多相关文章
- LightOj 1138 - Trailing Zeroes (III) 阶乘末尾0的个数 & 二分
题目链接:http://lightoj.com/volume_showproblem.php?problem=1138 题意:给你一个数n,然后找个一个最小的数x,使得x!的末尾有n个0:如果没有输出 ...
- Lightoj 1090 - Trailing Zeroes (II)
题目连接: http://www.lightoj.com/volume_showproblem.php?problem=1090 题目大意: 给出n,r,p,q四个数字1<=n,r,p,q< ...
- 172. Factorial Trailing Zeroes(阶乘中0的个数 数学题)
Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explan ...
- Java 计算N阶乘末尾0的个数-LeetCode 172 Factorial Trailing Zeroes
题目 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in ...
- POJ 1401:Factorial 求一个数阶乘的末尾0的个数
Factorial Time Limit: 1500MS Memory Limit: 65536K Total Submissions: 15137 Accepted: 9349 Descri ...
- Algorithm --> 求阶乘末尾0的个数
求阶乘末尾0的个数 (1)给定一个整数N,那么N的阶乘N!末尾有多少个0?比如:N=10,N!=3628800,N!的末尾有2个0. (2)求N!的二进制表示中最低位为1的位置. 第一题 考虑哪些数相 ...
- 求N的阶乘N!中末尾0的个数
求N的阶乘N!中末尾0的个数 有道问题是这样的:给定一个正整数N,那么N的阶乘N!末尾中有多少个0呢?例如:N=10,N=3628800,则N!的末尾有两个0:直接上干货,算法思想如下:对于任意一个正 ...
- lightoj 1138 - Trailing Zeroes (III)【二分】
题目链接:http://lightoj.com/volume_showproblem.php? problem=1138 题意:问 N. 末尾 0 的个数为 Q 个的数是什么? 解法:二分枚举N,由于 ...
- 牛客小白月赛6 水题 求n!在m进制下末尾0的个数 数论
链接:https://www.nowcoder.com/acm/contest/135/C来源:牛客网 题目描述 其中,f(1)=1;f(2)=1;Z皇后的方案数:即在Z×Z的棋盘上放置Z个皇后,使其 ...
随机推荐
- NOI2010 : 超级钢琴
求出前缀和 对于每个结尾i,设现在取的区间是[j+1,i],则i-R<=j<=i-L,取出该区间sum[j]的最小值,将sum[i]-sum[j]放入堆中 建立一个大根堆,每次取出堆顶元素 ...
- jQuery Select操作大集合
jQuery获取Select选择的Text和Value: 语法解释: $("#select_id").change(function(){//code...}); //为S ...
- Centos python 2.6 升级到2.7.3
wget http://python.org/ftp/python/2.7.3/Python-2.7.3.tar.bz2 sudo make all sudo mak install sudo mak ...
- winform学习之----图片控件应用(上一张,下一张)
示例1: int i = 0; string[] path = Directory.GetFiles(@"C:\Users\Administrator\Desktop\图片&q ...
- 浅试 JNI编程
好吧,开始我的第一个JNI试验小程序 HelloWorld.java 代码清单 public class HelloWorld { static { System.loadLibrary(" ...
- MemPool
腾讯笔试题,设计内存池,alloc和free都是O(1). 和LRUCache类似,这里用了一个list表示可用的空间,用一个map来记录这块内存是否已分配,这样free的时候才可能O(1). cla ...
- 如何进行SCCM中客户端记录信息维护
SCCM 部署完毕之后,不久我们就会发现客户端代理状态,因为重装系统,非正常的退域,长时间不开机,导致客户端状态有不可用的,有过期的,重复的记录很多.当然我们可以手动的快速删除重复的记录,那么怎么能做 ...
- php 递归创建目录、递归删除非空目录、迭代创建目录
递归创建目录 方法一 function mk_dir($path){ if(is_dir($path)){ //参数本身是一个目录 return true; } if(is_dir(dirname($ ...
- twistd 启动Python服务
twistd 启动Python服务 shell 脚本如下: #! /usr/bin/env sh MAIN_MODULE=pdf_task.py ROOT="/var/www/pdf/&qu ...
- 【翻译】Kinect v2程序设计(C++) Color篇
Kinect SDK v2预览版,获取数据的基本流程的说明.以及取得Color图像的示例程序的介绍. 上一节,是关于当前型号Kinect for Windows(后面称作Kinect v1)和次世代型 ...