热门标签 | 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(大小)


推荐阅读
  • 微软头条实习生分享深度学习自学指南
    本文介绍了一位微软头条实习生自学深度学习的经验分享,包括学习资源推荐、重要基础知识的学习要点等。作者强调了学好Python和数学基础的重要性,并提供了一些建议。 ... [详细]
  • 本文介绍了闭包的定义和运转机制,重点解释了闭包如何能够接触外部函数的作用域中的变量。通过词法作用域的查找规则,闭包可以访问外部函数的作用域。同时还提到了闭包的作用和影响。 ... [详细]
  • 本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • 目录实现效果:实现环境实现方法一:基本思路主要代码JavaScript代码总结方法二主要代码总结方法三基本思路主要代码JavaScriptHTML总结实 ... [详细]
  • 本文讨论了在Windows 8上安装gvim中插件时出现的错误加载问题。作者将EasyMotion插件放在了正确的位置,但加载时却出现了错误。作者提供了下载链接和之前放置插件的位置,并列出了出现的错误信息。 ... [详细]
  • 如何使用Java获取服务器硬件信息和磁盘负载率
    本文介绍了使用Java编程语言获取服务器硬件信息和磁盘负载率的方法。首先在远程服务器上搭建一个支持服务端语言的HTTP服务,并获取服务器的磁盘信息,并将结果输出。然后在本地使用JS编写一个AJAX脚本,远程请求服务端的程序,得到结果并展示给用户。其中还介绍了如何提取硬盘序列号的方法。 ... [详细]
  • 本文介绍了九度OnlineJudge中的1002题目“Grading”的解决方法。该题目要求设计一个公平的评分过程,将每个考题分配给3个独立的专家,如果他们的评分不一致,则需要请一位裁判做出最终决定。文章详细描述了评分规则,并给出了解决该问题的程序。 ... [详细]
  • 本文介绍了C#中数据集DataSet对象的使用及相关方法详解,包括DataSet对象的概述、与数据关系对象的互联、Rows集合和Columns集合的组成,以及DataSet对象常用的方法之一——Merge方法的使用。通过本文的阅读,读者可以了解到DataSet对象在C#中的重要性和使用方法。 ... [详细]
  • javascript  – 概述在Firefox上无法正常工作
    我试图提出一些自定义大纲,以达到一些Web可访问性建议.但我不能用Firefox制作.这就是它在Chrome上的外观:而那个图标实际上是一个锚点.在Firefox上,它只概述了整个 ... [详细]
  • 本文介绍了P1651题目的描述和要求,以及计算能搭建的塔的最大高度的方法。通过动态规划和状压技术,将问题转化为求解差值的问题,并定义了相应的状态。最终得出了计算最大高度的解法。 ... [详细]
  • 计算机存储系统的层次结构及其优势
    本文介绍了计算机存储系统的层次结构,包括高速缓存、主存储器和辅助存储器三个层次。通过分层存储数据可以提高程序的执行效率。计算机存储系统的层次结构将各种不同存储容量、存取速度和价格的存储器有机组合成整体,形成可寻址存储空间比主存储器空间大得多的存储整体。由于辅助存储器容量大、价格低,使得整体存储系统的平均价格降低。同时,高速缓存的存取速度可以和CPU的工作速度相匹配,进一步提高程序执行效率。 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 不同优化算法的比较分析及实验验证
    本文介绍了神经网络优化中常用的优化方法,包括学习率调整和梯度估计修正,并通过实验验证了不同优化算法的效果。实验结果表明,Adam算法在综合考虑学习率调整和梯度估计修正方面表现较好。该研究对于优化神经网络的训练过程具有指导意义。 ... [详细]
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社区 版权所有