lightoj1035 欧拉函数(暴力)

该博客介绍了如何利用暴力算法解决lightoj1035题目,涉及计算100以内阶乘的欧拉函数值。首先解释题意,要求以x=px11px22...的形式表示N!,接着阐述解决方案,重点是找出100以内的素数,并对其在N!中出现的次数进行暴力计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意

用表达式 x=px11px22... 的形式表示N!, 1N100

思路

先求出100以内的素数,然后暴力分解,记录每个素数出现的次数;

/*****************************************
Author      :Crazy_AC(JamesQi)
Time        :2016
File Name   :欧拉函数暴力
*****************************************/
// #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <climits>
using namespace std;
#define MEM(x,y) memset(x, y,sizeof x)
#define pk push_back
#define lson rt << 1
#define rson rt << 1 | 1
#define bug cout << "BUG HERE\n"
#define debug(x) cout << #x << " = " << x << endl
#define ALL(v) (v).begin(), (v).end()
#define lowbit(x) ((x)&(-x))
#define Unique(x) sort(ALL(x)); (x).resize(unique(ALL(x)) - (x).begin())
#define BitOne(x) __builtin_popcount(x)
#define showtime printf("time = %.15f\n",clock() / (double)CLOCKS_PER_SEC)
#define Rep(i, l, r) for (int i = l;i <= r;++i)
#define Rrep(i, r, l) for (int i = r;i >= l;--i)
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> ii;
typedef pair<ii,int> iii;
const double eps = 1e-8;
const double pi = 4 * atan(1);
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const int MOD = 1e9 + 7;
int nCase = 0;
//精度正负、0的判断
int dcmp(double x){if (fabs(x) < eps) return 0;return x < 0?-1:1;}
template<class T> inline bool read(T &n){
    T x = 0, tmp = 1;
    char c = getchar();
    while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();
    if(c == EOF) return false;
    if(c == '-') c = getchar(), tmp = -1;
    while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar();
    n = x*tmp;
    return true;
}
template <class T> inline void write(T n){
    if(n < 0){putchar('-');n = -n;}
    int len = 0,data[20];
    while(n){data[len++] = n%10;n /= 10;}
    if(!len) data[len++] = 0;
    while(len--) putchar(data[len]+48);
}
LL QMOD(LL x, LL k) {
    LL res = 1LL;
    while(k) {if (k & 1) res = res * x % MOD;k >>= 1;x = x * x % MOD;}
    return res;
}
int prime[100];
bool vis[100];
inline void init() {
    for (int i = 2;i < 100;++i) {
        if (!vis[i]) {
            prime[++prime[0]] = i;
            for (int j = i * i;j < 100;j += i)
                vis[j] = true;
        }
    }
}
int a[100];
int main(int argc, const char * argv[])
{    
    // freopen("/Users/jamesqi/Desktop/in.txt","r",stdin);
    // freopen("/Users/jamesqi/Desktop/out.txt","w",stdout);
    // ios::sync_with_stdio(false);
    // cout.sync_with_stdio(false);
    // cin.sync_with_stdio(false);

    int kase;cin >> kase;
    init();
    while(kase--) {
        long long n;cin >> n;
        memset(a, 0, sizeof a);
        for (int i = 1;i <= n;++i) {
            int x = i;
            for (int j = 1;j <= prime[0] && prime[j] * prime[j] <= x;++j) {
                if (x % prime[j] == 0) {
                    while(x % prime[j] == 0) {
                        ++a[prime[j]];
                        x /= prime[j];
                    }
                }
            }
            //very Important
            if (x > 1) ++a[x];
        }
        vector<ii> buff;
        for (int i = 2;i < 100;++i)
            if (a[i] != 0) buff.push_back(ii(i, a[i]));
        printf("Case %d: ", ++nCase);
        printf("%lld =", n);
        int Size = buff.size();
        for (int i = 0;i < Size;++i) {
            printf(" %d (%d)", buff[i].first, buff[i].second);
            if (i != Size - 1) printf(" *");
        }
        printf("\n");
    }

    // showtime;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值