这里放一个大数加法的板子,这是mhr大佬的写法,自己学习了一下,然后存起来怕以后找不到了23333
模板:
#include <bits/stdc++.h>
using namespace std;
int cnt=0;
string qp(int t)
{
string a="3";
for(int i=0;i<t;i++)
{
string ans="";
reverse(a.begin(),a.end());
string b=a;
string c=a;
int jw=0,now=0;
for(int k=0;k<a.size();k++)
{
int t=a[k]-'0'+b[k]-'0'+c[k]-'0'+jw;
jw=t/10;
now=t%10;
ans+=char(now+'0');
}
if(jw)
ans+="1";
reverse(ans.begin(),ans.end());
a=ans;
}
return a;
}
int main()
{
/*ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);*/
//freopen("data.out","w",stdout);
int t;
cin>>t;
for(int i=0;i<t;i++)
{
int n;
cin>>n;
if(n==0)
cout<<1<<"\n";
else
cout<<qp(n-1)<<"\n";
}
}
主要步骤分为这样几步:1.补零 2.倒序 3.模拟加法进位 4.倒序 5.返回string类型的值
大佬博客还整理了n的t次幂的模板,这里来解释一下:先放马大佬的博客:
#include<bits/stdc++.h>
using namespace std;
string ans1 (string a,int n) {
string jk=a;
string jb=a;
reverse(a.begin(),a.end());
for(int i=0;i<n-1;i++) {
for(int j=jk.size();j<a.size();j++)
jk='0'+jk;
reverse(jk.begin(),jk.end());
int jw=0;
int now;
string anss="";
for(int k=0;k<a.size();k++) {
int t=a[k]-'0'+jk[k]-'0'+jw;
jw=t/10;
now=t%10;
anss+=char(now+'0');
}
if(jw)
anss+='1';
a=anss;
jk=jb;
}
reverse(a.begin(),a.end());
return a;
}
string qp(int n,int cs) {
stringstream s;
s<<n;
string a;
s>>a;
string ans;
for(int i=0;i<cs;i++) {
ans=ans1(a,n);
a=ans;
}
return a;
}
int main() {
int t;
cin>>t;
while(t--) {
int n,cs;
cin>>n>>cs;
if(n==0)
cout<<1<<endl;
else
cout<<qp(n,cs-1)<<endl;
}
}
这个代码看的我头晕眼花,看了俩小时,中途还自闭,走到走廊里(因为走廊比较冷,思路清晰)最后打比着打了一遍还调试了半天,最后自己模拟了很多次才明白咋回事的。
下面是详细解释: 先输入t 代表有t组数据,然后输入n和cs 代表n的cs次方 调用第一个qp函数 把传过去的n用stringstream转换一下,变成字符串传到下一个函数(ans1)中,作为初始的值,来进行下面的运算,经过大量的模拟发现 最上面的(ans1函数)的作作用是计算一次平方 :举个例子 要计算3的4次方,在qp函数中第一次调用ans1函数,得到的结果是9,第二次运行结果是27,第三次是81,而从主函数传过来的值是cs-1 所以只运行这三次,就得到了结果
下面解释一下ans1函数的作用原理 :
刚开始传过去一个string类型的a,这个a是字符串类型的,它转换成数字的话和n是一样的,让两个string赋值为a的作用是,每一次计算完以后都要重新把jk(用来计算的字符串)更新一下,成为原本要加的,第二个参数是n,这里的n传过去就是输入的n,因为刚开始已经给两个字符串赋初值了,所以只需要进行n-1次运算即可!!!这里每一次循环的原理就是让n+n一次,所以算2的乘方的时候只需要加一次,因为2的乘方加一次就变成了二倍,而计算3乃至n的乘方的时候,就需要加2次一直到n-1次 这还是很好理解的,这里的for循环里面的具体步骤用的是刚开始那个大数加法的模版思想,最后倒过来返回到qp函数中,等qp函数结束以后再把最后的结果a返回,这样计算出来就是最后的结果了。
呼 终于解释完了
最后附上一道山师的题目,题目是计算2的n次幂,由于只需要加一次,所以就没有用两个函数,一个就足够了!
As we all know, lls is the boss of SDNU ACM Training Team, so many team members may be afraid of him.
But this is not the case, lls is very kindly to us. To show his love to us, he plan to give all of us an “Accepted”. The problem shows as follows:
There are n numbers .Each time you can choose a subset of them(may be empty), and then add them up.Count how many numbers can be represented in this way.
Input
The first line of the input contains an integer T, denoting the number of test cases.
In each test case, there is a single integers n in one line, as described above.
,
Output
For each test case, output one line contains a single integer, denoting the answer.
Sample Input
4
9
7
8
233
Sample Output
512
128
256
13803492693581127574869511724554050904902217944340773110325048447598592
代码:
#include <bits/stdc++.h>
using namespace std;
int cnt=0;
string a;
string qp(int t)
{
string a="2";
for(int i=0;i<t;i++)
{
string ans="";
reverse(a.begin(),a.end());
string b=a;
int jw=0,now=0;
for(int k=0;k<a.size();k++)
{
int t=a[k]-'0'+b[k]-'0'+jw;
jw=t/10;
now=t%10;
ans+=char(now+'0');
}
if(jw)
ans+="1";
reverse(ans.begin(),ans.end());
a=ans;
}
return a;
}
int main()
{
/*ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);*/
//freopen("data.out","w",stdout);
int t;
cin>>t;
for(int i=0;i<t;i++)
{
int n;
cin>>n;
if(n==0)
cout<<1<<"\n";
else
cout<<qp(n-1)<<"\n";
}
}
之后这里可能还会更新大数乘法和除法 ok!