zoukankan      html  css  js  c++  java
  • java实现指数问题

    3^n mod 19

    求n次幂,对19取模

    ================

    (3 * 3) * (3 * 3) * 3

    
    public class A
    {
    	// 分治
    	public static int g(int n)
    	{
    		if(n==0) return 1;
    		if(n==1) return 3;
    		
    		int m = n/2;
    		int a = g(m);
    		if(n%2==0)
    			return a * a % 19;
    		else
    			return a * a * 3 % 19;
    	}
    	
    	// 3的n次幂,对19模,暴力
    	public static int f(int n)
    	{
    		int x = 1;
    		for(int i=0; i<n; i++){
    			x = (x*3)%19;
    		}
    		return x;
    	}
    	
    	public static void main(String[] args)
    	{
    		System.out.println(f(1000000000));
    		System.out.println("-------------------");
    		System.out.println(g(1000000000));
    	}
    }
    
    /*
      数论定理
      
      a = ka * p + a1
      b = kb * p + b1
      
      (a * b) % p === (a1*b1)%p 
    */
    
  • 相关阅读:
    朋友
    Music
    Rnadom Teams
    Bone Collector(01背包)
    Common Subsequence LCS
    Copying Books
    Equal Sum Sets
    Checker Challenge
    棋盘问题
    油田(Oil Deposits)
  • 原文地址:https://www.cnblogs.com/a1439775520/p/13076460.html
Copyright © 2011-2022 走看看