1. 程式人生 > >154 Factorial(階乘素因子次數的逆問題)

154 Factorial(階乘素因子次數的逆問題)

題目:

You task is to find minimal natural number N, so that N! contains exactly Q zeroes on the trail in decimal notation. As you know N! = 1*2*...*N. For example, 5! = 120, 120 contains one zero on the trail.
Input One number Q written in the input (0<=Q<=10^8).
Output Write "No solution", if there is no such number N, and N otherwise.
Sample test(s)

Input 2 Output 10

這個題目還是要用下面這個函式

long long degree_in_fact(long long m, int p)
{
	if (m)return degree_in_fact(m / p, p) + m / p;
	return 0;
}

這個題目其實就是求,滿足degree_in_fact(m,5)=q的最小m
但是因為q非常大,而m一定比q還大,所以m不能從1開始列舉,要先給m估值。

注意到,因為要求最小的m,所以m肯定是5的倍數。

假設m=k*5
根據上面這篇部落格可以得到:

q=k+k/5+k/(5*5)+k/(5*5*5)+......<k*1.0/(1-1.0/5)=k*5/4.0

所以q*4<k*5,即k>=q*4/5+1

這個k就是我的程式碼中的key

程式碼:

#include<iostream>
using namespace std;

long long degree_in_fact(long long m, int p)
{
	if (m)return degree_in_fact(m / p, p) + m / p;
	return 0;
}

int main()
{
	long long q;
	cin >> q;
	if (q == 0)
	{
		cout << "1";
		return 0;
	}
	long
long key = q * 4 / 5 + 1; while (degree_in_fact(key, 5) + key<q)key++; if (degree_in_fact(key, 5) + key == q)cout << key * 5; else cout << "No solution"; return 0; }

程式碼裡面有q為0的特判,這是這個題目的汙點。

如果沒有這個特判的話,輸入0會輸出0,明明這個剛好符合要求,

而且所有輸出的數字都是5的倍數,很好。偏偏這個題目非要這麼破壞一下美感。。。

相關推薦

154 Factorial因子次數問題

題目: You task is to find minimal natural number N, so that N! contains exactly Q zeroes on the trail in decimal notation. As you know N!

Factorial最後0的個數

Factorial Time Limit: 1500MS Memory Limit: 65536K Total Submissions: 15475 Accepted: 9533 Description The most important part of

light oj 1045 - Digits of Factorial(求在不同進制下的位數)

its sum math.h factorial padding top turn ase cin Factorial of an integer is defined by the following function f(0) = 1 f(n) = f(n - 1) *

《資料庫技巧》資料庫兩個表求笛卡爾積

最近遇到了一個需求:使用者在客戶端頁面上進行資料錄入,下拉列表的選擇,然後使用者對頁面資料進行提交。後臺要根據客戶端傳來的資料進行分析,並且生成一串數字,將該數字串進行儲存。 介紹之前,我們要了解本文的一個名詞【笛卡爾積】,同俗的來講,就是數學中的排列組合。

資料結構--遞迴的幾個應用求和,,漢諾塔

定義     一個函式自己呼叫自己遞迴的條件             必須要有明確的終止條件         所處理的資

Java中的組織形式、類與物件、靜態的static關鍵字、最終的final關鍵字、方法傳參方式、遞迴、斐波那契數列、漢諾塔

Java程式的組織形式 Java程式需要把程式碼以類的形式組織起來,然後被Java編譯器編譯,再被JVM執行。Java程式是以類的結構為基礎的。 Java程式的基本要素 識別符號 識別符號命名規範 關鍵字(保留字) 關鍵字(保留字)具有專門的意義和用途

Math數論-分解因子

JATC's math teacher always gives the class some interesting math problems so that they don't get bored. Today the problem is as follows. Given an inte

HDU Problem - 5976 Detachment元,打表,數學

題目連結 Problem Description In a highly developed alien society, the habitats are almost infinite dimensional space.In the history of this planet

for迴圈語句運算

#include<stdio.h> main() { int i,j,n; long term, sum=0; printf("Input n:"); while(scanf("%d",&n)!=EOF){

五十道程式設計小題目 --- 11 排列與組合 java

package wn.comeOn.java.test.arithmetic50; import java.util.ArrayList; public class Arrangement { // 階乘 public static int factorial(int n) { if (n

GCC和求餘,很重要

GCC Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 4162    Accepted Submissi

SGU 154 Factorial數論+二分

Description 求最小的整數n使得n!的值後面有q個0 Input 一個整數q(0<=q<=10^8) Output 滿足條件的最小的正整數n,如果不存在則輸出No sol

[Python函式] 遞迴函式

在函式內部,可以呼叫其他函式。如果一個函式在內部呼叫自身本身,這個函式就是遞迴函式。 例如,計算階乘 n! = 1 x 2 x 3 x … x n,用函式fact(n)表示。 fact(n) = n

的質因數分解算組合數】【TOJ4111】【Binomial efficient】

n<=10^6 m<=10^6 p=2^32 用unsigned int 可以避免取模 我寫的SB超時 階乘分解程式碼 #include <cstdio> #include <cstdlib> #include <cmath&

Leetcode-1006 Clumsy Factorial(笨)

階乘 define clas cto class tor def turn leetcode 1 #define pb push_back 2 #define _for(i,a,b) for(int i = (a);i < (b);i ++) 3 con

HDU 5407 CRB and Candies(LCM +最大因子元)

blog std 歸納 get pos http and -a 思路 【題目鏈接】click here~~ 【題目大意】求LCM(Cn0,Cn1,Cn2....Cnn)%MOD 的值 【思路】來圖更直觀: 這個究竟是怎樣推出的,說實話。本人數學歸納大法沒有推出來

nyist67 三角形面積求三角形的面積

題目地址:http://acm.nyist.net/JudgeOnline/problem.php?pid=67 思路:任意2邊向量的叉積的絕對值的1/2即為三角形的面積 AC程式碼: #inclu

FMFactorization Machine,因子分解機演算法個人理解

1. FM是什麼 因子分解機(Factorization Machine, FM)是由Steffen Rendle提出的一種基於矩陣分解的機器學習演算法。 1.1 背景 常見的線性模型,比如線性迴歸、邏輯迴歸等,它只考慮了每個特徵對結果的單獨影響,而沒有考慮特徵間的組合

[Codeforces438E]The Child and Binary Tree多項式開根+多項式求

=== === 這裡放傳送門 === === 題解 這道題的意思是給定一個集合,求有多少形態不同的二叉樹滿足每個點的權值都屬於這個集合並且總點權等於i。 設F(x)表示總點權等於x的的二叉樹數目,那麼由當前已經有的二叉樹構造更大的二叉樹就是先把

Luogu5162 WD與積木生成函數+多項式求

積木 math 生成 getch style fin stdin while stream   顯然的做法是求出斯特林數,但沒有什麽優化空間。   考慮一種暴力dp,即設f[i]為i塊積木的所有方案層數之和,g[i]為i塊積木的方案數。轉移時枚舉第一層是哪些積木,於是有f[