Principle of HDU 4407 sum

Source: Internet
Author: User

The meaning is very simple, that is, to give you an array of N values from 1 to n, then there are m operations, or modify one of the preceding arrays, it is either the sum of the numbers from X to Y (including the boundary) and the numbers of P mutual quality. Since m is very small (M <= 1000), when we save the modified part and output the statistics, we can determine whether the modified part is of mutual quality with P. If we want to modify the original one, modify the current one.

Note that only the last modification is recorded.

For the sum of the numbers from X to Y and P, there is a result = Number 1 to y that satisfies the requirement-Number 1 to the result that the X-1 meets the requirement.

The sum of the numbers starting from 1 to n that are mutually dependent on P is an algorithm based on the exclusion principle.

Remove the multiples of a single prime factor in P, add the multiples of two factors, and then remove the multiples of the three prime factors until all prime factors end.

. /*** @ Title: hdustm7.java * @ package COM. wangzhu. HDU * @ Description: Todo * @ author * @ date 2012-9-22 7:40:27 * @ version V1.0 **/package COM. wangzhu. HDU; import Java. io. bufferedinputstream; import Java. util. arrays; import Java. util. break;/*** @ classname: hduyun 7 * @ description: todo * @ author * @ date 2012-9-22 7:40:27 */public class hdu4407 {public static void main (string [] ARGs ){/ /System. setin (New fileinputstream ("data. in "); then CIN = new second (New bufferedinputstream (system. in); int t, n, m, Cas, X, Y, C, p; initprime (); While (CIN. hasnext () {T = cin. nextint (); While (t --> 0) {n = cin. nextint (); M = cin. nextint (); node = new node [M + 1]; lastindex = new int [maxn]; nodelen = 0; For (INT I = 0; I <m; I ++) {CAS = cin. nextint (); If (CAS = 2) {x = cin. nextint (); C = cin. nextint (); node [nodelen] = new node (x, c); lastindex [x] = nodelen ++;} else {x = cin. nextint (); y = cin. nextint (); P = cin. nextint (); Work (X, Y, P, n) ;}}} cin. close ();} private final static int maxn = 400001; static int [] lastindex; private final static int val = 1000; static int [] Prime = new int [Val]; static Boolean [] isprime = new Boolean [Val]; static int primelen; Public stat IC void initprime () {arrays. fill (isprime, false); primelen = 0; prime [primelen ++] = 2; for (INT I = 4; I <val; I ++ = 2) {isprime [I] = true ;}for (INT I = 3; I <val; I ++ = 2) {If (! Isprime [I]) {Prime [primelen ++] = I ;}for (Int J = 0; (j <primelen) & (I * prime [J] <Val); j ++) {isprime [I * prime [J] = true; if (I % prime [J] = 0) {break ;}}} static node [] node; static int nodelen; static int [] primefactor; // storage prime factor static int primefactorlen;/*** decomposition number P obtains its prime factor ** @ Param p */public static void mkprimefactor (int p) {primefactor = new int [Val]; int temp = (INT) math. SQ RT (1.0 * P); primefactorlen = 0; For (INT I = 0; (I <primelen) & (prime [I] <= temp); I ++) {If (P % prime [I] = 0) {primefactor [primefactorlen ++] = prime [I]; while (P % prime [I] = 0) {P/= prime [I] ;}}if (p> 1) {primefactor [primefactorlen ++] = P ;}} Private Static void work (INT X, int y, int P, int N) {mkprimefactor (p); long res = getsum (y)-getsum (X-1); For (INT I = 0; I <nodelen; I ++) {If (node [I]. x> = x & node [I]. x <= y) {// if the same place is modified multiple times, only the last if (I! = Lastindex [node [I]. x]) {continue;} If (iscoprime (node [I]. x, p) {res-= node [I]. x;} If (iscoprime (node [I]. c, p) {res + = node [I]. c ;}} system. out. println (RES);} Private Static long getsum (int I) {long res = (long) I * (I + 1)/2; res-= DFS (0, I, 1); Return res;} Private Static Boolean iscoprime (int c, int p) {return gcd (C, p) = 1 ;} /***** maximum common divisor ** @ Param A * @ Param B * @ return */P Ublic static long gcd (long a, long B) {long R; if (a <B) {r = A; A = B; B = r ;}while (B! = 0) {r = A % B; A = B; B = r;} return ;} /*** the refresh principle is used to obtain the sum of all the numbers that interact with P under N and ** @ Param start * @ Param N * @ Param now * @ return */Private Static long. DFS (INT start, int N, Long Now) {long res = 0, temp; For (INT I = start; I <primefactorlen; I ++) {temp = lcm (now, primefactor [I]); // each start, followed by its minimum public multiple res + = getprimesum (temp, n)-DFS (I + 1, n, temp );} return res;}/*** minimum public multiple ** @ Param now * @ Param I * @ return */Private Static long lcm (long now, long I) {return (long) now/gcd (now, I) * I;} Private Static long getprimesum (long num, int N) {long temp = N/num; return temp * (temp + 1) /2 * num ;}} class node {int X; int C; Public node (int x, int c) {This. X = x; this. C = C ;}}

 

 

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.