约数的个数与和(基本算数定理求解) |
您所在的位置:网站首页 › 因数个数公式怎么推导出来的 › 约数的个数与和(基本算数定理求解) |
目录
基本算数定理原理由算数基本定理而来的重要推论:
[acwing 870. 约数个数](https://www.acwing.com/problem/content/872/)[acwing871. 约数之和](https://www.acwing.com/problem/content/873/)
基本算数定理
在求解之前,我们先来了解一下基本算数定理。 原理算术基本定理可表述为:任何一个大于1的自然数 N,如果N不为质数,那么N可以唯一分解成有限个质数的乘积 N = P 1 a 1 ∗ P 2 a 2 ∗ P 3 a 3 ∗ . . . . . . ∗ P n a n N=P_1^{a1}*P_2^{a2}*P_3{a3}*......*P_n^{an} N=P1a1∗P2a2∗P3a3∗......∗Pnan,这里 P 1 < P 2 < P 3...... < P n P1 int x; scanf("%d",&x); for(int i=2;i int a=i.first,b=i.second; res=(res*(b+1))%mod; } printf("%lld\n",res); return 0; } 这里为了方便用了 unordered_map 来存所有的质因子,也可以用hash拉链法方法来存。 acwing871. 约数之和给定n个正整数aiai,请你输出这些数的乘积的约数之和,答案对109+7109+7取模。 输入格式 第一行包含整数n。 接下来n行,每行包含一个整数aiai。 输出格式 输出一个整数,表示所给正整数的乘积的约数之和,答案需对109+7109+7取模。 数据范围 1≤n≤100, 1≤ai≤2e9输入样例: 3 2 6 8输出样例: 252分析:这道题跟上道题相同,都是对算数基本定理推论的应用前面的质因子分解相同,只有后面推论公式计算不同。 代码如下 #include #include #include #include using namespace std; typedef long long ll; const int mod=1e9+7; int main() { int n; scanf("%d",&n); unordered_map p; while(n--) { int x; scanf("%d",&x); for(int i=2;i int a=i.first,b=i.second; ll t=1; while(b--) { t=(t*a+1)%mod; } res=(res*t)%mod; } printf("%lld\n",res); return 0; } |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |