热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

求有序对的个数,a*p+b*q=N,其中p和q为素数

求有序对的个数,a*p+b*q=N,其中p和q为素数

求有序对的个数,a * p + b * q = N,其中 p 和 q 为素数

原文:https://www . geesforgeks . org/find-有序对的数量-这样-a-p-b-q-n-其中-p-和-q-是素数/

给定一个数组 arr[] ,以及表示查询数的整数 Q 和两个数字 a,b ,任务是找到有序对(p,Q)的数量,使得 a * p + b * q = arr[i] ,其中 p 和 Q 是素数。
举例:

输入: Q = 3,arr[] = { 2,7,11 },a = 1,b = 2
输出: 0 1 2
解释:
2 - >不存在 p + 2q = 2 的有序对(p,Q)。
7 - >只有一个有序对(p,q) = (3,2),这样 p + 2
q = 7。
11 - >有两个有序对(p,q) = (7,2),(5,3),这样 p + 2q = 11。
输入: Q = 2,arr[] = { 15,25 },a = 1,b = 2
输出:* 2 3

方法:想法是使用厄拉多塞的筛将每个素数存储在一个数组中。存储素数后,计算有序对(p,q)的数量,使得素数数组中(p,q)的每个组合的 ap + bq = N。
以下是上述方法的实施:

C++

// C++ program to find the number of ordered
// pairs such that a * p + b * q = N
// where p and q are primes
#include
#define size 10001
using namespace std;
int prime[size];
int freq[size];
// Sieve of erastothenes
// to store the prime numbers
// and their frequency in form a*p+b*q
void sieve(int a, int b)
{
    prime[1] = 1;
    // Performing Sieve of Eratosthenes
    // to find the prime numbers unto 10001
    for (int i = 2; i * i         if (prime[i] == 0) {
            for (int j = i * 2; j                 prime[j] = 1;
        }
    }
    // Loop to find the number of
    // ordered pairs for every combination
    // of the prime numbers
    for (int p = 1; p         for (int q = 1; q             if (prime[p] == 0 && prime[q] == 0
                && a * p + b * q                 freq[a * p + b * q]++;
            }
        }
    }
}
// Driver code
int main()
{
    int queries = 2, a = 1, b = 2;
    sieve(a, b);
    int arr[queries] = { 15, 25 };
    // Printing the number of ordered pairs
    // for every query
    for (int i = 0; i         cout <    }
    return 0;
}

Java 语言(一种计算机语言,尤用于创建网站)

// Java program to find the number of ordered
// pairs such that a * p + b * q = N
// where p and q are primes
public class GFG {
    final static int size = 10001;
    static int prime[] = new int[size];
    static int freq[] = new int [size];
    // Sieve of erastothenes
    // to store the prime numbers
    // and their frequency in form a*p+b*q
    static void sieve(int a, int b)
    {
        prime[1] = 1;
        // Performing Sieve of Eratosthenes
        // to find the prime numbers unto 10001
        for (int i = 2; i * i             if (prime[i] == 0) {
                for (int j = i * 2; j                     prime[j] = 1;
            }
        }
        // Loop to find the number of
        // ordered pairs for every combination
        // of the prime numbers
        for (int p = 1; p             for (int q = 1; q                 if (prime[p] == 0 && prime[q] == 0
                    && a * p + b * q                     freq[a * p + b * q]++;
                }
            }
        }
    }
    // Driver code
    public static void main (String[] args)
    {
        int queries = 2, a = 1, b = 2;
        sieve(a, b);
        int arr[] = { 15, 25 };
        // Printing the number of ordered pairs
        // for every query
        for (int i = 0; i             System.out.print(freq[arr[i]] + " ");
        }
    }
}
// This code is contributed by AnkitRai01

Python 3

# Python3 program to find the number of ordered
# pairs such that a * p + b * q = N
# where p and q are primes
from math import sqrt
size = 1000
prime = [0 for i in range(size)]
freq = [0 for i in range(size)]
# Sieve of erastothenes
# to store the prime numbers
# and their frequency in form a*p+b*q
def sieve(a, b):
    prime[1] = 1
    # Performing Sieve of Eratosthenes
    # to find the prime numbers unto 10001
    for i in range(2, int(sqrt(size)) + 1, 1):
        if (prime[i] == 0):
            for j in range(i*2, size, i):
                prime[j] = 1
    # Loop to find the number of
    # ordered pairs for every combination
    # of the prime numbers
    for p in range(1, size, 1):
        for q in range(1, size, 1):
            if (prime[p] == 0 and prime[q] == 0 and a * p + b * q                 freq[a * p + b * q] += 1
# Driver code
if __name__ == '__main__':
    queries = 2
    a = 1
    b = 2
    sieve(a, b)
    arr = [15, 25]
    # Printing the number of ordered pairs
    # for every query
    for i in range(queries):
        print(freq[arr[i]],end = " ")
# This code is contributed by Surendra_Gangwar

C

// C# program to find the number of ordered
// pairs such that a * p + b * q = N
// where p and q are primes
using System;
class GFG {
    static int size = 10001;
    static int []prime = new int[size];
    static int []freq = new int [size];
    // Sieve of erastothenes
    // to store the prime numbers
    // and their frequency in form a*p+b*q
    static void sieve(int a, int b)
    {
        prime[1] = 1;
        // Performing Sieve of Eratosthenes
        // to find the prime numbers unto 10001
        for (int i = 2; i * i             if (prime[i] == 0) {
                for (int j = i * 2; j                     prime[j] = 1;
            }
        }
        // Loop to find the number of
        // ordered pairs for every combination
        // of the prime numbers
        for (int p = 1; p             for (int q = 1; q                 if (prime[p] == 0 && prime[q] == 0
                    && a * p + b * q                     freq[a * p + b * q]++;
                }
            }
        }
    }
    // Driver code
    public static void Main (string[] args)
    {
        int queries = 2, a = 1, b = 2;
        sieve(a, b);
        int []arr = { 15, 25 };
        // Printing the number of ordered pairs
        // for every query
        for (int i = 0; i             Console.Write(freq[arr[i]] + " ");
        }
    }
}
// This code is contributed by AnkitRai01

java 描述语言


Output: 

2 3

时间复杂度: O(N)

辅助空间: O(大小)


推荐阅读
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • 本文介绍了C#中数据集DataSet对象的使用及相关方法详解,包括DataSet对象的概述、与数据关系对象的互联、Rows集合和Columns集合的组成,以及DataSet对象常用的方法之一——Merge方法的使用。通过本文的阅读,读者可以了解到DataSet对象在C#中的重要性和使用方法。 ... [详细]
  • 闭包一直是Java社区中争论不断的话题,很多语言都支持闭包这个语言特性,闭包定义了一个依赖于外部环境的自由变量的函数,这个函数能够访问外部环境的变量。本文以JavaScript的一个闭包为例,介绍了闭包的定义和特性。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 本文介绍了九度OnlineJudge中的1002题目“Grading”的解决方法。该题目要求设计一个公平的评分过程,将每个考题分配给3个独立的专家,如果他们的评分不一致,则需要请一位裁判做出最终决定。文章详细描述了评分规则,并给出了解决该问题的程序。 ... [详细]
  • baresip android编译、运行教程1语音通话
    本文介绍了如何在安卓平台上编译和运行baresip android,包括下载相关的sdk和ndk,修改ndk路径和输出目录,以及创建一个c++的安卓工程并将目录考到cpp下。详细步骤可参考给出的链接和文档。 ... [详细]
  • C# 7.0 新特性:基于Tuple的“多”返回值方法
    本文介绍了C# 7.0中基于Tuple的“多”返回值方法的使用。通过对C# 6.0及更早版本的做法进行回顾,提出了问题:如何使一个方法可返回多个返回值。然后详细介绍了C# 7.0中使用Tuple的写法,并给出了示例代码。最后,总结了该新特性的优点。 ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • 先看官方文档TheJavaTutorialshavebeenwrittenforJDK8.Examplesandpracticesdescribedinthispagedontta ... [详细]
  • JDK源码学习之HashTable(附带面试题)的学习笔记
    本文介绍了JDK源码学习之HashTable(附带面试题)的学习笔记,包括HashTable的定义、数据类型、与HashMap的关系和区别。文章提供了干货,并附带了其他相关主题的学习笔记。 ... [详细]
  • 模板引擎StringTemplate的使用方法和特点
    本文介绍了模板引擎StringTemplate的使用方法和特点,包括强制Model和View的分离、Lazy-Evaluation、Recursive enable等。同时,还介绍了StringTemplate语法中的属性和普通字符的使用方法,并提供了向模板填充属性的示例代码。 ... [详细]
  • 本文记录了在vue cli 3.x中移除console的一些采坑经验,通过使用uglifyjs-webpack-plugin插件,在vue.config.js中进行相关配置,包括设置minimizer、UglifyJsPlugin和compress等参数,最终成功移除了console。同时,还包括了一些可能出现的报错情况和解决方法。 ... [详细]
  • 本文介绍了Swing组件的用法,重点讲解了图标接口的定义和创建方法。图标接口用来将图标与各种组件相关联,可以是简单的绘画或使用磁盘上的GIF格式图像。文章详细介绍了图标接口的属性和绘制方法,并给出了一个菱形图标的实现示例。该示例可以配置图标的尺寸、颜色和填充状态。 ... [详细]
  • C# WPF自定义按钮的方法
    本文介绍了在C# WPF中实现自定义按钮的方法,包括使用图片作为按钮背景、自定义鼠标进入效果、自定义按压效果和自定义禁用效果。通过创建CustomButton.cs类和ButtonStyles.xaml资源文件,设计按钮的Style并添加所需的依赖属性,可以实现自定义按钮的效果。示例代码在ButtonStyles.xaml中给出。 ... [详细]
author-avatar
无敌BUG
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有