1、试除法求约数
给定n个正整数ai,对于每个整数ai,请你按照从小到大的顺序输出它的所有约数。
输入格式
第一行包含整数n。
接下来n行,每行包含一个整数ai。
输出格式
输出共n行,其中第 i 行输出第 i 个整数ai的所有约数。
数据范围
1≤n≤100,
2≤ai≤2∗109
输入样例:
2
6
8
输出样例:
1 2 3 6
1 2 4 8
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
vector<int> get_divisors(int n)
{
vector<int> res;
for(int i=1;i<=n/i;i++)
if(n%i==0)
{
res.push_back(i);
if(i!=n/i) res.push_back(n/i);
}
sort(res.begin(),res.end());
return res;
}
int main()
{
int n;
cin >> n;
while(n--)
{
int m;
cin >> m;
auto a=get_divisors(m);
for(int i=0;i<a.size();i++) printf("%d ",a[i]);
puts("");
}
return 0;
}
2、约数个数
给定n个正整数ai,请你输出这些数的乘积的约数个数,答案对109+7取模。
输入格式
第一行包含整数n。
接下来n行,每行包含一个整数ai。
输出格式
输出一个整数,表示所给正整数的乘积的约数个数,答案需对109+7取模。
数据范围
1≤n≤100,
1≤ai≤2∗109
输入样例:
3
2
6
8
输出样例:
12
#include<iostream>
#include<algorithm>
#include<unordered_map>
using namespace std;
typedef long long LL;
const int N=1e9+7;
int n;
unordered_map<int,int> primes;
int main()
{
cin >> n;
while(n--)
{
int x;
cin >> x;
for(int i=2;i<=x/i;i++)
while(x%i==0)
{
x/=i;
primes[i]++;
}
if(x>1) primes[x]++;
}
LL res = 1;
for (auto p : primes) res = res * (p.second + 1) % N;
cout << res << endl;
return 0;
}
3、约数之和
给定n个正整数ai,请你输出这些数的乘积的约数之和,答案对109+7取模。
输入格式
第一行包含整数n。
接下来n行,每行包含一个整数ai。
输出格式
输出一个整数,表示所给正整数的乘积的约数之和,答案需对109+7取模。
扫描二维码关注公众号,回复:
9469398 查看本文章
数据范围
1≤n≤100,
1≤ai≤2∗109
输入样例:
3
2
6
8
输出样例:
252
#include<iostream>
#include<algorithm>
#include<unordered_map>
using namespace std;
typedef long long LL;
const int N=1e9+7;
int n;
unordered_map<int,int> primes;
int main()
{
cin >> n;
while(n--)
{
int x;
cin >> x;
for(int i=2;i<=x/i;i++)
while(x%i==0)
{
x/=i;
primes[i]++;
}
if(x>1) primes[x]++;
}
LL res = 1;
for (auto p : primes)
{
LL t=1;
LL a=p.first,b=p.second;
while(b--)
{
t=(t*a+1)%N;
}
res=res*t%N;
}
cout << res << endl;
return 0;
}
4、欧几里得算法(辗转相除法)
int gcd(int a,int b)
{
return b? gcd(b,a%b) : a;
}