Article Directory
[NOIP2000 Improvement Group] Base conversion
topic description
We can use this way to represent a decimal number: multiply each Arabic numeral by an exponent of the position of the number, with 10 10Form of the sum of powers of base 10 . eg123 123123 can be expressed as1 × 1 0 2 + 2 × 1 0 1 + 3 × 1 0 0 1 \times 10^2+2\times 10^1+3\times 10^01×102+2×101+3×100 such a form.
Similarly, for binary numbers, it can also be expressed as each binary number multiplied by an exponent based on the position of the number, with 2 2Form of the sum of powers of base 2 .
Generally speaking, any positive integer RRR or a negative integer− R -R− R can be chosen as the base of a number system. If byRRR or− R -R− R is the base number, the numbers to be used are0 , 1 , . . . . R − 1 0,1,....R-10,1,....R−1。
For example when R = 7 R=7R=At 7 , the required numbers are0, 1, 2, 3, 4, 5, 6 0,1,2,3,4,5,60,1,2,3,4,5,6 , which is ratherthe RRR or− R -R− R is irrelevant. If the absolute value of the base number exceeds10 1010 , in order to represent these numbers, English letters are usually used to represent those greater than9 99 digits. For example for16 16For hexadecimal numbers, useAAA means10 1010 , useBBB means11 1111 , withCCC means12 1212 , and so on.
In negative base numbers, $-R $ is used as the base, such as − 15 -15− 15 (decimal) is equivalent to( 110001 ) − 2 (110001)_{-2}(110001)−2 ( − 2 -2 − binary ), and it can be expressed as2 2Sums of power-of -2 series:
( 110001 ) − 2 = 1 × ( − 2 ) 5 + 1 × ( − 2 ) 4 + 0 × ( − 2 ) 3 + 0 × ( − 2 ) 2 + 0 × ( − 2 ) 1 + 1 × ( − 2 ) 0 (110001)_{-2}=1\times (-2)^5+1\times (-2)^4+0\times (-2)^3+0\times (-2)^2+0\times (-2)^1 +1\times (-2)^0 (110001)−2=1×(−2)5+1×(−2)4+0×(−2)3+0×(−2)2+0×(−2)1+1×(−2)0
Design a program that reads in a base of a decimal number and a negative base, and converts the decimal number into a number in this negative base.
input format
Each line of input has two input data.
The first is the decimal number nnn .
The second is the base of negative base numbers− R -R−R。
output format
Output this negative base number and its base, if the base exceeds 10 1010 , then refer to16 16Hexadecimal way processing.
Example #1
Sample Input #1
30000 -2
Sample output #1
30000=11011010101110000(base-2)
Example #2
Sample Input #2
-20000 -2
Sample output #2
-20000=1111011000100000(base-2)
Example #3
Sample Input #3
28800 -16
Sample output #3
28800=19180(base-16)
Example #4
Sample Input #4
-25000 -16
Sample output #4
-25000=7FB8(base-16)
hint
【Data range】
for 100 % 100\%100% of the data,− 20 ≤ R ≤ − 2 -20 \le R \le -2−20≤R≤− 2,∣ n ∣ ≤ 37336 |n| \le 37336∣n∣≤37336。
The first question of NOIp2000 improvement group
train of thought
First of all, we need to know how the decimal system is converted into other base systems. Suppose the decimal number 15 is converted to binary. 15 = ( ( 1 × 2 + 1 ) × 2 + 1 ) × 2 + 1 15 = ((1\times2+1)\times2+1)\times2+115=((1×2+1)×2+1)×2+1 , only need to continue to take the modulo, divisible. The loop goes on until the divisibility is 0, the modulus is arranged1111
. In essence, the number of digits obtained for each digit represents the number of decimal weights of this number, that is, multiplied by (number of digits - 1) 2.
For negative decimal numbers, it is assumed to be− 2 -2− Binary can be expressed as15 = ( ( ( 1 × − 2 ) × − 2 ) × − 2 ) × − 2 − 1 15 = (((1 \times-2) \times -2) \times- 2) \times -2-115=(((1×−2)×−2)×−2)×−2−1 , according to the original method, the modulus has a negative number. It needs to be transformed according tothe dividend = quotient × divisor + remainder ≡ ( quotient + 1 ) × divisor + remainder − divisor dividend = quotient \times divisor + remainder \equiv (quotient + 1) \times divisor + remainder - divisordividend=business×divisor+remainder≡( Business+1)×divisor+remainder−Divisor ,15 = ( ( ( 1 × − 2 ) × − 2 ) × − 2 + 1 ) × − 2 + 1 15 = (((1 \times-2) \times -2) \times-2 + 1) \times -2+115=(((1×−2)×−2)×−2+1)×−2+1 , the obtained -binary number is10011
. Then in the transformation, if a negative remainder is encountered, the resultingremainder-divisor remainder-divisorremainder−Divisor ,quotient + 1 quotient + 1business+1 is enough.
the code
def convert(n, base) :
res = ''
if n == 0 :
return 0
while n :
t = n % base
n //= base
if t < 0 :
n += 1
t -= base
if t >= 10 :
t = chr(ord('A') + t - 10)
res = str(t) + res
return res
n, base = map(int, input().split())
res = convert(n, base)
print(f"{
n}={
res}(base{
base})")
Number of straight line intersections
topic description
Suppose there are NNs on the planeIf there are N straight lines and no three lines have the same point, how many different intersections can these straight lines have in total?
input format
one line, an integer NNN , stands forNNN straight lines.
output format
One line, an integer, indicates the total number of schemes.
Example #1
Sample Input #1
4
Sample output #1
5
hint
For all data, satisfy 1 ≤ N ≤ 25 1 \le N \le 251≤N≤25。
train of thought
For a pile (N) of straight lines, it can be divided into two piles, one pile § is the straight lines parallel to each other, and one pile (N -r) is the straight lines not parallel to the other pile. The point of intersection between these two lines is p × ( N − r ) p \times (N - r)p×(N−r ) . There is no point of intersection between parallel lines, and the point of intersection can also be calculated according to this method for straight lines that are not parallel to another group, until the number of parallel lines is 0 and there is no point of intersection.
Recursive thinking, assuming that there are currently n straight lines, enumerate the number of parallel straight lines, record the intersection points between two sets of straight lines, and continue to recursively process the non-parallel straight lines until the remaining straight lines are 0.
the code
N = 25 * 25 + 10
st = [False] * N
ans = 0
def dfs(n, s) : # 分别表示当前处理的直线堆、已算出的交点数
global ans
if n == 0 : # 终止条件
if not st[s] :
ans += 1
st[s] = True
return
# 枚举平行线数
for i in range(1, n + 1) :
dfs(n - i, s + i * (n - i))
n = int(input())
dfs(n, 0)
print(ans)
coding
topic description
Encoding is often applied to ciphertext or compressed transmission. Here we use one of the simplest encoding methods for encoding: encode some regular words into numbers.
There are 26 letters {a, b, ..., z} in the alphabet, these special words are not more than 6 in length and the letters are arranged in ascending order. Putting all such words together, in lexicographical order, a word's encoding corresponds to its position in the dictionary.
For example:
a→1 b→2 z→26 ab→27 ac→28
Your task is to find the code for the given word.
input format
Just one line, encoded words.
output format
Only one line, corresponding to the encoding. If the word is not in the alphabet, output 0.
Example #1
Sample Input #1
ab
Sample output #1
27
train of thought
Take cgx
as an example
- First of all, a word with a length of 1 must be smaller than it C 26 1 C_{26}^1C261
- A word of length 2 must be smaller than it C 26 2 C_{26}^2C262
- The case where the first letter is less
c
than must be less than it C 24 2 C_{24}^2C242 - The case where the first letter is equal
c
, andgx
the word less than it must be smaller than it (recursive processing is enough).
the code
It's Graduation Season II
topic background
"Dingling, ringing", as the bell of the last subject of the college entrance examination rang, three years of youth suddenly froze at this moment. The joy of graduation is no match for the reluctance of parting, looking forward to the future and still not forgetting the past songs. The laughter and tears of more than a thousand days and nights were all gathered at the graduation party. I believe that this must be the most unforgettable moment in my life!
topic description
There was a rehearsal once, and the teacher was not very satisfied. Of course, it is obviously unreasonable to take the number of each student to find the greatest common divisor. So the teacher rated each student with an ability value. So now the problem becomes, from nnPick kk out of n studentsK individuals maximize their tacit understanding (that is, the greatest common divisor of ability values). But because there are too many programs, and the number of people needed for each program is unknown. The teacher wants to know what is the maximum level of rapport that can be achieved in all situations. It's even more troublesome now, so I'll leave it to you~
PS: The greatest common divisor of a number is itself.
input format
The first line is a positive integer nnn。
The second line is nnA positive integer separated by n spaces, representing the ability value of each student.
output format
Total nnline n , iii actk = ik = ik=The maximum degree of tacit understanding in case i .
Example #1
Sample Input #1
4
1 2 3 4
Sample output #1
4
2
1
1
hint
[source of topic]
lzn original
【data range】
Note that the maximum value of the ability value in the input data is inf \textit{inf}inf。
- For 20 % 20\%20% of the data,n ≤ 5 n \leq 5n≤5, inf ≤ 1 0 3 \textit{inf}\leq 10^3 inf≤103;
- For the other 30 % 30\%30% of the data,n ≤ 100 n \leq 100n≤100, inf ≤ 10 \textit{inf} \leq 10 inf≤10;
- For 100 % 100\%100% of the data,n ≤ 1 0 4 n \leq 10^4n≤104, inf ≤ 1 0 6 \textit{inf} \leq 10^6 inf≤106。
train of thought
Solve 1 − n 1-n of 1-n numbers1−The greatest common factor of n numbers. A violent idea is to use the number of combinations from 1 to n, and then find the data that the greatest common factor can pass 30%.
If you want to ac, you have to start from the factor itself. Suppose you want to find the greatest common factor of k numbers, then for this factor, it must be shared by k numbers or more. Then we can compare the factors by counting the number of factors of all numbers. The greatest factor of a number greater than or equal to k is the greatest common factor of k numbers.
the code
N = 1000010
C = [0] * N
A = [0] * N
t = 0
def get_factor(num) : # 求约数(O(sqrt(n)))
i = 1
while i <= num // i :
if num % i == 0 :
C[i] += 1
if i != num // i :
C[num // i] += 1
i += 1
n = int(input())
A[1 : n + 1] = list(map(int, input().split()))
# 记录所有数因子情况
for i in range(1, n + 1) :
t = max(t, A[i])
get_facotor(A[i])
for i in range(1, n + 1) : # 因子数也具有单调性
if C[t] < i : t-= 1
print(t)
check-in question
topic background
This is a check-in question!
It is recommended to read the data range carefully before doing the question!
topic description
We define a function: qiandao ( x ) \operatorname{qiandao}(x)qiandao ( x ) is less than or equal toxxAmong the numbers of x , and xxx is the number of numbers thatare not coprime
This question is used as a check-in question, given lll andrrr , find:
∑ i = lr qiandao ( i ) mod 666623333 \sum_{i=l}^r \operatorname{qiandao}(i)\bmod666623333i=l∑rqiandao ( i )mod666623333
input format
A line of two integers, lll、 r r r。
output format
One integer per line represents the answer.
Example #1
Sample Input #1
233 2333
Sample output #1
1056499
Example #2
Sample Input #2
2333333333 2333666666
Sample output #2
153096296
hint
- For 30% 30\%30% of the data,l , r ≤ 1 0 3 l,r\leq 10^3l,r≤103。
- For 60 % 60\%60% of the data,l , r ≤ 1 0 7 l,r\leq 10^7l,r≤107。
- For 100 % 100\%100% of the data,1 ≤ l ≤ r ≤ 1 0 12 1 \leq l \leq r \leq 10^{12}1≤l≤r≤1012, r − l ≤ 1 0 6 r-l \leq 10^6 r−l≤106。
(30 points)
'''
筛法筛欧拉函数,用筛好的欧拉函数,继续筛区间欧拉函数
'''
from math import ceil
N = 1000010
MOD = 666623333
eula = [0] * N
st = [False] * N
primes = []
def init(n) :
for i in range(2, n + 1) :
if not st[i] :
primes.append(i)
for j in primes :
if i * j > n : break
st[i * j] = True
if i % j == 0 :
break
init(N - 1)
l, r = map(int, input().split())
def get_eula(x) :
res = x
for i in primes :
if i > x : break
flag = True
while x % i == 0 :
x //= i
flag = False
if not flag :
res = res * (i - 1) // i
return res
# 区间筛质数
st = [False] * N
for i in primes[:-1] :
if i > r : break
j = max(2, ceil(l / i)) * i
while j <= r :
st[j - l] = True
j += i
for i in range(l, r + 1) :
if i >= 2 and not st[i - l] :
eula[i - l] = i - 1
elif i < 2 : eula[i] = 1
else :
eula[i - l] = get_eula(i)
res = 0
for i in range(l, r + 1) :
res = (res + i - eula[i - l]) % MOD
print(res)