"SDOI2008" "P1377" honor Guard

Source: Internet
Author: User
Tags gcd

The application of Euler function

Original question:

As a sports Commissioner, C June is responsible for the training of the honor guard.    Guard of Honor is composed of students of N * N of the square, in order to ensure that the procession uniform, c June will follow the guard of Honor's left rear, according to the number of students in the line of sight to determine whether the team is neat (such as). Now, C-June wants you to tell him the number of students you can see when the team is neat.

1<=n<=40000

First, the graph along the lower left corner to the upper right corner of the diagonal fold, the two sides are the same, so only one side of the *2+1 can be asked (because there is a diagonal, so + 1)

How do you ask for each side?

You can find that all the points you see meet gcd (x, y) ==1

And then you're asking for 1<=x<=n-1 's Phi.

Why is that?

First y must be smaller than x, or run to the other side (on the face of the folding ↑), and then gcd (x, y) also ==1, is smaller than x and x coprime number of numbers, is also the definition of Euler functions, so directly to Phi and can

It is important to note that the coordinates of the lower left corner should be set to 0 (which is also the reason for the 1<=x<=n-1 Phi and ↑), as this will ensure that X-y is the horizontal distance from the lower-left corner

Code:

1#include <iostream>2#include <cstdio>3#include <algorithm>4#include <cstring>5#include <cmath>6 using namespacestd;7 intN;8 intkang[41000],phi[41000],zhi[410000],ztop=0;9 voidShai () {Tenmemset (Kang,0,sizeof(Kang)); Onephi[1]=1; A      for(intI=2; i<=n;i++){ -         if(!kang[i]) {phi[i]=i-1; zhi[++ztop]=i;} -          for(intj=1; J<=ztop && i*zhi[j]<=n;j++){ thekang[i*zhi[j]]=true; -             if(i%zhi[j]==0) {Phi[i*zhi[j]]=phi[i]*zhi[j]; Break;} -phi[i*zhi[j]]=phi[i]*Phi[zhi[j]]; -         } +     } - } + intMain () {//freopen ("ddd.in", "R", stdin); ACin>>N; at Shai (); -     Long LongBowl=0; -      for(intI=1; i<n;i++) bowl+=Phi[i]; -cout<<bowl*2+1<<Endl; -     return 0; -}
View Code

"SDOI2008" "P1377" honor Guard

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.