我一看这么大的数据,哼哼,坑定会超时的呀!但是我好像也没有什么太好的方法,笨笨的以为自己挺聪明的写了两个for循环,没想到……反而直接把数据弄爆了,心累呀!以下是我最初的代码。当一个反面教材吧。
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
int main()
{
ll n,p,a,b=1,c=1;
cin>>n>>p;
for(int i=0;i<n;i++)
{
b*=2;
b%=p;
}
for(int i=0;i<b;i++)//真是当局者迷,这个b的值这时很大呀、
{ //但是当时真的没感觉,还自认为可行,可笑,可笑。
c*=2;
c%=p;
}
cout<<c<<endl;
}
经过借鉴别人的代码之后,我才发现这是我以前遇到的一个问题,但是忘记了,,忘得干干净净的,这也反映了一个事实,就是你写过的代码,还不是你的东西。过段时间就忘的干干净净。看这次能记得多长时间吧。(其实挺简单的思路)
#include<iostream>
using namespace std;
int main()
{
long long n,p,a=2;
cin>>n>>p;
while(n--) a=a*a%p;
cout<<a;
}
看了别人的代码,自己的多还错,跟狗屎一样!