SOJ2666 Decomposition of Problem solving report n!

Source: Internet
Author: User

SOJ2666 decomposition of Problem solving report n!
Description give you a number n (1 < n <= 1000000), Beg n! The factorization form of the mass factor (n factorial) is n=p1^m1*p2^m2*p3^m3......*  here P1 < P2 < P3 < ... For prime numbers *  if mi = 1, then the   ^ mi   input input is multiple case, one number per line n,1 < n <= 1000000, when n equals 0 o'clock input end   output one row per n for its mass factorization form, sample Inputsample output   Author  windy7926778

The main idea of the topic: slightly.
Analysis: The feeling is the bare prime number Operation property. There is a property of prime number, in the decomposition of n!, the quantity of themass factor P is pn (n,p) =[n/p]+[n/p^2]+[n/p^3]+ ... Until [N/p^q]==0, where [] means rounding down. So we're going to break down the n!. You can first use the number of sieve to hit the table, and then decomposition by one to see how many, and finally output results can be. Note The format of the output.
on the code:
#include <iostream> #include <cmath> #include <cstring>using namespace Std;int isprime[1010000];long  Long Prime[1010000];int pnum[1010000];int cnt;void Getp () {for (int i = 1; i < 1010000; i++) Isprime[i] = 1;for (int i = 2; i < 1010000; i++) {if (!isprime[i]) continue;prime[cnt++] = i;for (int j = 2 * i; J < 1010000; J + = i) isprime[j] = 0;}} Long long get (long long n, long long key) {int tem = Key;long Long num = 0;while (n >= key) {num + = N/key;key *= tem;} return num;} int main () {Long long n;getp (), while (CIN >> n &&n) {if (n = = 1) {cout << "1=1" << endl;continue;} memset (pnum, 0, sizeof pnum); for (int i = 0; Prime[i] <= n&&prime[i]; i++) {Pnum[i] = Get (n, Prime[i]);} cout << n << "="; bool FST = 0;for (int i = 0; Prime[i] <= n&&prime[i]; i++) {if (!fst) FST = True;else cout << "*"; if (Pnum[i]) cout << prime[i];if (pnum[i]>=2) cout << "^" << pnum[i];} cout << Endl;} return 0;}

Changmei Sister salute!

SOJ2666 Decomposition of Problem solving report n!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.