更高效率的A^B mod C
bird
#include <stdio.h>
#define LL unsigned long long int
inline LL mod(LL a,LL b)
{
while (a>=b)
a-=b;
return a;
}
//a*b mod c
inline LL MulAndMod(LL a, LL shl_b,LL c)
{
LL val,pre;
pre = mod(a,c);
val = 0;
while (shl_b)
{
if (shl_b&0x1)
val = mod(val + pre,c);
shl_b>>=1;
pre = mod(pre<<1,c);
}
return val;
}
inline LL A_BModC(LL a,LL shl_b,LL c)
{
LL val,pre;
if (shl_b&0x1) //根据2相应二进制位的值判断是否加A*2^n;\\因为有对b进行右移运算,所以每次只需判断最末位的结果就可以。
val = mod(a,c);
else
val = 1;
shl_b >>= 1; //计算 A*2^n的值。
pre = MulAndMod(a,a,c);
while (shl_b)
{
if (shl_b&0x1)
val = MulAndMod(val,pre,c);
shl_b>>=1;
pre = MulAndMod(pre,pre,c);
}
return val;
}
int main()
{
LL a,b,c;
while (scanf("%llu%llu%llu", &a,&b,&c)!=EOF)
printf("%llu\n", A_BModC(a,b,c));
return 0;
}
相关文档:
<!--
/* Font Definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-alt:SimSun;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
@font-face
{font-family:"\@宋体" ......
/*
coder: ACboy
date: 2010-3-14
result: 1A
description: UVa 327 Evaluating Simple C Expressions
*/
#include <iostream>
#include <algorithm>
using namespace std;
struct Node {
char name;
int value;
int lastValue;
int pos;
};
int cmp(const Node & a, const Node &a ......
C的变参问题与print函数的实现
我们在C语言编程中会遇到一些参数个数可变的函数,例如printf() 这个函数,它的定义是这样的: int printf( const char* format, ...);
它除了有一个参数format固定以外,后面跟的参数的个数和类型是可变的,例如我们可以有以下不同的调用方法:
printf("%d",i);
&nb ......
1、C/C++程序员请注意,不能在case语句不为空时“向下执行”。
2、值类型和引用类型之间的区别:C#的基本类型(int,char等)都是值类型,是在栈中创建的。而对象是引用类型,创建于堆中,需要使用关键字new。
3、在C#中通过实例访问静态方法或成员变量是不合法的,会生成编译器错误。但是我们可以通过声 ......
opendir(打开目录)
相关函数
open,readdir,closedir,rewinddir,seekdir,telldir,scandir
表头文件
#include<sys/types.h>
#include<dirent.h>
定义函数
DIR * opendir(const char * name);
函数说明
opendir()用来打开参数name指定的目录,并返回DIR*形态的目录流,和open()类似,接下 ......