zoukankan      html  css  js  c++  java
  • NYOJ 102(高次幂取模)

    1.二分递归:

    使用a*b(mod   n)=(a(mod   n)*b(mod   n))(mod   n)即可。 

    #include<stdio.h>
    /*
    错误
    long long fun(long long a,long long b,long long c)
    {
    	long long temp;
    	if(0==b)
    		return 1;
    	temp=fun(a,b/2,c);
    	if(b&1)
    		return temp*a%c;
    	return temp;
    }
    */
    /*
    AC
    long long fun(long long x,long long y,long long p)
    
    {
    
    	long long t=x;
    
    	long long ans=1;
    
    	while(y)
    
    	{
    
    	if(y&1)
    
    	ans=t*ans%p;
    
    	t=t*t%p;
    
    	y=y>>1;
    	}
    	return (int)ans;
    }
    */
    long long fun(long long a,long long b,long long c)
    {
    	long long temp,ans;
    	if(0==b)
    		return 1;
    	temp=fun(a,b/2,c);
    	ans=temp*temp%c;
    	if(b&1)
    		return ans*a%c;
    	return ans;
    }
    /*
    TLE
    long long fun(long long a,long long b,long long c)
    {
    	long long temp,ans;
    	int i;
    	ans=a%c,temp=1;
    	for(i=0;i<b;i++)
    		temp=(ans*temp)%c;
    	return temp;
    }
    */
    int main()
    {
    	int i,T;int num;
    	long long a,b,c;
    	scanf("%d",&T);
    	while(T--)
    	{
    		scanf("%lld%lld%lld",&a,&b,&c);
    		printf("%lld\n",fun(a,b,c));
    	}
    	return 0;
    }
    

      

    2.s   =   (p   ^   e)   mod   n   =   ((p   mod   n)   ^   e)   mode   n。 

    所以,只需要对p除以n的余数进行e次方的运算,而且每运算一步都进行一次取模就可以了。 

    做密码的程序需要一点数论基础。

  • 相关阅读:
    Visual Studio 中的 .NET Framework 类库
    泛型
    泛型
    事件
    基于事件的异步模式
    使用委托进行异步编程
    使用 IAsyncResult 调用异步方法
    异步编程设计模式
    演练:使用VS2010 C# 创作简单的多线程组件
    [转][MEF插件式开发] 一个简单的例子
  • 原文地址:https://www.cnblogs.com/hxsyl/p/2464959.html
Copyright © 2011-2022 走看看