Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 小于1的不同分数的数目_Java_Algorithm_Time_Dynamic Programming - Fatal编程技术网

Java 小于1的不同分数的数目

Java 小于1的不同分数的数目,java,algorithm,time,dynamic-programming,Java,Algorithm,Time,Dynamic Programming,给定一个数N,要求求出不同分数的个数,这样,如果约化分数是p/Q(p和Q是共素数),那么p和Q必须0){ int n=sc.nextInt();//整数n 整数计数=0; for(int i=1;ifor循环很好。我很确定while循环中出现了问题,即您的条件的计算结果始终为true。它可能与->含义暗示而不是(t-->)有关,我相信这正是您想要的 一种更好的方法是实现类似或的东西。您可以使用和动态编程 步骤1:生成最大可能值n的所有素数 步骤2:计算ToClient[i]总的来说1 TLE意味

给定一个数
N
,要求求出不同分数的个数,这样,如果约化分数是p/Q(p和Q是共素数),那么p和Q必须
0){
int n=sc.nextInt();//整数n
整数计数=0;

for(int i=1;ifor循环很好。我很确定while循环中出现了问题,即您的条件的计算结果始终为true。它可能与->含义暗示而不是(t-->)有关,我相信这正是您想要的

一种更好的方法是实现类似或的东西。

您可以使用和动态编程

步骤1:生成最大可能值n的所有素数


步骤2:计算<代码>ToClient[i]
总的来说
1 TLE意味着什么?太少的努力?你可能想要查找Farey序列,例如@subrunner TLE超过了时间限制。第二种方法通常比第一种方法最差。第一种方法对于特定的大数失败,但第二种方法试图做更多的工作来计算所有可能的大数。看起来这个问题应该解决了我有一些数学技巧,可以减少计算量。我在(t-->0)
时使用了
很长一段时间。它以前从未造成过这样的错误(除此之外,之前的解决方案在没有TLE的情况下运行,用于
N=1
2
3
,在原始程序中也有
t-->0
,可能只是嵌套for循环的时间复杂度为O(N^2),并且您为n输入1000000。在第二个程序中,它与while循环有关。您是否可以观看程序的某个版本的运行,然后查看它在哪里被卡住?t-->0似乎没有问题。对此表示抱歉。我在提到n(n)的地方找到了此链接=3*nn/PIPI,其中PI是3.14…所以我写了这篇文章,给出了
1
2
3
的预期结果,但在线评委认为这是错误的答案。你能给我提些更好的建议吗?@yobro97我在我的答案中添加了另一个序列,称为Stern Brocot序列,可能对你更有效。
public static void main (String[] args) throws java.lang.Exception
  {
    // your code goes here
    Scanner sc = new Scanner (System.in);
    int t = sc.nextInt();//number of test cases
    while(t-->0){
        int n = sc.nextInt();//integer N
        int count=0;
        for(int i=1;i<=n;i++){
            for(int j=1;j<i;j++){
                if(gcd(i,j)==1)
                    count++;
            }
        }
        System.out.println(count);
    }
  }
  public static int gcd(int a,int b){
      if(b!=0)
          return gcd(b,a%b);
      else
          return a;
  }
public static void main (String[] args) throws java.lang.Exception
  {
    // your code goes here
    Scanner sc = new Scanner (System.in);
    int[] arr = new int[1000001];
    arr[0]=0;
    for(int i=1;i<1000001;i++)
    {
        arr[i]=arr[i-1];
        for(int j=i-1;j>=0;j--)
        {
            if(gcd(i,j)==1)
                arr[i]++;
        }
    }
    int t = sc.nextInt();
    while(t-->0){
        int n = sc.nextInt();
        System.out.println(arr[n]);
    }
  }
  public static int gcd(int a,int b){
      if(b!=0)
          return gcd(b,a%b);
      else
          return a;
  }