generated from sigonasr2/CPlusPlusProjectTemplate
Finished!
Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
This commit is contained in:
parent
fce397b68b
commit
7d8bb3561a
Binary file not shown.
4
main.cpp
4
main.cpp
@ -61,8 +61,8 @@ struct Data{
|
|||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
utils::PowMod(5,117,19);
|
std::cout<<utils::PowMod(5,117,19)<<std::endl;
|
||||||
utils::PowMod(5,117,19);
|
std::cout<<utils::PowMod(98765,1234,123557)<<std::endl;
|
||||||
std::cout<<"Works!"<<std::endl;
|
std::cout<<"Works!"<<std::endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
30
utils.cpp
30
utils.cpp
@ -3,16 +3,34 @@
|
|||||||
|
|
||||||
std::map<internal::PowModStruct,int>utils::mapping;
|
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;
|
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()){
|
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;
|
continue;
|
||||||
}
|
}
|
||||||
std::cout<<std::fmod(std::pow(double(a),double(n)),double(mod))<<std::endl;
|
if(n==1){
|
||||||
utils::mapping[{a,n,mod}]=std::fmod(std::pow(double(a),double(n)),double(mod));
|
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++;
|
shift++;
|
||||||
}
|
}
|
||||||
return 0;
|
return product%mod;
|
||||||
}
|
}
|
6
utils.h
6
utils.h
@ -4,9 +4,9 @@
|
|||||||
|
|
||||||
namespace internal{
|
namespace internal{
|
||||||
struct PowModStruct{
|
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; };
|
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;
|
static std::map<internal::PowModStruct,int>mapping;
|
||||||
public:
|
public:
|
||||||
//a^b % mod
|
//a^b % mod
|
||||||
static int PowMod(int a,int b,int mod);
|
static long PowMod(int a,int b,int mod);
|
||||||
};
|
};
|
Loading…
x
Reference in New Issue
Block a user