Hdoj 2582 F (n) (yy+ search pattern)

Source: Internet
Author: User
Tags gcd

Test instructions

GCD (n) =gcd (c[n][1],c[n][2],......, c[n][n-1])
F (n) = GCD (3) +GCD (4) +...+GCD (i) +...+GCD (n).
F (N).

Ideas

At the beginning of the GCD () to eliminate the formula to see, found that gcd (n) seems to be equal to N, and then Python forget the second example found the wrong, and then consider the possible quality factors will make the GCD smaller, hit a table, and then found a total of three cases:
1. If n is a prime number, GCD (n) = n
2. If n is a unique mass factor, GCD (n) = P
3. If n has multiple mass factors, GCD (n) = 1
Then just enumerate the number of the 1e6 number of qualitative factors to be good.

Code
#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <stack>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <time.h>using namespace STD;#define LL Long Long#define LOWBIT (x) ((x) & ( -X))#define Lson L, Mid, RT << 1#define Rson Mid + 1, R, RT << 1|1#define MP (A, B) Make_pair (A, b)Const intINF =0x3f3f3f3f;Const intMAXN =1e6+7;Const DoubleEPS =1e-8;Const DoublePI =ACOs(-1.0); LL SUM[MAXN];BOOLVIS[MAXN]; vector<int>PrimevoidInit () { for(inti =2; I * I <= MAXN; i++)if(!vis[i]) {Prime.push_back (i); for(intj = i *2; J <= Maxn; J + = i) vis[j] =1; }}intSolveintN) {intResintCNT =0; for(inti =2; I * I <= N; i++)if(n% i = =0) {cnt++, res = i; while(n% i = =0) n/= i;if(CNT >1)return 1; }if(N >1) cnt++;if(CNT >1)return 1;Else returnRes;}intMain () {//freopen ("In.txt", "R", stdin);    //freopen ("OUT.txt", "w", stdout);    intN    Init (); sum[2] =0; for(inti =3; I <= MAXN; i++)//Prime is N, a mass factor is p, and several qualitative factors are 1        if(!vis[i]) sum[i] = sum[i-1] + i;ElseSum[i] = sum[i-1] + solve (i); while(Cin>> N) {cout<< Sum[n] << Endl; }return 0;}

Hdoj 2582 F (n) (yy+ search pattern)

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.