Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
master
Nic0Nic0Nii 1 year ago
parent fce397b68b
commit 7d8bb3561a
  1. BIN
      C++ProjectTemplate
  2. 4
      main.cpp
  3. 30
      utils.cpp
  4. 6
      utils.h

Binary file not shown.

@ -61,8 +61,8 @@ struct Data{
int main()
{
utils::PowMod(5,117,19);
utils::PowMod(5,117,19);
std::cout<<utils::PowMod(5,117,19)<<std::endl;
std::cout<<utils::PowMod(98765,1234,123557)<<std::endl;
std::cout<<"Works!"<<std::endl;
return 0;
}

@ -3,16 +3,34 @@
std::map<internal::PowModStruct,int>utils::mapping;
int utils::PowMod(int a,int b, int mod){
long utils::PowMod(int a,int b, int mod){
int shift=0;
for(int n=1;n<=b;n*=2){
long product=0;
for(long n=1;n<=b;n*=2){
if(utils::mapping.find({a,n,mod})!=utils::mapping.end()){
std::cout<<internal::PowModStruct{a,n,mod}<<"="<<utils::mapping[{a,n,mod}]<<std::endl;
if(b&n){
if(product==0){
product=1;
}
std::cout<<utils::mapping[{a,n,mod}]<<std::endl;
product*=utils::mapping[{a,n,mod}];
product%=mod;
}
continue;
}
std::cout<<std::fmod(std::pow(double(a),double(n)),double(mod))<<std::endl;
utils::mapping[{a,n,mod}]=std::fmod(std::pow(double(a),double(n)),double(mod));
if(n==1){
utils::mapping[{a,n,mod}]=a%mod;
}else{
utils::mapping[{a,n,mod}]=long(utils::mapping[{a,n/2,mod}])*utils::mapping[{a,n/2,mod}]%mod;
}
if(b&n){
if(product==0){
product=1;
}
product*=utils::mapping[{a,n,mod}];
product%=mod;
}
shift++;
}
return 0;
return product%mod;
}

@ -4,9 +4,9 @@
namespace internal{
struct PowModStruct{
int a,b,mod;
long a,b,mod;
friend std::ostream& operator << (std::ostream& os, const internal::PowModStruct& rhs) { os << rhs.a << "^" << rhs.b << "%" << rhs.mod; return os; };
bool operator<(const internal::PowModStruct&rhs)const{return a<rhs.a||b<rhs.b||mod<rhs.mod;};
bool operator<(const internal::PowModStruct&rhs)const{return a<rhs.a||a==rhs.a&&b<rhs.b||a==rhs.a&&b==rhs.b&&mod<rhs.mod;};
};
}
@ -16,5 +16,5 @@ class utils{
static std::map<internal::PowModStruct,int>mapping;
public:
//a^b % mod
static int PowMod(int a,int b,int mod);
static long PowMod(int a,int b,int mod);
};
Loading…
Cancel
Save