zoukankan      html  css  js  c++  java
  • BZOJ 2818 Gcd 线性欧拉筛(Eratosthenes银幕)

    标题效果:定整N(N <= 1e7),乞讨1<=x,y<=N和Gcd(x,y)素数的数(x,y)有多少.、


    思考:推,。

    建立gcd(x,y) = p,然后,x / p与y / p互素

    问题就转化成了N / p中有多少个数互质,然后累加就能够了.

    =>对于随意a,b,a <= N / p,b <= N / p,且a与b互质

    =>gcd(a,b) == 1

    如今问题就非常明显了。看到这个形式就非常easy想到欧拉函数。求一下phi,算一下前缀和,累加。

    注意这里求欧拉一定要线性的,1qw的数据。nloglogn都非常悬。


    CODE:


    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #define MAX 10000010
    using namespace std;
    
    bool not_prime[MAX];
    int prime[MAX],primes;
    long long phi[MAX],ans;
    
    int n;
    
    inline void Eratosthenes();
    
    int main()
    {
    	cin >> n;
    	Eratosthenes();
    	for(int i = 2;i <= n; ++i)
    		phi[i] += phi[i - 1];
    	for(int i = 1;i <= primes; ++i)
    		ans += phi[n / prime[i]];
    	cout << (ans << 1) - primes << endl;
    	return 0;
    }
    
    inline 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[i * prime[j]] = true;
    			if(i % prime[j] == 0) {
    				phi[i * prime[j]] = phi[i] * prime[j];
    				break;
    			}
    			else	phi[i * prime[j]] = phi[i] * (prime[j] - 1);
    		}
    	}
    }

     

    版权声明:本文博主原创文章。博客,未经同意不得转载。

  • 相关阅读:
    some tips
    ORA00847: MEMORY_TARGET/MEMORY_MAX_TARGET and LOCK_SGA cannot be set together
    Chapter 01Overview of Oracle 9i Database Perfomrmance Tuning
    Chapter 02Diagnostic and Tuning Tools
    变量与常用符号
    Chapter 18Tuning the Operating System
    标准输入输出
    Trace files
    DBADeveloped Tools
    Chapter 03Database Configuration and IO Issues
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/4908249.html
Copyright © 2011-2022 走看看