1600: Big Mod (大数幂取模)
来源:互联网 发布:四级单词一笑而过软件 编辑:程序博客网 时间:2024/05/20 02:51
Calculate
for large values of B, P, and M using an efficient algorithm. (That's right, this problem has a time dependency !!!.)
Input
Three integer values (in the order B, P, M) will be read one number per line. B and P are integers in the range 0 to 2147483647 inclusive. M is an integer in the range 1 to 46340 inclusive.
Output
The result of the computation. A single integer.
Sample Input
3181321717176532374859302938236123
Sample Output
13213195/*
*/
- #include <stdio.h>
- #include <memory>
- int main ()
- {
- int a,b,n;
- while (scanf("%d%d%d",&a,&b,&n)!=EOF)
- {
- bool b2[35];
- memset (b2,0,sizeof(b2));
- int tmp=b,i,d=1;
- for ( i=0; tmp>0 ; i++)
- {
- b2[i]=tmp%2;
- tmp=tmp/2;
- }
- for (;i>=0;i--)
- {
- d=d*d%n;
- int aa=a%n;
- if ( b2[i]==1)
- d=d*aa%n;
- }
- printf("%d/n",d);
- }
- return 0;
- }
方法二:超短代码
#include <cstdio>
int b, p, m, k;
int main()
{
while(scanf("%d%d%d", &b, &p, &m)!=EOF)
{
for(k=1,b=b%m; p ; p/=2,b=b*b%m)
if(p%2) k=k*b%m;
printf("%d\n", k%m);
}
return 0;
}
方法三:递归 时间稍慢
#include <cstdio>
int m;
int bigmod(int a,int b)
{
if(!b)return 1;
int d=bigmod(a,b>>1)%m;
return b&1?(d*d%m*a)%m:(d*d)%m;
}
int main ()
{
int a,b;
while (scanf("%d%d%d",&a,&b,&m)!=EOF)
{
int ans=bigmod(a%m,b)%m;
printf("%d\n",ans);
}
return 0;
}
快速幂取模 最短代码 for(k=1,a=a%m; b ; b>>=1,a=a*a%m) if(b&1) k=k*a%m; 求a^b(mod m) k%m是答案。
- 1600: Big Mod (大数幂取模)
- BIG MOD
- UVa 374 Big Mod (快速幂取模)
- JOJ1600 Big Mod
- JOJ1600:Big Mod
- UVa 374 Big Mod
- UVa 374 - Big Mod
- HDU1212 BIG NUMBER (MOD)
- UVA, 374 Big Mod
- 大数整除(大数mod)
- 1600: Big Mod mod运算的性质, a^n合同与a模m的积
- Big Number-大数运算
- c语言大数mod模板
- uva 374 - Big Mod(整数快速幂)
- Big Integer 大数求模
- 大数加法 big number add
- HDU 1212 Big Number 【大数】
- HDU-1212-Big Number【大数】
- MatSetValues()函数
- ArrayList
- 在MTK平台上扩展的操作字符串的三个函数
- wpf动态生成ListBox 及添加ListBoxItem 并定义样式
- 我用的常见的代码
- 1600: Big Mod (大数幂取模)
- 有十个数按从大到小的顺序存放在一个数组中,输入一个数,要求找出该数是数组中的第几个元素。如果该数不在数组中,则打印出“无此数”
- APK程序获取system权限
- 对话框程序最大化 控件以及字体大小自适应
- MTK文件系统之quota空间使用介绍
- 2300: We Share a Wireless Route(费马点)
- java反射机制
- Video resource
- 捕食者物体跟踪算法:未来的计算机操作界面