2021 Niu Ke Winter Holiday Algorithm Basic Training Camp 1 A-string (mathematics formula) J A group of little frogs quack quack quack quack (LCM)

Topic link: A-string   J A group of little frogs croak croak croak croak

A-string

Topic

  • How many strings consist of lowercase letters that are not more than n and contain the subsequence "us"? The answer is modulo 1e9+7.
  • Subsequence, us may not be adjacent
  • Range: 2≤n≤1e6

Ideas

  • Mathematical formulas. If you want to see the dp push status,  click here~
  • Difficulty is the opposite . Find the one that does not contain the us subsequence, such as n equals 3.
  1. Choose no s for all three, then a total of 25*25*25
  2. The last one is s, then u cannot be selected before, a total of 25*25*1
  3. The second one chooses s, then the penultimate one cannot choose s, because it will be repeated with 2, and the first one cannot be u, a total of 25*1*25
  4. The first one chooses s, then neither the second nor the last one can choose s, because it will be repeated with the previous one, a total of 1*25*25
  • Randomly select 26^3, remove 25^2 * 3 + 25^3 that does not contain the us subsequence, then the rest contains the us subsequence
  • So for a string of length n, the number of us subsequences is 26^n-25^(n-1) * n + 25 ^ n, and subtraction needs to be added mod and then modulo.

ac code

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int mod = 1e9 + 7;
const int maxn = 1e6 +  5;
ll _pow(ll a, ll b){
    ll ans = 1;
    while(b){
        if(b & 1) ans = ans * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return ans;
}
int main(){
    int n; cin >> n;
    ll ans = 0;
    for(int i = 2; i <= n; i ++){
        ans = (ans + _pow(26, i) - (i * _pow(25, i - 1) % mod + _pow(25, i)) % mod + mod) % mod;
    }
    cout << ans << endl;
    return 0;
}

J A group of little frogs croak croak croak croak

Topic

  • There are n grids, and each grid has a number, 1, 2, 3, 4...n
  • Then Niu Niu released endless frogs. The path of the i-th frog is a geometric sequence with the first term being 1 and the common ratio p(i), where p(i) represents the i-th prime number.
  • When the frog jumps to a grid, if there is a number on the grid, the frog will eat the number
  • Ask what is the least common multiple of all the remaining numbers that have not been eaten, and take the modulus of 1e9+7
  • Range 1≤n≤1.6e8, time limit 2s

Ideas

  • For a number n, first 1 will definitely be eaten, and then if there is only one prime factor after decomposing the prime factors, then it will definitely be eaten, so the condition for survival is that it can decompose more than one prime factor
  • Then there is the theorem of lcm, such as lcm of three numbers x, y, z, after decomposing the prime factors respectively x = p1^a1 * p2^b1 * p3^c1, y = p1^a2 * p2^b2 * p3^ c3, z = p1^a3 * p2^b3 * p3^c3, then lcm=p1^(max(a1, a2, a3)) * p2^(max(b1, b2, b3)) * p3^(max( c1, c2, c3)) , so while looking for prime factors, we also need to find the most decomposed quantity
  • So we must first sieve prime numbers, and Euler sieve is used here .
  • Then how can we decompose the larger quantity? If the prime factor is 2, then in order to have an extra prime factor, then find the smaller 3, and then multiply the largest 2 and 3, and the multiplication result does not exceed n; if If it is another prime factor ci, then you can find a 2, and multiply the most calculated ci with 2, and the multiplication result does not exceed n. Finally, multiplying the reasonable prime factor is its lcm.

ac code

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 8e7 + 5; //给的上限的一半
const int mod = 1e9 + 7;
int v[N], prime[N], cnt = 0;
void get_prime(){ //欧拉筛
	for(int i=2;i<=N;i++){
		if(!v[i]){
			v[i]=i;prime[++cnt]=i;
		}
		for(int j=1;j<=cnt&&i*prime[j]<=N;j++){
			if(prime[j]>v[i])break;
			v[i*prime[j]]=prime[j];
		}
	}
} 
int main(){
    get_prime();
    int n; scanf("%d", &n);
    if(n < 6){ //小于6的都会被吃
        puts("empty");
        return 0;
    }
    ll cc = 0, dd = 2; //质因子是2的另算,cc是能取2的最多数量
    while(dd * 3 <= n) dd *= 2, cc ++;  
    ll ans = 1;
    while(cc --) ans = (ans * 2) % mod; //cc个2相乘,就是pi^ai,最后lcm就是这些算式的乘积
    for(int i = 2; i <= cnt && prime[i] * 2 <= n; i ++){
        cc = 0, dd = prime[i];
        while(dd * 2 <= n) dd *= prime[i], cc ++; //同理,其他质因子要与2配对,找最大的数量
        while(cc --) ans = ans * prime[i] % mod;
    }
    printf("%lld\n", ans);
	return 0;
}

 

Guess you like

Origin blog.csdn.net/weixin_43911947/article/details/113530909