Bzoj 2190 sdoi 2008 Honor guard linear Euler sieve

Source: Internet
Author: User

Title Effect: There is a grid component diagram, assuming that three people in a straight line, then the first person will not see a third person. Now, there is a man standing in the. I asked him if he could see the matrix of the number of n*n.


Think: If you want to stand (in the first place) This man sees a position (x, y) of a person. GCD (x, y) = = 1, which is a classic model, only requires a PHI within N and is able to.

The method is linear sieve.


CODE:


#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define MAX 40010using namespace Std;int n;bool not_prime[max];int prime[max],primes;int phi[max];void Eratosthenes (); int main () { Cin >> n;n--; Eratosthenes (); int ans = 0;for (int i = 2;i <= n; ++i) ans + = Phi[i];cout << (ans << 1) + 3 << Endl;ret Urn 0;} void Eratosthenes () {phi[1] = 1;for (int i = 2;i <= n; ++i) {if (!not_prime[i]) prime[++primes] = I,phi[i] = i-1;for (int j = 1;j <= primes && prime[j] * i <= N; ++J) {not_prime[prime[j] * i] = true;if (i% prime[j] = = 0) {Phi[i * prime[j]] = phi[i] * prime[j];break;} Elsephi[i * Prime[j]] = phi[i] * (Prime[j]-1);}}}


Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.

Bzoj 2190 sdoi 2008 Honor guard linear Euler sieve

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.