Poj about LCM

Source: Internet
Author: User

Note:

G (n) = Sigma {PHI (N/d) * n/d}, d | n

F (n) = (G (n) + 1)/2

Due to the product of the Euler's function, Phi (N * m) = PHI (n) * PHI (M), gcd (n, m) = 1

Therefore, n = Pi (PI ^ CI)

Therefore, g (n) = g (PI ^ CI )).

G (n) = Pi (G (PI ^ CI)

G (PI ^ CI) = sigma (PHI (PI ^ CI)

For PHI (PI ^ CI) (Pi-1) * (PI ^ (ci-1)

This problem is solved now!

My code:

Code for Timeout:

LL sumlcm(int n) {
int te = 0, temp = n;
LL sum = 0;
if (n == 1)
return 1;
te = (int) sqrt(n * 1.0);
sum = (LL) n;
for (int i = 2; i <= te; i++) {
if (n % i == 0) {
sum = sum / i * (i - 1);
while (n % i == 0) {
n /= i;
}
}
}
if (n > 1) {
sum = sum / n * (n - 1);
}
return sum * temp / 2;
}
void solve(int n) {
int i, te, temp;
LL sum;
te = (int) sqrt(n * 1.0);
for (i = 1, sum = 0; i <= te; i++) {
if (n % i == 0) {
temp = n / i;
sum += sumlcm(temp);
if (i != temp) {
sum += sumlcm(i);
}
}
}
printf("%I64d\n", sum);
}

AC code:

#include<stdio.h>
#include<string.h>
#include<math.h>
#define LL long long
#define nmax 44725
int flag[nmax], prime[nmax], plen;
void mkprime() {
memset(flag, -1, sizeof(flag));
int i, j;
for (i = 2, plen = 0; i < nmax; i++) {
if (flag[i]) {
prime[plen++] = i;
}
for (j = 0; (j < plen) && (i * prime[j] < nmax); j++) {
flag[i * prime[j]] = 0;
if (i % prime[j] == 0) {
break;
}
}
}
}
int getPow(int a, int b) {
int res;
res = 1;
while (b) {
if (b & 1) {
res = res * a;
}
a = a * a;
b >>= 1;
}
return res;
}
int getpPhi(int p, int c) {
if (c == 0) {
return 1;
}
return (p - 1) * getPow(p, c - 1);
}

LL calc(int p, int c) {
int i;
LL temp, sum;
for (i = 0, sum = 0LL, temp = 1LL; i <= c; i++) {
sum += temp * getpPhi(p, i);
temp = temp * p;
}
return sum;
}
void solve(int n) {
int i, te, cnt;
LL res;
te = (int) (sqrt(n * 1.0));
for (i = 0, res = 1LL; (i < plen) && (prime[i] <= te); i++) {
if (n % prime[i] == 0) {
cnt = 0;
while (n % prime[i] == 0) {
cnt++;
n /= prime[i];
}
res = res * calc(prime[i], cnt);
}
}
if (n > 1) {
res = res * calc(n, 1);
}
printf("%lld\n", (res + 1) / 2);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif
int n;
mkprime();
while (scanf("%d", &n) != EOF) {
solve(n);
}
return 0;
}
#include<stdio.h>
#include<string.h>
#include<math.h>
#define LL long long
#define nmax 44725
int flag[nmax], prime[nmax], plen;
void mkprime() {
memset(flag, -1, sizeof(flag));
int i, j;
for (i = 2, plen = 0; i < nmax; i++) {
if (flag[i]) {
prime[plen++] = i;
}
for (j = 0; (j < plen) && (i * prime[j] < nmax); j++) {
flag[i * prime[j]] = 0;
if (i % prime[j] == 0) {
break;
}
}
}
}
LL getPow(int a, int b) {
LL res, temp;
res = 1, temp = (LL) a;
while (b) {
if (b & 1) {
res = res * temp;
}
temp = temp * temp;
b >>= 1;
}
return res;
}
LL calc(int p, int c) {
LL res;
res = getPow(p, 2 * c);
res--;
res = res / (p + 1);
res = res * p;
res++;
return res;
}
void solve(int n) {
int i, te, cnt;
LL res;
te = (int) (sqrt(n * 1.0));
for (i = 0, res = 1LL; (i < plen) && (prime[i] <= te); i++) {
if (n % prime[i] == 0) {
cnt = 0;
while (n % prime[i] == 0) {
cnt++;
n /= prime[i];
}
res = res * calc(prime[i], cnt);
}
}
if (n > 1) {
res = res * calc(n, 1);
}
printf("%lld\n", (res + 1) / 2);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif
int n;
mkprime();
while (scanf("%d", &n) != EOF) {
solve(n);
}
return 0;
}

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.