Cod sursa(job #1307306)
| Utilizator | Data | 1 ianuarie 2015 21:42:43 | |
|---|---|---|---|
| Problema | Ridicare la putere in timp logaritmic | Scor | 10 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.47 kb |
#include <fstream>
using std::ifstream;
using std::ofstream;
template <int mod>
int putere(const int e, const int x){
if(x == 0){
return 1; }
else if(x == 1){
return e; }
else if(x % 2 == 0){
const int tmp = putere<mod>(e, x/2);
return (tmp * tmp) % mod; }
else /* if(x % 2 == 1*/ {
return (putere<mod>(e, x-1) * e) % mod; } }
int main(){
ifstream f("lgput.in");
int e = 0, x = 0;
f >> e >> x;
ofstream g("lgput.out");
g << putere<1999999973>(e, x);
return 0; }
