Problem 1752 A^B mod C

Accept: 579    Submit: 2598
Time Limit: 1000 mSec    Memory Limit : 32768 KB

Problem Description

Given A,B,C, You should quickly calculate the result of A^B mod C. (1<=A,B,C<2^63).

Input

There are multiply testcases. Each testcase, there is one line contains three integers A, B and C, separated by a single space.

Output

For each testcase, output an integer, denotes the result of A^B mod C.

Sample Input

3 2 4 2 10 1000

Sample Output

1 24

Source

FZU 2009 Summer Training IV--Number Theory

很多题目,平时提交是对的。但是比赛的时候,数据加强,数字范围变化就错了。
溢出......
 
这个题,两个知识点,很好的题目。
1. a^b%m;
2. a*b%m;
由于这道题,数字的大小是(1<=A,B,C<2^63).
一开始,我们都会想到用__int64 或者 long long
计算的方法也有多种,但是本质上是一样的。
整数的二进制拆分。
在计算的过程中会出现一些问题。
错误的代码:
 
  Source Code
RunID: 508246UserID: 987690183Submit time: -- ::52Language: Visual C++Length: Bytes.Result: Wrong Answer #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std; unsigned __int64 mgml(unsigned __int64 a,unsigned __int64 n,unsigned __int64 m )
{
unsigned __int64 ans=;
a=a%m;
while(n)
{
if(n&)
{
ans=(ans*a)%m;//当数字很大的时候,ans*a就溢出了。
}
n=n>>;
a=(a*a)%m;//这边的也是一样的。
}
return ans;
} int main()
{
unsigned __int64 a,b,c;
while(scanf("%I64u%I64u%I64u",&a,&b,&c)>)
{
printf("%I64u\n",mgml(a,b,c));
}
return ;
}

因此,通过以前的方法就难以解决这个问题,这也是这道题好的地方了。

处理的方法,加一个a*b%m的函数,解决在错误代码里遇到的问题。

AC代码:

 #include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
using namespace std; typedef __int64 LL; LL pow1_sum(LL a,LL b,LL mod)
{
a=a%mod;
b=b%mod;
LL cur=;
while(b)
{
if(b&)
{
cur=cur+a;
if(cur>=mod) cur=cur-mod;
}
a=a<<;
if(a>=mod) a=a-mod;
b=b>>;
}
return cur;
}
LL pow_sum(LL a,LL b,LL mod) //a^b%mod
{
LL cur= ;
a=a%mod;
while(b)
{
if(b&)
{
cur=pow1_sum(cur,a,mod);
}
a=pow1_sum(a,a,mod);
b=b>>;
}
return cur;
}
void solve(LL a,LL b,LL mod)
{
LL result = pow_sum(a,b,mod);
printf("%I64d\n",result);
}
int main()
{
LL a,b,mod;
while(scanf("%I64d%I64d%I64d",&a,&b,&mod)>)
{
solve(a,b,mod);
}
return ;
}

这道题,没有很复杂的算法,但是又容易出错,很值得推荐

福州大学oj 1752 A^B mod C ===>数论的基本功。位运用。五星*****的更多相关文章

  1. [FOJ 1752] A^B mod C

    Problem 1752 A^B mod C Accept: 750    Submit: 3205Time Limit: 1000 mSec    Memory Limit : 32768 KB   ...

  2. 51Nod1123 X^A Mod B 数论 中国剩余定理 原根 BSGS

    原文链接https://www.cnblogs.com/zhouzhendong/p/51Nod1123.html 题目传送门 - 51Nod1123 题意 $T$ 组数据. 给定 $A,B,C$,求 ...

  3. 51Nod1039 N^3 Mod P 数论 原根 BSGS

    原文链接https://www.cnblogs.com/zhouzhendong/p/51Nod1039.html 题目传送门 - 51Nod1039 题意 题解 这题我用求高次剩余的做法,要卡常数. ...

  4. 51Nod1038 X^A Mod P 数论 原根 BSGS

    原文链接https://www.cnblogs.com/zhouzhendong/p/51Nod1038.html 题目传送门 - 51Nod1038 题意 题解 在模质数意义下,求高次剩余,模板题. ...

  5. FZU 1650 1752 a^b mod c

    http://acm.fzu.edu.cn/problem.php?pid=1752 http://acm.fzu.edu.cn/problem.php?pid=1650 给跪了. 我的快速幂会越界. ...

  6. FZU 1752 A^B mod C(快速加、快速幂)

    题目链接: 传送门 A^B mod C Time Limit: 1000MS     Memory Limit: 65536K 思路 快速加和快速幂同时运用,在快速加的时候由于取模耗费不少时间TLE了 ...

  7. 中国剩余定理的应用:猪的安家 ->福州大学 OJ

                                                                     Problem 1402 猪的安家 Accept: 984    Su ...

  8. HDU 4389 X mod f(x)

    题意:求[A,B]内有多少个数,满足x % f(x) == 0. 解法:数位DP.转化为ans = solve(b) - solve(a - 1).设dp[i][sum][mod][r]表示长度为i, ...

  9. HDU - 4389 X mod f(x)(数位dp)

    http://acm.hdu.edu.cn/showproblem.php?pid=4389 题意 为[A,B] 区间内的数能刚好被其位数和整除的数有多少个. 分析 典型的数位dp...比赛时想不出状 ...

随机推荐

  1. 20164317《网络对抗技术》Exp2 后门原理与实践

    1.实验内容 (1)使用netcat获取主机操作Shell,cron启动 (2)使用socat获取主机操作Shell, 任务计划启动 (3)使用MSF meterpreter(或其他软件)生成可执行文 ...

  2. Spring Boot快速搭建Web工程

    先想一下,正常我们想要创建一个web服务,首先需要下载tomcat,创建web工程,配置各种web.xml,引入spring的配置,各种配置文件一顿倒腾.....下载有了spring boot,你创建 ...

  3. Xshell连接不上Linux

    用Xshell连接Linux的时候报错了: Connecting to 192.168.79.147:22...Could not connect to '192.168.79.147' (port ...

  4. css实现带箭头的流程条

    直接上效果图: <ul class="navs"> <li>1</li> <li>2</li> <li>3& ...

  5. 利用Django和装饰器做一个简单的修改密码页面

    view视图代码: from django.shortcuts import render,redirect from django.http import HttpResponse from PIL ...

  6. 如何解决jade标签没有闭合,如input

    最近用jade模板引擎编写html时发现input编译输出为<input>,而我想要的效果为<input/>, 如何解决呢,这时我们可以这样写: input/     ---& ...

  7. 神策Loagent数据收集 windows部署的坑

    部署可以修改bin文件夹下的bat文件.. java改为javaw..无窗口运行 重新启动的时候..要保证上次运行到的日志文件要还在..或者同名文件.. 保证要比之前的文件大些..所以最好是之前的文件 ...

  8. c# 测试方法执行时间

    class Program { static void Main(string[] args) { Console.WriteLine(ActionExtension.Profiler(a, )); ...

  9. 下拉菜单;手风琴;九宫格的Jquery的使用实例

    下拉菜单;手风琴;九宫格的Jquery的使用实例 1.下拉菜单 效果如图: 代码如下: <!DOCTYPE html> <html lang="en"> & ...

  10. java学习--Reflection反射机制

    JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意方法和属性:这种动态获取信息以及动态调用对象方法的功能称为java语言的反射机制. ...