标签:stack 个数 tac def 包含 out std 算数 ack
这样分析的话,其实把阶乘算出来再搞是必然超时的
对于每个质因子p,就相当于1N每个数包含的质因子p的和。在1Nz中包含1个质因子的有N/p个,p2则为N/p2,,,,,以此类推
所以N!中质因子的个数:
n/p+n/p2+n/p3.......
#include <iostream>
#include <stack>
#include<algorithm>
#include <cstring>
#include <stdio.h>
#include <string.h>
#define ll long long
using namespace std;
const int N = 1e6 + 5;
ll n;
ll vis[N],p[N],c[N];
ll cnt ;
int main() {
cin >> n;
for (ll i = 2; i <= n; i++) {
if (!vis[i]) {
ll num = 0;
for (ll j = i; j <= n; j *= i) num += n / j;
p[++cnt] = i;
c[cnt] = num;
for (ll j = i * i; j <= n; j += i)//素数筛啊啊啊啊!!保证那个i是个质数
vis[j] = 1;
}
}
for (int i = 1; i <= cnt; i++) cout << p[i] << " " << c[i] << ‘\n‘;
return 0;
}
标签:stack 个数 tac def 包含 out std 算数 ack
原文地址:https://www.cnblogs.com/zhmlzhml/p/14327682.html