标签:end algorithm math font 需要 输入数据 int problem scan
描述
给你一个数n,请你把n的所有约数都找出来(显然1和n本身肯定都是n的约数),假设这些约数我们用a0,a1,a2……am表示,其中a0=1,am=n。可以用这些约数中一些或全部来组成序列ai0ai1…aik,但组成的序列需要满足序列中的每一项都能被后一项整除这一条件,现在请你找出满足条件的序列的最大长度k,以及满足最大长度序列的个数。
输入
多组数据,每行输入一个整数n(n<=300);
输出
对于每组输入数据,输出两个数用空格隔开,第一个数表示满足条件序列最大长度。第二个数表示,满足最大长度序列的个数。
输入样例 1
2 3 4 10 100
输出样例 1
1 1 1 1 2 1 2 2 4 6
思路:
先筛出所有素数,存在超级好用的map里,边读入边统计,最后输出答案
完结撒花??ヽ(°▽°)ノ?
代码:
#include<map>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
map <int,int> prim_factor(long long n2) {
map<int,int> maps;
int N = n2;
for(int i=2; i*i<=N; i++) {
while(n2 % i == 0) {
maps[i]++;
n2 /= i;
}
}
if(n2 > 1)
maps[n2]++;
return maps;
};
long long mult(int k) {
long long ans = 1;
for(int i=2; i<=k; i++)
ans *= i;
return ans;
}
int main() {
long long n;
while(scanf("%lld",&n) != EOF) {
map <int,int> :: iterator iter;
map <int,int> maps = prim_factor(n);
int len = 0;
for(iter=maps.begin(); iter!=maps.end(); iter++)
len += iter->second;
long long ans = mult(len);
for(iter=maps.begin(); iter!=maps.end(); iter++)
ans /= mult(iter->second);
printf("%d %lld\n",len,ans);
}
return 0;
}
标签:end algorithm math font 需要 输入数据 int problem scan
原文地址:https://www.cnblogs.com/mysh/p/11306372.html