查询素数 [英] Queries on a prime number

查看:131
本文介绍了查询素数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个N个整数和Q查询的数组,其中每个查询为您提供四个整数L,R,X和Y.

你必须根据给定的函数计算每个查询的值?

Given an array a of N integers and Q queries where each query provides you with four integers L, R, X and Y.
you have to calculate value of each query according to the given function ?

F(L, R, X, Y) {

     result := 0
     for( i = X to i = Y )  {
         if( isPrime(i) ) {
             for( j = L to j = R ) {
                  number := a[j]
                  exponent := 0
                  while( number % i == 0 ) {
                     exponent := exponent + 1 
                     number := number/i
                  }
                  result := result + exponent
              }
         }
     }
     return result
}





isPrime(x)如果x为Prime则返回true,否则返回false。





isPrime(x) returns true if x is Prime otherwise false.

Example:
4 // size of array
2 3 4 5 // array elements
2 //number of queries
1 3 2 3 
1 4 2 5

Output:
4
5





我尝试过:



i尝试预先计算阵列中所有数字的所有素因子,但之后我在处理查询时有问题



What I have tried:

i have tried with pre computing all prime factors of all numbers in the array , but after that i have problem in processing queries

推荐答案

引用:

但之后我有处理查询时出现问题

but after that i have problem in processing queries



这是最接近问题的部分,你看我们什么都没有。



这是你需要做的事情才能找到解决方案的希望。

- 选择语言。

- 当你举例时,给出输入,实际输出和预期输出。

- 当您提供代码时,请提供足够完整的代码以便我们知道它的作用。包括调用代码。

- 有问题,不工作不提供信息。告诉它它做了什么以及它出了什么问题。



现在,你是唯一能够帮助你的人。



有一个工具可以让你看到你的代码在做什么,它的名字是调试器。掌握调试器不是可选的,它对任何程序员都是强制性的,也不例外。

它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪个期望与现实相符。

当你不明白你的代码在做什么或为什么它做它的作用时,答案是调试器

使用调试器查看你的代码是什么代码正在做。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量,这是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java- application.html [ ^ ]

phpdbg | php调试器 [ ^ ]

PHP程序员的调试技术 [ ^ ]

调试器在这里向您展示您的代码正在做什么,您的任务是比较它应该做什么。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


This is the part that is the closest to a question, and you see we have nothing.

This is what you have to do in order to have hope for a solution.
- Choose the language.
- When you give example, give input, actual output and expected output.
- When you give code, give code complete enough to allow us to know what it does. Include calling code.
- "have problem", "not work" are not informative. Tell what it does and in what it is wrong.

For now, you are the only one able to help you.

There is a tool that allow you to see what your code is doing, its name is debugger. Mastering the debugger is not optional, it is mandatory for any programmer, no exception.
It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
phpdbg | php debugger[^]
Debugging techniques for PHP programmers[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于查询素数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆