zoukankan      html  css  js  c++  java
  • hdu2837数论

    http://acm.hdu.edu.cn/showproblem.php?pid=2837

    // a^b%p=a^(b%phi(p)+phi(p))%p
    
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<bitset>
    #include<iomanip>
    
    using namespace std;
    #define INT long long  
    
    INT euler( int n)
    {
    	INT ret=1,i;
    	for (i=2;i*i<=n;i++)
    		if (n%i==0){
    			n/=i,ret*=i-1;
    			while (n%i==0)
    				n/=i,ret*=i;
    		}
    	if (n>1)
    		ret*=n-1;
    	return ret;
    }
    
    INT Pow( INT a , INT b , INT m )
    {
    	INT ans = 1 ;
    	while( b )
    	{
    		if( b & 1 )
    		{
    			ans = ans * a % m ;
    		} 
    		a *= a ;
    		a %= m ;
    		b >>= 1 ;
    	}
    	return ans % m  ;
    }
    
    INT fun2( int a , int b , int m )
    {
    	INT ans = 1 ;
    	for( int i = 1 ; i <= b ; ++i )
    	{
    		ans *= a ;
    		if( ans >= m )
    			return ans ;
    	}
    	return ans ;
    }
    
    INT fun1( int n , int m )
    {
    	INT phi = euler( m ) ;
    	if( n < 10 )
    		return n ;
    	INT a = fun1( n / 10 , phi ) ;
    	INT b = fun2( n % 10  , a , m ) ;
    	if( b >= m )
    	{
    		INT ans = Pow( n % 10 , a + phi , m ) ;
    		if( ans == 0 )
    			 ans += m ;
    		return ans ;
    	}
    	return b ;  
    }
    
    int main()
    {
    	int Case , n , m , p ;
    	cin >> Case ;
    	while( Case-- )
    	{
    		cin >> n >> m ;
    		INT ans = fun1( n , m ) % m ;
    		cout << ans << endl ;
    	} 
    	return 0 ;
    }


  • 相关阅读:
    程序猿小白博客报道
    UML
    mysql隔离级别相关
    mac使用相关笔记
    git相关
    maven相关
    springboot相关
    guava
    IDEA高效运用技巧
    spring事务相关
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3221581.html
Copyright © 2011-2022 走看看