Блог пользователя Onyx

Автор Onyx, история, 4 года назад, По-английски

Any optimize idea/hint about this problem?? initially have a idea about calculating trailing 0..... https://open.kattis.com/problems/inversefactorial

  • Проголосовать: нравится
  • +19
  • Проголосовать: не нравится

»
4 года назад, скрыть # |
 
Проголосовать: нравится +5 Проголосовать: не нравится

There is an editorial on DMOJ: https://dmoj.ca/problem/naq16g.

»
4 года назад, скрыть # |
 
Проголосовать: нравится +14 Проголосовать: не нравится

The title actually got me thinking of this unrelated task. Is it possible?

Can you find the value of $$$n \lt 10^9$$$, given n! mod (10^9 + 7) as the input? In case of multiple solutions, print any.

»
4 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

An easy way is the following: find the number of trailing zeros to find the maximum power of 5 that divides this number. Using binary search, you can find the range of 5 integers $$$5n + \varepsilon$$$ with $$$0 \le \varepsilon \lt 5$$$ which are candidates for the inverse factorial of this number. Using divide and conquer and FFT, you can find the factorial of $$$(5n)!$$$ in $$$O(n \log^2 n)$$$ with a not-so-bad constant factor (since you can work in base $$$10^9$$$). Then count the number of digits in it. For every $$$n \gt 1$$$, these five numbers have a different number of digits, and the difference between the number of digits can be estimated easily. For $$$n = 1$$$ you can run a brute force.

»
4 года назад, скрыть # |
 
Проголосовать: нравится +15 Проголосовать: не нравится

$$$\ln x!=x\ln x-x+\frac12\ln\sqrt{2\pi}+\frac12\ln x+o(1)$$$ (the o(1) term is between 0 and 0.1 for all $$$x \gt 1$$$) so it might be possible to binary search for the value of $$$x$$$.

»
4 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится
#include <bits/stdc++.h>
using namespace std;

const int mod = 1e9 + 7;
int main(){
	ios::sync_with_stdio(false);cin.tie(0);
	
	string s;
	cin >> s;
	
	long long int yet = 0;
	
	for(char c : s){
		yet *= 10;
		yet += c-'0';
		yet %= mod;
	}
	
	long long int here = 1;
	for(int i = 1; ; i++){
		(here *= i) %= mod;
		
		if(here == yet){
			cout << i << '\n';
			return 0;
		}
	}
	
	return 0;
}