原创

a^b % 1e9+7 を計算しています

温馨提示:
本文最后更新于 2024年04月12日,已超过 47 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我

多くの入力に対して質問 a^b mod 1000000007 を計算しているとき。大きな欠陥は整数オーバーフローです。これを回避した後、一部のモジュラー操作でスタックする可能性があるため、コードがいくつかありますのエッジケースを解決できません。

public class Solution {
    protected static final int mod=(int)(1e9+7);
     public static List<Integer> cal_setBits(int num) {
        List<Integer> positions = new ArrayList<>();
        int position = 0;

        while (num > 0) {
            if ((num & 1) == 1) {
                positions.add(position);
            }
            position++;
            num = num >> 1;
        }

        return positions;
    }
    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        while(t-->0)
        {
            int n1=sc.nextInt();
            int n2=sc.nextInt();
             // using modular exponential
            int arr[]=new int[33];
            // store set-bits
            int count=1;
            int index=1;
             List<Integer> list=cal_setBits(n2);
            arr[0]=n1;
            // int count=1;
            while(count<n2)
            {
                arr[index]=(int)(arr[index-1] *arr[index-1])%mod;
                count*=2;
                index++;
            }
            long res=1;
            for(int i=0;i<list.size();i++)
            {
                res=(long)(res*arr[list.get(i)]*1l)%mod;
            }
                    System.out.println(res);
        }
    }
}

誰かコードの問題なのか説明してもらえませんか。

コードを正しくする

正文到此结束
热门推荐
本文目录