题目描述

给定整数,求且为素数的数对有多少对.

分析

首先筛出所有的素数。

我们考虑枚举素数p,统计满足的个数,等价于统计的个数,即统计以内满足互质的有序数对个数。

不难发现,也就是说,我们只要预处理出欧拉函数,就可以在之内求出。

我们只需要用线性筛预处理出素数和欧拉函数,然后求,就可以在内解决问题。

代码

#include <cstdio>

typedef long long lint;

const int N=10000010;

int n;

int vis[N];
int pri[N];
int phi[N];

lint d[N];

lint res;

int main(void)
{
    scanf("%d",&n);

    vis[0]=vis[1]=1,phi[1]=1;
    for (int i=2;i<=n;i++)
    {
        if (!vis[i]) pri[++pri[0]]=i,phi[i]=i-1;
        for (int j=1;j<=pri[0];j++)
        {
            if ((lint)i*pri[j]>n) break;
            vis[i*pri[j]]=1;
            if (i%pri[j])
                phi[i*pri[j]]=phi[i]*phi[pri[j]];
            else phi[i*pri[j]]=phi[i]*pri[j];
            if (i%pri[j]==0) break;
        }
    }

    d[1]=1;
    for (int i=2;i<=n;i++)
        d[i]=d[i-1]+phi[i]*2;

    for (int i=1;i<=pri[0];i++)
        res=res+d[n/pri[i]];
    printf("%lld\n",res);

    return 0;
}

【BZOJ 2818】Gcd - 筛法求素数&phi()的更多相关文章

  1. BZOJ 2818: Gcd 筛法

    2818: Gcd 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=2818 Description 给定整数N,求1<=x,y< ...

  2. 初等数论-Base-1(筛法求素数,欧拉函数,欧几里得算法)

    前言 初等数论在OI中应用的基础部分,同机房的AuSquare和zhou2003君早就写完了,一直划水偷懒的Hk-pls表示很方,这才开始了这篇博客. \(P.S.\)可能会分部分发表. Base-1 ...

  3. 2018牛客网暑期ACM多校训练营(第三场) H - Diff-prime Pairs - [欧拉筛法求素数]

    题目链接:https://www.nowcoder.com/acm/contest/141/H 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K ...

  4. 蓝桥杯 算法训练 Torry的困惑(基本型)(水题,筛法求素数)

    算法训练 Torry的困惑(基本型) 时间限制:1.0s   内存限制:512.0MB      问题描述 Torry从小喜爱数学.一天,老师告诉他,像2.3.5.7……这样的数叫做质数.Torry突 ...

  5. hdu 4548 筛法求素数 打表

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=4548 Problem Description 小明对数的研究比较热爱,一谈到数,脑子里就涌现出好多数的问题 ...

  6. UVA 10006 - Carmichael Numbers 数论(快速幂取模 + 筛法求素数)

      Carmichael Numbers  An important topic nowadays in computer science is cryptography. Some people e ...

  7. 筛法求素数Java

    输出:一个集合S,表示1~n以内所有的素数 import java.util.Scanner; public class 筛法求素数 { public static void main(String[ ...

  8. Algorithm --> 筛法求素数

    一般的线性筛法 genPrime和genPrime2是筛法求素数的两种实现,一个思路,表示方法不同而已. #include<iostream> #include<math.h> ...

  9. JD 题目1040:Prime Number (筛法求素数)

    OJ题目:click here~~ 题目分析:输出第k个素数 贴这么简单的题目,目的不清纯 用筛法求素数的基本思想是:把从1開始的.某一范围内的正整数从小到大顺序排列, 1不是素数,首先把它筛掉.剩下 ...

随机推荐

  1. xcode下编译.a文件的路径

    http://www.it165.net/pro/html/201503/36842.html 每当我们编译之后, 实际上系统就给我编译好了一个可以运行的.app文件,在某个路径下 如果我们建立的是静 ...

  2. mvcAPI (入门 3)

    续上 1)无参数Get请求 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> < ...

  3. FlexCell控件的使用

    private void grid1_GetCellText(object Sender, FlexCell.Grid.GetCellTextEventArgs e) { // 要使用虚表,可以在Gr ...

  4. POJ 1840 Eqs 暴力

      Description Consider equations having the following form: a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 The ...

  5. HDU 5615 Jam's math problem

    Jam's math problem Problem Description Jam has a math problem. He just learned factorization.He is t ...

  6. Centos6.6 tar包安装JDK

    Linux CentOS 6.6安装JDK1.7 目录 1.下载JDK 2.卸载JDK 3.安装JDK 3.1..rpm后缀格式JDK安装方式 3.2..tar.gz后缀格式JDK安装方式 4.验证安 ...

  7. C#(数据类型)

    刚开始学c#!!!

  8. HDU 4870 Rating 概率DP

    Rating Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Statu ...

  9. centos 扩展root根分区的大小

    目标:将VolGroup-lv_home缩小到125G,并将剩余的空间添加给VolGroup-lv_root 1.首先查看磁盘使用情况[root@localhost ~]# df -h文件系统     ...

  10. Android中四种OnClick事件的写法

    package com.example.dailphone; import android.support.v7.app.ActionBarActivity; import android.suppo ...