n!分解质因数、因数

Problem J

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 90   Accepted Submission(s) : 17
Font: Times New Roman | Verdana | Georgia
Font Size: ← →

Problem Description

  Now there is an integer N. What you should do is to calculate how many different factors the N! has. For example, 3! = 3 * 2 * 1, the factors of 3! are 1,2,3,6, so the 3! has 4 factors in total.

Input

  The first line contains an integer T (T <= 100) indicating the number of test cases. In each test cases these is an integer N which is from 0 to 10^7.

Output

  For each test case you should output the number of factors N! has, the number will be quite large so you should output it after mod 10007 in one line.

Sample Input

3
2
3
4

Sample Output

2
4
8
 
 
这道题的代码,分解因数,n!因数个数,感谢hdu-Tyro
#define DeBUG
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <sstream>
#include <map>
#include <bitset>
using namespace std ;
#define zero {0}
#define INF 2000000000
#define EPS 1e-6
typedef long long LL;
const double PI = acos(-1.0);
//#pragma comment(linker, "/STACK:102400000,102400000")
inline int sgn(double x)
{
    return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);
}
const int N = 10000005;
const int md = 10007;
bool pr[N];
int p[N / 10], lp;
void gp()
{
    for (int i = 2; i < N; i++)
    {
        if (!pr[i])
            p[lp++] = i;
        for (int j = 0; j < lp && p[j]*i < N; j++)
        {
            pr[i * p[j]] = 1;
        }
    }
}
int ans[N], cnt[N / 10], sum;
int main()
{
#ifdef DeBUGs
    freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);
#endif
    int n;
    gp();
    int T;
    scanf("%d", &T);
    while (T--)
    {
        scanf("%d", &n);
        if (n < 2)
        {
            printf("1\n");
            continue;
        }
        int ans = 1;
        for (int i = 0; i < lp && p[i] <= n; i++)
        {
            int cnt = 0;
            int tmp = n;
            while (tmp > 1)
            {
                cnt += (tmp /= p[i]);
            }
            cnt = (cnt + 1) % md;
            
            ans = ans * cnt % md;
        }
        printf("%d\n", ans);
    }

    return 0;
}

修改后的分解N!质因数代码
#define DeBUG
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <sstream>
#include <map>
#include <bitset>
using namespace std ;
#define zero {0}
#define INF 2000000000
#define EPS 1e-6
typedef long long LL;
const double PI = acos(-1.0);
//#pragma comment(linker, "/STACK:102400000,102400000")
inline int sgn(double x)
{
    return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);
}
const int N = 10000005;
const int md = 10007;
bool pr[N];
int p[N / 10], lp;
void gp()
{
    for (int i = 2; i < N; i++)
    {
        if (!pr[i])
            p[lp++] = i;
        for (int j = 0; j < lp && p[j]*i < N; j++)
        {
            pr[i * p[j]] = 1;
        }
    }
}
int ans[N], cnt[N / 10], sum;
int main()
{
#ifdef DeBUGs
    freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);
#endif
    int n;
    gp();
    int T;
    scanf("%d", &T);
    while (T--)
    {
        scanf("%d", &n);
        if (n < 2)
        {
            printf("1\n");
            continue;
        }
        int ans = 0;
        for (int i = 0; i < lp && p[i] <= n; i++)
        {
            int cnt = 0;
            int tmp = n;
            while (tmp >=p[i])
            {
                tmp /= p[i];
                cout<<p[i]<<" ";
                cnt++;
            }
            ans += cnt % md;
        }
        printf("质因数个数%d\n", ans);
    }

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值