Description
求出区间[a,b]中所有整数的质因数分解。
Input
输入两个整数a,b。
Output
每行输出一个数的分解,形如k=a1a2a3…(a1<=a2<=a3…,k也是从小到大的)(具体可看样例)
Sample Input
3 10
Sample Output
3=3
4=22
5=5
6=23
7=7
8=222
9=33
10=25
Hint
提示
先筛出所有素数,然后再分解。
数据规模和约定
2<=a<=b<=10000
代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
using namespace std;
const int N=1e6+10;
const double pai=4*atan(1);
int a[N];
int f(int x)
{
int i,j=sqrt(x);
if(x%2==0)
return 2;
for(int i=3;i<=j;i++)
if(x%i==0)
return i;
return x;
}
int main()
{
ios::sync_with_stdio(false);
int a,b;
while(cin>>a>>b)
{
for(int i=a;i<=b;i++)
{
int m=f(i);
int k,t;
k=i/m;
cout<<i<<"="<<m;
while(k>1)
{
t=f(k);
k/=t;
cout<<"*"<<t;
}
cout<<endl;
}
}
return 0;
}
本文介绍了一种用于求解区间[a,b]内所有整数的质因数分解的方法。通过筛选出所有素数并进行分解,实现了对于指定范围内每个整数的有效质因数分解。
2294

被折叠的 条评论
为什么被折叠?



