亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

大輸入在 biginteger 中的超時

大輸入在 biginteger 中的超時

陪伴而非守候 2022-09-01 19:52:57
考慮一下寫在紙上的數字1到n的排列。讓我們將其元素的乘積表示為 p,將其元素的總和表示為 s。給定一個正整數 n,您的任務是確定 p 是否可被 s 整除。我嘗試使用bigInteger概念,但在50個測試用例中,有30個成功通過,但其余的都顯示超時,這可能是因為遞歸。import java.util.*;import java.math.BigInteger;public class TestClass {    static BigInteger factorial(int n){        if(n==0||n==1)            return new BigInteger("1");        return BigInteger.valueOf(n).multiply(factorial(n-1));    }    public static void main(String args[] ) throws Exception {        Scanner s=new Scanner(System.in);        int n=s.nextInt();        int nn=n*(n+1)/2;        BigInteger sum=BigInteger.valueOf(nn);        BigInteger p=factorial(n);            if((p.mod(sum)).equals(BigInteger.valueOf(0)))            System.out.println("YES");        else            System.out.println("NO");   }}對于示例測試用例,輸入為 3,其輸出應為“YES”,因為 (1+2+3) 除以 (1*2*3)。
查看完整描述

3 回答

?
莫回無

TA貢獻1865條經驗 獲得超7個贊

嘗試刪除遞歸并使用 for 循環計算階乘。


import java.util.*;

import java.math.BigInteger;

public class TestClass {

static void factorial(long n, long nn){


    BigInteger answer=new BigInteger("1");

    BigInteger sum=BigInteger.valueOf(nn);

    int foundMatch =0;

    for(long i=n;i>0;i--){

        answer=answer.multiply(new BigInteger(String.valueOf(i)));

        if((answer.mod(sum)).equals(BigInteger.valueOf(0)))

        {

            System.out.println("YES");

            foundMatch = 1;

            break;

        }

    }

    if(foundMatch!=1)

    System.out.println("NO");

}


public static void main(String args[] ) throws Exception {

    Scanner s=new Scanner(System.in);

    long n=s.nextLong();

    long nn=n*(n+1)/2;


    factorial(n, nn);    

}


}


查看完整回答
反對 回復 2022-09-01
?
LEATH

TA貢獻1936條經驗 獲得超7個贊

import java.util.*;

class TestClass {

    public static int prime(int n)

    {

        int count=0,flag=0;

        if(n==1)

        flag=1;

        if(n==2)

        flag=0;

        else {

        for(int i=2;i<=Math.sqrt(n);i++) {

            if(n%i==0)

            {

                flag=1;

                break;

            }

        }}

        if(flag==1)

        return 1;

        return 0;


    }

    public static void main(String args[] ) throws Exception {

        Scanner s=new Scanner(System.in);

        int t=s.nextInt();

        while(t-->0)

        {

            int flag=0;

            int n=s.nextInt();

            if(n%2==0)

            {

             flag=prime(n+1);   

            }

            else

            {

                flag=1;

            }

        if(flag==1)

        System.out.println("YES");

        else

        System.out.println("NO");

        }

    }

}



查看完整回答
反對 回復 2022-09-01
?
largeQ

TA貢獻2039條經驗 獲得超8個贊

您可以使用這樣的邏輯:如果階乘的中間乘積可被總和整除,則整個階乘也可以被和整除。


import java.math.BigInteger;

import java.util.Scanner;


public class TestClass {

static boolean isFactorialDivisible(int n, BigInteger sum) {

    BigInteger p = BigInteger.ONE;

    for (int i = n; i > 0; i--) {

        p = p.multiply(BigInteger.valueOf(n));

        if (p.mod(sum).equals(BigInteger.ZERO)) {

             return true;

        }

        BigInteger gcd = p.gcd(sum);

        if (!gcd.equals(BigInteger.ONE)) {

            p = p.divide(gcd);

            sum = sum.divide(gcd);

        }

    }

    return false;

}


public static void main(String args[]) throws Exception {

    Scanner s = new Scanner(System.in);

    int n = s.nextInt();

    int nn = n * (n + 1) / 2;

    BigInteger sum = BigInteger.valueOf(nn);

    boolean p = isFactorialDivisible(n, sum);

    if (p)

    System.out.println("YES");

    else

    System.out.println("NO");

}

}


查看完整回答
反對 回復 2022-09-01
  • 3 回答
  • 0 關注
  • 159 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號