Number theory - various methods for solving divisor numbers

Various Methods of Solving Divided Numbers

Knowing the sequence, find the divisor relationship between each number in the sequence and other numbers

pat the bull's head

Today is Bessie's birthday. To celebrate her birthday, Bessie invites you to play a game.

Bessie makes N cows (numbered 1 to N) sit in a circle. Except for cows 1 and N, cow i is adjacent to cows i−1 and i+1, and cow N is adjacent to cow 1.

Farmer John fills a bucket with slips of paper, each slip containing a number between 1 and 1,000,000. Then each cow i takes out a note from the bucket, and the number on the note is
represented by Ai.

After all the cows have been selected, each cow takes turns to walk in a circle. When walking to a cow, if the number in his hand can be divisible by the number in the cow's hand, he will pat the cow's head.

The cows want your help in determining how many cows to pat each cow.

That is, there are N integers A1, A2,..., AN. For each number Ai, find how many other numbers are its divisors.

Input Format
The first line contains the integer N.

Next N lines, each containing an integer Ai.

The output format
has a total of N lines, and the number in the i line is the number of cows that the i cow needs to beat.

Data range
1≤N≤105, 1≤Ai≤106
Input example:
5
2
1
2
3
4
Output example:
2
0
2
1
3

train of thought

This question is essentially to find out how many other numbers are divisors of each number in the sequence for a given sequence.
A positive idea is to enumerate each number, and then enumerate other numbers to judge the divisor relationship. At this time, the time complexity is O ( n 2 ) O(n^2)O ( n2 )Very pessimistic.
There is such an equivalence relationship that A1 is a divisor of A2, then A2 is a multiple of A1. For each number, we can record its multiples by enumerating its multiples.

the code

N = 1000010

a = [0] * N
cnt = [0] * N
s = [0] * N

n = int(input())

for i in range(1, n + 1) :
	a[i] = int(input())
	cnt[a[i]] += 1

for i in set(a[1 : n + 1]) :
	j = i
	while j < N :
		s[j] += cnt[i]
		j += i
for i in range(1, n + 1) :
	print(s[a[i]] - 1) # 除去自己

factorial divisor

Cherry blossoms

Given an integer n, find how many positive integer pairs (x,y)
satisfy 1/x+1/y=1/n!.

Input format
An integer n.

Output format
An integer indicating the number of pairs that satisfy the condition.
The answer is modulo 109+7.

Data range
1≤n≤106
Input sample:
2
Output sample:
3
Sample explanation
There are three pairs of numbers (x, y) that
satisfy the conditions, namely (3,6), (4,4), (6, 3).

train of thought

Factorial is a very large number. It will be time-consuming to find the number of prime factors and record the number of numbers from 1 to n one by one.
N! N!The number of prime factors p in N ! is equal to 1 ˜ N 1~~\~~N1   ˜ NEach number contains the sum of the numbers of prime factors p. In1 ˜ N 1~~\~~N1   ˜ N,pkp^kpA multiple of k , that is, contains at least 1 prime factorpkp^kpk 's obviously haveN // pk N // p^kN//pk . Since1 ˜ k − 1 1~~\~~k - 11   ˜k1 prime factor is already inN // p 1 k N // p^{1 ~k}N//pIt has been counted in 1 k  , so you only need to count the kth prime factor again, that is, addN // pk N // p^kN//pk
this question 1/x + 1/y = 1/n!
=> y = x * n!/(x - n!) => y = n! + (n!)^2/(x - n!)
ie Find an x ​​such that (n!)^2/(x - n!) is a positive integer, x > n!.
At this time, all the x numbers that satisfy the situation are the divisors of (n!)^2.
For the divisors, we can use the divisor number theorem

the code

MOD = int(1e9) + 7
N = 1000010

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)
n = int(input())

res = 1
for i in primes :
	s = 0
	j = n
	while j :
		s += j // i
		j //= i
	res = res * (1 + 2 * s) % MOD
print(res)

smallest divisor largest number

Antiprime

For any positive integer x, the number of divisors is recorded as g(x), for example g(1)=1, g(6)=4.
If a certain positive integer x satisfies: for any positive integer i smaller than x, there is g(x)>g(i), then x is called antiprime.

For example, the integers 1, 2, 4, 6, etc. are all antiprimes.

Now given a number N, request the largest anti-prime number not exceeding N.

Input format
A positive integer N.

Output format
An integer representing
the largest antiprime number not exceeding N.

Data range
1≤N≤2∗109
Input samples:
1000
Output samples:
840

train of thought

The largest antiprime number: the smallest number with the largest number of divisors
has the following properties

  1. The degree of prime factors is monotonically non-increasing
  2. Prime factors are enumerated up to 13
  3. The maximum number of prime factors is 30.
    The search sequence: search for prime factors from small to large, enumerate the desirable times, know that the prime factors are searched to the end, compare the approximate numbers, and save the answer.

the code

primes = [2, 3, 5, 7, 11, 13, 17, 19, 13]
ans = 0
maxx = 0
def dfs(u, t, s, p) :
	global ans, maxx
	# 可行性剪枝
	if p > n : return
	if s > maxx or (s == maxx and p < ans) :
		maxx = s
		ans = p
	if u == 9 : return	
	for i in range(1, t + 1) :
		dfs(u + 1, i, s * (1 + i), p * primes[u] ** i)

n = int(input())
dfs(0, 30, 1, 1)
print(ans)

Quick rounding of numbers

Hankson's quiz

Dr. Hanks is a well-known expert in the field of BT (Bio-Tech, biotechnology), and his son is named Hankson.

Now, Hankson, who just got home from school, is thinking about an interesting question.

In class today, the teacher explained how to find the greatest common divisor and least common multiple of two positive integers c1 and c2.

Now that Hankson thought he had mastered this knowledge proficiently, he began to think about an "inverse problem" of problems such as "common divisor" and "common multiple", which looks like this:

Given the positive integers a0, a1, b0, b1, suppose an unknown positive integer x satisfies: the greatest common divisor of x and a0 is a1; the least common multiple of x and b0 is b1. Hankson's "inverse problem" is to find a positive integer x that satisfies the conditions.
But after a little thought, he found that such x is not unique, and may not even exist.
So he turned to think about how to find
the number of x that satisfies the condition.
Please help him program to solve this problem.

Input Format
The first line of input is a positive integer n, indicating that there are n sets of input data.
The next n lines each have a set of input data, which are four positive integers a0, a1, b0, b1, and each two integers are separated by a space.

The input data guarantees that a0 is divisible by a1 and b1 is divisible by b0.

Output format
Output a total of n lines.

The output result of each set of input data occupies one line and is an integer.

For each set of data: if there is no such x, please output 0;
if such x exists, please output the number of x that meets the condition;

Data range
1≤n≤2000, 1≤a0,a1,b0,b1≤2∗109
Input example:
2
41 1 96 288
95 1 37 1776
Output example:
6
2

train of thought

First enumerate b 1 b1The divisor of b 1 , and then judge whether the above relationship is satisfied.
You can quickly find out the prime factors of the sieved prime numbers, and finally find all their divisors by combination, which is faster than trial division.

the code

'''
gcd(x, a) = b
lcm(x, c) = d
求满足此方程组的解的个数
枚举d的约数,判别
通过分解质因数来枚举约数
'''
from math import gcd

N = 50010

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

def dfs(u, p) :
    if u == len(factors) :
        dividers.append(p)
        return
    for i in range(factors[u][1] + 1) :
        dfs(u + 1, p * factors[u][0] ** i)
        
def lcm(a, b) :
    return a * b // gcd(a, b)

init(N - 1)

T = int(input())
factors = []
dividers = []

for _ in range(T) :
    a, b, c, d = map(int, input().split())
    # 分解质因数
    factors = []
    dividers = []
    tmp = d
    i = 0
    while primes[i] <= tmp // primes[i] :
        s = 0
        while tmp % primes[i] == 0 :
            s += 1
            tmp //= primes[i]
        if s :
            factors.append([primes[i], s])
        i += 1
    if tmp > 1 :
        factors.append([tmp, 1])
    dfs(0, 1)
    res = 0
    for i in dividers :
        if gcd(i, a) == b and lcm(i, c) == d :
            res += 1
    print(res)

Guess you like

Origin blog.csdn.net/qq_57150526/article/details/129474917