May 23 race off white cow factorial

https://ac.nowcoder.com/acm/contest/4784/B

This problem is not difficult, think about what makes a great answer?

 

If I want to enumerate, when p is a large prime number, when you die, so pre-emptive, take a look at p / i is not a prime number.

Specifically look at the code on the line, not difficult

#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
int jude(int x){
	for(long long i = 2;i*i<=x;i++){
		if(x % i == 0) return 0;
	}
	return 1;
} 
int main(){
	int t;
	long long p;
	scanf("%d",&t);
	while(t--){
		scanf("%lld",&p);
		long long n = 1;
		long long ans = 0;
		for(int i=1;;i++){
			n *= i;
			if(n % p == 0){
				ans = i;
				break;
			}
			if(p % i == 0 && p/i != i){
				if(jude(p/i)){
					ans = max(p/i,(long long)i);
					break;
				}
			}
			n %= p;
		}
		printf("%lld\n",ans);
	} 
	return 0;
} 

  

Guess you like

Origin www.cnblogs.com/lesning/p/12543420.html
Recommended