口碑好的免费网站建设,wordpress插件安装本地安装,wordpress主题 html5,备案网站名怎么写1. 试除法求约数
给定 n 个正整数 ai#xff0c;对于每个整数 ai#xff0c;请你按照从小到大的顺序输出它的所有约数。
输入格式
第一行包含整数 n。
接下来 n 行#xff0c;每行包含一个整数 ai。
输出格式
输出共 n 行#xff0c;其中第 i 行输出第 i 个整数 ai 的…1. 试除法求约数
给定 n 个正整数 ai对于每个整数 ai请你按照从小到大的顺序输出它的所有约数。
输入格式
第一行包含整数 n。
接下来 n 行每行包含一个整数 ai。
输出格式
输出共 n 行其中第 i 行输出第 i 个整数 ai 的所有约数。
数据范围
1≤n≤100 1≤ai≤2×10^9
输入样例
2
6
8输出样例
1 2 3 6
1 2 4 8 #include iostream
#include algorithm
#include vector
using namespace std;
const int N 1000010;
vectorint get_divisiors(int n)
{ // 求n的所有约数vectorint res;for (int i 1; i n / i; i){ // 这里n/i其实就是sqrt(n)不用i*in因为怕溢出if (n % i 0){res.push_back(i);if (i ! n / i) // i和n/i都为约数所以当i ! n / i时加入n/i// in/i时因为之前已经加入i了所以不用再加n/i了res.push_back(n / i);}}sort(res.begin(), res.end());return res;
}
int main()
{int n;cin n;get_divisiors(n);while (n--){int x;cin x;auto res get_divisiors(x);for (auto t : res){cout t ;}cout endl;}return 0;
}2.约数个数
给定 n 个正整数 ai请你输出这些数的乘积的约数个数答案对 10^97 取模。
输入格式
第一行包含整数 n。
接下来 n 行每行包含一个整数 ai。
输出格式
输出一个整数表示所给正整数的乘积的约数个数答案需对 10971097 取模。
数据范围
1≤n≤100 1≤ai≤2×10^9
输入样例
3
2
6
8输出样例
12
思路 一个数的约数是由这个数的几个质因子相乘得到的。
例如12 的质因子有 23。12的约数有1234612。
约数1 是由 0 个 2 0 个3相乘得到的。 约数2 是由 1 个 2 0 个3相乘得到的。 约数3 是由 0 个 2 1 个3相乘得到的。 约数4 是由 2 个 2 0 个3相乘得到的。 约数6 是由 1 个 2 1 个3相乘得到的。 约数12 是由 2 个 2 1 个3相乘得到的。 12 可以分解为2^2*3^1。所以2可以取 0 ~ 2个3种取法。3可以取 0~1 个2种取法。12的约数一共2 * 3 6个。
也就是把一个数N 写成N (p1^x1^)(p^x2)(p3^x3)…(pk^xk)其中pi为质数。则N的约数个数为(x11)(x21)(x31)…(xk1)
作者Hasity 链接https://www.acwing.com/solution/content/148964/ 来源AcWing
#include iostream
#include algorithm
#include unordered_map
using namespace std;
const int p 1e9 7;
int main()
{int n;cin n;unordered_mapint, int primes; // 存底数和指数while (n--){int x;cin x;for (int i 2; i x / i; i){while (x % i 0){x / i;primes[i]; // 质因数的指数1}}if (x 1) // 如果有剩余也是一个质因子primes[x];}long long int res 1;for (auto prime : primes){res res * (prime.second 1) % p;}cout res endl;return 0;
}3. 约数之和
给定 n 个正整数 ai请你输出这些数的乘积的约数之和答案对 10^97 取模。
输入格式
第一行包含整数 n。
接下来 n 行每行包含一个整数 ai。
输出格式
输出一个整数表示所给正整数的乘积的约数之和答案需对 10971097 取模。
数据范围
1≤n≤100 1≤ai≤2×10^9
输入样例
3
2
6
8输出样例
252
#include iostream
#include algorithm
#include unordered_map
using namespace std;
const int p 1e9 7;
int main()
{int n;cin n;unordered_mapint, int primes; // 存底数和指数while (n--){int x;cin x;for (int i 2; i x / i; i){while (x % i 0){x / i;primes[i]; // 质因数的指数1}}if (x 1) // 如果有剩余也是一个质因子primes[x];}long long int res 1;for (auto prime : primes){int a prime.first, b prime.second; // 记录底数和指数// 秦九韶算法求p^bp^(b-1)...p1;long long int t 1;while (b--) // 指数从b开始遍历到0{t (t * a 1) % p; // 秦九韶算法公式}res res * t % p;}cout res endl;return 0;
}