1.题目描述:
A. Fake NP time limit per test 1 second memory limit per test 256 megabytes input standard input output standard outputTavak and Seyyed are good friends. Seyyed is very funny and he told Tavak to solve the following problem instead of longest-path.
You are given l and r. For all integers from l to r, inclusive, we wrote down all of their integer divisors except 1. Find the integer that we wrote down the maximum number of times.
Solve the problem to show that it's not a NP problem.
InputThe first line contains two integers l and r (2 ≤ l ≤ r ≤ 109).
OutputPrint single integer, the integer that appears maximum number of times in the divisors.
If there are multiple answers, print any of them.
Examples input 19 29 output 2 input 3 6 output 3 NoteDefinition of a divisor: https://www.mathsisfun.com/definitions/divisor-of-an-integer-.html
The first example: from 19 to 29 these numbers are divisible by 2: {20, 22, 24, 26, 28}.
The second example: from 3 to 6 these numbers are divisible by 3: {3, 6}.
2.题意概述:给你一个区间[l, r],要你求区间内所有的数的因数的集合中,出现次数最多的那个数。
3.解题思路:
首先,一个数不是奇数就是偶数,那么一个连续区间,肯定有n/2取整个偶数。而对于偶数他们的公因数肯定有2。而对于奇数就没有这么强的结论了,所以我们考虑,当区间长度为1时候,它本身一定是自己的公因数。当区间长度大于1时候考虑:如果长度为偶数,则肯定有n/2个数能够被2整除,2毫无疑问出现次数最多。如果长度为奇数,因为是连续的,但也肯定有(n + 1)/2 - 1个偶数,也就是2出现次数为(n + 1) / 2 - 1,但是对于相邻的奇数很容易发现,他们公因数出现次数肯定是小于等于2的。那么结论就出来了。
4.AC代码:
#include <bits/stdc++.h> #define INF 0x3f3f3f3f #define maxn 100100 #define lson root << 1 #define rson root << 1 | 1 #define lent (t[root].r - t[root].l + 1) #define lenl (t[lson].r - t[lson].l + 1) #define lenr (t[rson].r - t[rson].l + 1) #define N 1111 #define eps 1e-6 #define pi acos(-1.0) #define e exp(1.0) using namespace std; const int mod = 1e9 + 7; typedef long long ll; typedef unsigned long long ull; int main() { #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); long _begin_time = clock(); #endif int l, r; while (~scanf("%d%d", &l, &r)) { if (l == r) printf("%d\n", l); else puts("2"); } #ifndef ONLINE_JUDGE long _end_time = clock(); printf("time = %ld ms.", _end_time - _begin_time); #endif return 0; }