快速幂
#include<stdio.h> #include<string> #include<iostream> using namespace std; string s; int mod; long long multy(long long q, long long n) { long long cnt = n; long long base = q; long long ret = 1; while(cnt > 0) { if(cnt & 1) ret = (long long)ret*base%mod; cnt = cnt >> 1; base = (long long)base*base%mod; } return ret; } int main() { int y; long long ans; while(cin>>s) { scanf("%d",&mod); y=0; for(int i=0;i<s.size()&&mod!=1;i++) { y=(y*10+s[i]-'0')%(mod-1); } ans=multy(2,y); printf("%lld\n",ans%mod); } return 0; }