作业-周五-2026-1-2(2026年开年第一课)
wangsy2014
·
2026-01-02 18:03:21
·
个人记录
作业(共五题题解)
考点:唯一分解定理
1.什么是唯一分解定理:
一个数a ,在(a>1 )时可以分解成有且仅有一个质数序列相乘
### 2.如何求出一个数的所有质因子:
```cpp
for(int i=2;i<=sqrt(x);i++)
{
while(x%i==0)
{
x/=i;
cout<<i<<' ';
}
}
if(x!=1)
{
cout<<x;
}
cout<<'\n';
```
## 讲题部分:
### 第一题(T717048 数学专题2-1)
#### 思路:
这一题很简单,数据范围比较小,$2≤n,m≤5×10^3$,很明显是为了$O(n^2)$算法定制的数据范围。那么我们可以外层循环直接枚举$i$,内层循环暴力求解最大质因子,注意,最大质因子不能再内部再套一个质数判断,因为那样子$O(n^2×\sqrt{n})$,会超时,那么使用埃氏筛预处理$5000$以内的质数,在需要时直接查询。
#### 代码:
```cpp
#include<bits/stdc++.h>
using namespace std;
int n,m;
bool isprime[5005];
void as()
{
isprime[0]=true;
isprime[1]=true;
for(int i=1;i<=5000;i++)
{
if(isprime[i]==false)
{
for(int j=i+i;j<=5000;j+=i)
{
isprime[j]=true;
}
}
}
}
int f(int x)
{
for(int i=x;i>1;i--)
{
if(isprime[i]==false && x%i==0)
{
return i;
}
}
return x;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
as();
cin>>n>>m;
for(int i=n;i<=m;i++)
{
cout<<f(i);
if(i!=m)
{
cout<<',';
}
}
return 0;
}
```
### 第二题(T717049 数学专题2-2)
#### 思路:
这一题**直接考察分解质因数(唯一分解定理)**,那么我们先看数据范围:
对全部的测试点,保证 $1 \leq T \leq 10$,$1 \leq n \leq 10^{12}$。
那么我们分析最经典的质因数分解代码的时间复杂度:
```cpp
for(int i=2;i<=sqrt(x);i++)
{
while(x%i==0)
{
x/=i
cout<<i<<' ';
}
}
if(x!=1)
{
cout<<x;
}
```
首先,外层循环最大时间复杂度$O(\sqrt{n})$,内层循环最大时间复杂度$O(log_2n)$,那么总时间复杂度是$O(t\sqrt{n}log_2n)$,对于这个数据超时?不对,在内层循环不运行时,时间复杂度$O(\sqrt{n})$,在外层循环不运行时,时间复杂度最大$O(log_2n)$,其余的在两者之间,那么最坏情况就是$O(t\sqrt{n})$,勉强通过。
注意使用$long long$类型
#### 代码:
```cpp
#include<bits/stdc++.h>
using namespace std;
long long n;
int t;
void f(long long x)
{
for(int i=2;i<=sqrt(x);i++)
{
while(x%i==0)
{
x/=i;
cout<<i<<' ';
}
}
if(x!=1)
{
cout<<x;
}
cout<<'\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin>>t;
while(t--)
{
cin>>n;
f(n);
}
return 0;
}
```
### 第三题(T717047 数学专题2-3)
#### 思路:
我们可以发现,当我们迭代出相同的数时,就不可以继续迭代了(类似于无限循环小数),又发现这种情况只会出现在$n$为质数的情况下(虽然没有什么用)。
那么我们再看模拟会不会超时($n≤10^5$):首先,可以发现,如果质因数只有$p$,那么每次会缩小$\frac{1}{p}$,那么最差的结果就是缩小$\frac{1}{2}$,那么外层循环时间复杂度是$O(logn)$,内层循环时间复杂度是$O(\sqrt{n})$,因为循环是$1$~$n$,所以总时间复杂度是$O(logn*\sqrt{n})$。可以通过。
#### 代码:
```cpp
#include<bits/stdc++.h>
using namespace std;
int x,x_2;
int f(int x)
{
int sum=0;
for(int i=2;i<=sqrt(x);i++)
{
while(x%i==0)
{
x/=i;
sum+=i;
}
}
if(x!=1)
{
sum+=x;
}
return sum;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin>>x;
while(true)
{
x_2=f(x);
if(x==x_2)
{
break;
}
x=x_2;
}
cout<<x_2;
return 0;
}
```
### 第四题(T717052 数学专题2-4)
#### 思路:
这一题的数据范围:
记输入数据中能力值的最大值为 $\textit{inf}$。
- 对于 $20\%$ 的数据,$n \leq 5$,$\textit{inf}\leq 10^3$;
- 对于另 $30\%$ 的数据,$n \leq 100$,$\textit{inf} \leq 10$;
- 对于 $100\%$ 的数据,$n \leq 10^4$,$\textit{inf} \leq 10^6$。
那么可以发现对于$20\%$数据,我们可以直接使用搜索:时间复杂度$O(n^2×n!×logn)
//深度优先搜索主要代码部分
void dfs(int step,int k)
{
if(step>n)
{
if(cnt<k || cnt>k)
{
return ;
}
int mygcd=0;
for(int i=1;i<=k;i++)
{
mygcd=gcd(mygcd,qplans[i]);
}
maxn=maxn(maxn,mygcd);
return ;
}
qplans[++cnt]=a[step];
dfs(step+1,k);
cnt--;
dfs(step+1,k);
}
//主函数内主要求解代码部分
for(int i=1;i<=k;i++)
{
maxn=-1;
dfs(1,i);
cout<<maxn<<'\n';
}
但是,dfs还是太慢了,需要寻找新的解法。那么,我们发现:
在n 个人参与的情况下,如果答案等于ans_n ,那么对于n-1 人参与的情况下,可以发现ans_{n-1}≥ans_n (对于有n-1 人参与等情况下,最坏解就是在原来的n 个人中选n-1 个,那么ans_{n-1} 就至少有ans_n 了)对于每个可能的 x(1 到最大能力值),统计有多少学生的能力值是 x 的倍数。
如果有sum_x 个学生的能力值是x 的倍数,那么我们可以选择其中任意k(k≤sum_x) 个学生,它们的gcd 至少是x 。
那么我们先取得每个数的最大值,在从后往前取后缀最大值。
代码:
#include<bits/stdc++.h>
using namespace std;
const int N=1e4+5,M=1e6+5,for_n=1e6;
int n;
int a[N],cnt[M],maxn[M],sum[M];
void getans(){
for(int i=1;i<=for_n;i++){
for(int j=i;j<=for_n;j+=i){
sum[i]+=cnt[j];
}
maxn[sum[i]]=max(maxn[sum[i]],i);
}
for(int i=for_n;i>=1;i--)
{
maxn[i]=max(maxn[i],maxn[i+1]);
}
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
cnt[a[i]]++;
}
getans();
for(int i=1;i<=n;i++){
cout<<maxn[i]<<'\n';
}
return 0;
}
第五题(T716631 数学专题2-5)
思路:
难题依旧先看数据
前10pts: 1<=n<=100,0<=m<=n,1<=ai,bi<=1e9,1 \le T \le 10, \space \space保证A和B的值不超过1e18
前40pts: 1<=n<=10^3,0<=m<=n,1<=ai,bi<=10^9,\space \space 1 \le T \le 10, \sum N \le 10^5
前100pts: 1<=n<=10^5, 0 \le m \le n, 1 \le a_i,b_i \le 10^{12}, 1 \le T \le 10, \sum N \le 10^5
首先,前10pts 的测试点很明显,是直接使用long long 类型暴力求解:
while(t--)
{
long long A=1,B=1;//记得初始化为一、使用long long,否则十年OI一场空
for(int i=1;i<=n;i++)
{
cin>>a[i];
A*=a[i];
}
cin>>n>>m;
for(int i=1;i<=m;i++)
{
cin>>b[i];
B*=b[i];
}
if(isprime(A/B))
{
cout<<"YES\n";
}
else
{
cout<<"NO\n";
}
}
这时候,看见题目上的:“保证对于一个数字,其在b_i 中出现的次数不多于在a_i 中出现的次数。”,恭喜你,离AC又近了一步。
但是别急,我们先做40pts 的部分分。
我们发现,可以使用双层循环枚举,看看能不能抵消,记得标记,防止重复抵消。
bool flag3=false,flag2=false;
for(int i=1;i<=n;i++)
{
flag=false;
for(int j=1;j<=m;j++)
{
if(f[j]==true)
{
continue;
}
if(a[i]==b[j])
{
f[j]=true;
flag=true;
}
}
if(flag==false)
{
if(flag2==true)
{
cout<<"NO\n";
flag3=true;
break;
}
flag2=true;
if(isprime(i)==false)
{
cout<<"NO\n";
flag3=true;
}
}
}
if(!flag3)
{
cout<<"YES\n";
}
以上的代码时间复杂度O(t×n^2) ,已经很接近正解了。
那么我们继续优化。想到抵消,很多人就会想到桶计数和双指针。那么恭喜你们,找到答案了。
我们先讲桶计数:
----
再讲双指针:
我们可以使用双指针匹配数字,然后抵消,最后就得到了数。
**两个方法最后的判断:** 需要种类数和个数都是1,并且是质数。**千万不要计算1!!!**
#### 代码:
##### map桶数组解法:
```cpp
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int t,n,m;
int a[N],b[N];
bool isprime(int x){
if(x<2){
return false;
}
for(int i=2;i<=sqrt(x);i++){
if(x%i==0){
return false;
}
}
return true;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin>>t;
while(t--){
map<long long,long long>mp;
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>a[i];
if(a[i]==1){
continue;
}
mp[a[i]]++;
}
for(int i=1;i<=m;i++){
cin>>b[i];
if(b[i]==1){
continue;
}
mp[b[i]]--;
if(mp[b[i]]==0){
mp.erase(b[i]);
}
}
if(mp.size()!=1){
cout<<"NO\n";
continue;
}
for(auto i:mp){
if(i.second!=1){
cout<<"NO\n";
}
else if(isprime(i.first)==false){
cout<<"NO\n";
}
else{
cout<<"YES\n";
}
}
}
return 0;
}
```
我用的是$map$,双指针写法自己根据思路发掘