1188 greatest common Divisor and V2 title Source: UVA Baseline time limit: 2 second space limit: 262144 KB gives a number n, outputs the sum of all the numbers less than or equal to N, and greatest common divisor between 22. Equivalent to the calculation of this procedure (GCD (I,J) in the program represents the greatest common divisor of I and J): G=0;for (i=1;i<n;i++) for (j=i+1;j<=n;j++) {G+=GCD (i,j);} Input
Line 1th: 1 number T, which indicates the number of numbers that are later used as input tests. (1 <= T <= 50000) 2-t + 1 lines: one number per line N. (2 <= N <= 5000000)
Output
A total of T-line, the sum of output greatest common divisor.
Input example
310100200000
Output example
6713015143295493160
Idea: Euler function;
Http://www.cnblogs.com/zzuli2sjy/p/5831575.html is a reinforced version of the problem, where Euler's function is obtained by sieve method, and then the method of similar sieve is used to find the contribution of each number to the answer, and finally the prefix and
1#include <stdio.h>2#include <algorithm>3#include <iostream>4#include <string.h>5#include <queue>6#include <math.h>7#include <Set>8#include <vector>9#include <string.h>Ten using namespacestd; OnetypedefLong LongLL; A BOOLprime[5000005]; - intoula[5000005]; - intans[5000005]; theLL ask[5000005]; - intMainvoid) - { - inti,j; + intT,n; - for(i =2; I <= the; i++) + { A if(!Prime[i]) at { - for(j = i; (I*J) <=5000005; J + +) - { -prime[i*j]=true; - } - } in } - intCN =0; to for(i =2; I <=5000000; i++) + { - if(!Prime[i]) the { *ans[cn++]=i; $ }Panax Notoginseng } - for(i =0; I <=5000000; i++) theoula[i]=i; +oula[1]=0; Amemset (Ask,0,sizeof(ask)); the for(i =0; I < CN; i++) + { - for(j =1; J*ans[i] <=5000000; J + +) $ { $Oula[j*ans[i]]/=Ans[i]; -oula[j*ans[i]]*=ans[i]-1; - } the } - for(i =1; I <=5000000; i++)Wuyi { the for(j =2; (I*J) <=5000000; J + +) - { Wuask[i*j]+=oula[j]*i; - } About } $ for(i =2; I <=5000000; i++) - { -ask[i]+=ask[i-1]; - } Ascanf"%d",&T); + while(t--) the { -scanf"%d",&N); $printf"%lld\n", Ask[n]); the } the return 0; the}
1188 greatest common divisor and V2