【ZhengQing】[1752]Math Three!

Problem Description

Loading...

Problem Analysis

At the time I thought
“Still brute force~”
I also thought it was correct just now…
But when I checked, it was TL (time limit exceeded)…

This is how I wrote it at the time:

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#include<stdio.h>
int main() {
	long long int N;
	int i;
	scanf("%lld",&N);
	for(i=1; i<N; i++) {
		if((N%(i+1))==0)
			break;
	}
	printf("%d\n",i);
	return 0;
}

Then I rewrote it:
In fact, it is still brute force at heart…
But this time only up to sqrt(N)
However, this causes incorrect output when N is prime
So another check is needed
The AC code is as follows:

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#include<stdio.h>
#include<math.h>
int main() {
	long long N,K;
	double L;
	scanf("%lld",&N);
	L=sqrt(N);
	for(K=1; K<=L; K++) {
		if((N%(K+1))==0)
			break;
	}
	if(K<=L)
		printf("%lld\n",K);
	else
		printf("%lld\n",N-1);
	return 0;
}

This was their November problem!!!

Problem address: ZZULI 1752 Math Three!

This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...