大数低速幂运算模板(c++)+python大数幂
简介
自己从大数加法改过来的模板,低速计算n的t次幂,n,t小于等于100速度能够保证
模板
#include <bits/stdc++.h>
using namespace std;
string cal(string a,int cs)
{
string jk=a;
string jc=a;
reverse(a.begin(),a.end());
for(int k=0;k<cs-1;k++)
{
for(int i=jk.size();i<a.size();i++)
jk='0'+jk;
reverse(jk.begin(),jk.end());
int jw=0,n,now;
string t="";
for(int i=0;i<a.size();i++)
{
n=jk[i]+a[i]-'0'-'0'+jw;
jw=n/10;
now=n%10;
t+=char(now+'0');
}
if(jw)
t+="1";
a=t;
jk=jc;
}
reverse(a.begin(),a.end());
return a;
}
string qp(int cs,int n)
{
stringstream s;
s<<cs;
string a;
s>>a;
string t;
for(int i=0;i<n;i++)
{
t=cal(a,cs);
//cout<<t<<"\n";
a=t;
}
return a;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while(t--)
{
int n,t;
cin>>n>>t;
if(t)
cout<<qp(n,t-1)<<endl;
else
cout<<1<<endl;
}
}
python版本(速度较快)
t=int(input())
for i in range(t):
a,b=map(int,input().split())
print(a**b)
使用
先输入计算的次数,然后每行两个数第一个数是n第二个数是t
大数低速幂运算模板(c++)+python大数幂的更多相关文章
- POJ1026 Cipher(置换的幂运算)
链接:http://poj.org/problem?id=1026 Cipher Time Limit: 1000MS Memory Limit: 10000K Total Submissions ...
- C++大数运算模板
#include<iostream> #include<cstring> #include<cstdio> #include<iomanip> #inc ...
- Modular_exponentiation模幂运算
https://en.wikipedia.org/wiki/Modular_exponentiation 蒙哥马利(Montgomery)幂模运算是快速计算a^b%k的一种算法,是RSA加密算法的核心 ...
- 算数运算符: + - * / //(地板除) %(取余) **(幂运算) / 比较运算符 > < >= <= == !=
# ### python运算符 #(1) 算数运算符: + - * / //(地板除) %(取余) **(幂运算) var1 = 5 var2 = 8 # +res = var1 + var2 pri ...
- Luogu P3390 【模板】矩阵快速幂&&P1939 【模板】矩阵加速(数列)
补一补之前的坑 因为上次关于矩阵的那篇blog写的内容太多太宽泛了,所以这次把一些板子和基本思路理一理 先看这道模板题:P3390 [模板]矩阵快速幂 首先我们知道矩阵乘法满足结合律而不满足交换律的一 ...
- Python3基础 ** 幂运算 // 整除运算
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- 组合数学 - 置换群的幂运算 --- poj CARDS (洗牌机)
CARDS Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 1448 Accepted: 773 Description ...
- 迭代加深搜索 codevs 2541 幂运算
codevs 2541 幂运算 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 从m开始,我们只需要6次运算就可以计算出 ...
- poj 3128 Leonardo's Notebook (置换群的整幂运算)
题意:给你一个置换P,问是否存在一个置换M,使M^2=P 思路:资料参考 <置换群快速幂运算研究与探讨> https://wenku.baidu.com/view/0bff6b1c6bd9 ...
随机推荐
- HDU5806 NanoApe Loves Sequence Ⅱ
NanoApe Loves Sequence Ⅱ Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/131072 K (Ja ...
- ATM网络
ATM是Asynchronous Transfer Mode(ATM)异步传输模式的缩写,是实现B-ISDN的业务的核心技术之一.ATM是以信元为基础的一种分组交换和复用技术
- Web 设计与开发者必须知道的 15 个站点
新闻来源:catswhocode.com公司博客整整一个月没有更新了,最近一段时间,全公司都忙于两件事,为海尔集团做定制,为一个合作伙伴做 OEM,终于有了眉目.工作期间,常用到一些工具与帮助站点,今 ...
- POJ 1330 LCA裸题~
POJ 1330 Description A rooted tree is a well-known data structure in computer science and engineerin ...
- [loj6089]小Y的背包计数问题
https://www.zybuluo.com/ysner/note/1285358 题面 小\(Y\)有一个大小为\(n\)的背包,并且小\(Y\)有\(n\)种物品. 对于第\(i\)种物品,共有 ...
- C#面向过程之冒泡排序
//定义一个数组,准备冒泡排序 ,,-,,,,-,}; //定义一个中间变量 ; //n个数字比较需要进行n-1次比较 ; j < arr.Length - - i; j++) { //每一趟的 ...
- 数学 Codeforces Round #219 (Div. 2) B. Making Sequences is Fun
题目传送门 /* 数学:这题一直WA在13组上,看了数据才知道是计算cost时超long long了 另外不足一个区间的直接计算个数就可以了 */ #include <cstdio> #i ...
- 题解报告:hdu 3501 Calculation 2 (欧拉函数的扩展)
Description Given a positive integer N, your task is to calculate the sum of the positive integers l ...
- 创建 pulic dblink 使不同的用户可以访问dblink
1. system: grant create public database link to ivrsdata; 2.userdate: tnsnames.ora config db connect ...
- data和string类型之间的相互转换
package main; import java.text.SimpleDateFormat;import java.util.Date; import freemarker.core.ParseE ...