(a*b)%c 问题
给你两个数__int64 类型的整数 a ,b,c。问你,(a*b)%c的值是多少??我们知道: (a*b)%c = ((a%c)*(b%c))%c 。不过即使这样做,在c很大的情况下,(a%c)*(b%c)还是会越界。下面给出二进制的实现代码:
#include<cstdio>
#include<cstring>
using namespace std; __int64 Pow(__int64 a, __int64 b, __int64 c)
{
a %= c;
b %= c;
__int64 ret = ; //计录(a*b)%c的值。
while(b)
{
if(b&)
ret += a, ret %= c;
a <<= ; a %= c; //a随着b中二进制位数而扩大每次扩大两倍。
b >>= ; //b来缩小两倍 去掉最后一位 由于当前最后一位我们用完了,
}
return ret;
} int main()
{
__int64 a, b, c;
while(~scanf("%I64d%I64d%I64d", &a, &b, &c))
{
printf("%I64d\n", Pow(a, b, c));
}
return ;
}
随机推荐
- hdu Big Number 求一个数的位数
Problem Description In many applications very large integers numbers are required. Some of these app ...
- DataTables获取表单输入框数据
$(document).ready(function() { var table = $('#example').DataTable(); $('button').click(function() { ...
- AI自动寻路
1.首先把游戏场景中的物体设为静态 2.选中Window 中的Navigation ,点击Bake进行场景烘焙 3.在需要寻路的游戏对象上添加 NavMeshAgent组件.调整其AgentSize大 ...
- Android DrawerLayout 点击事情穿透
今天使用DrawerLayout做网易4.4版本侧边栏发现点击DrawerLayout空白部分,下面部分content会获得点击事件.解决方法是在<!-- The navigation draw ...
- cf A. Jeff and Digits
http://codeforces.com/contest/352/problem/A #include <cstdio> #include <cstring> #includ ...
- LeetCode_String to Integer (atoi)
Implement atoi to convert a string to an integer. int atoi (const char * str); Convert string to int ...
- 《Programming WPF》翻译 第6章 4.应用程序全球化
原文:<Programming WPF>翻译 第6章 4.应用程序全球化 如果你打算发布你的应用程序到全球各地,你可能需要为不同地区的用户界面准备不同的版本.至少,这需要解决将文本翻译成适 ...
- IOS回调机制——代理,通知中心以及Block
Xcode5.0正式版 IOS7和Xcode5正式版在昨天正式可以下载.IOS7不多说了,交互设计,界面风格,操作的简化程度都属于比较领先的水平. 这里来说说Xcode5正式版,和以前的Xcode5测 ...
- ios 导航栏和旋屏
1,状态栏(UIStatusBar) http://my.oschina.net/shede333/blog/304560 2,visibleViewController和topViewControl ...
- 用户登陆,退出等基本Action
用户登陆页面user_login.jsp对应action为login.do: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transiti ...