Last active
January 18, 2016 20:43
-
-
Save andreuinyu/862ee6714849580702d5 to your computer and use it in GitHub Desktop.
Llei de Grup Modular en C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
using namespace std; | |
struct punt{ | |
int x; | |
int y; | |
int z; | |
}; | |
int mcd(int a, int b){ | |
if (b==0) return a; | |
if (a==0) return b; | |
else return mcd(b, a%b); | |
} | |
vector<int> euclid(int x, int y){ | |
int g=mcd(x, y); | |
for (int a=-y/g; a < y/g; ++a){ | |
for (int b=-x/g; b < x/g; ++b){ | |
if (a*x+b*y==g){ | |
vector<int> v(3); | |
v[0]=a; | |
v[1]=b; | |
v[2]=g; | |
return v; | |
} | |
} | |
} | |
} | |
punt add(punt P, punt Q, int A, int N){ | |
punt R; | |
R.x=0; | |
R.y=0; | |
R.z=0; //punt a l'infinit | |
int num; | |
int den; | |
if ((P.x == Q.x and P.y != Q.y) or (P.y == 0 and Q == P)){ | |
R.z=1; | |
return R; | |
} | |
if (P != Q){ | |
num=(Q.y-P.y) % N; | |
den=(Q.x-P.x) % N; | |
} | |
if (P == Q){ | |
num=(3*P.x*P.x+A) % N; | |
den=(2*P.y) % N; | |
} | |
vector<int> V(3); | |
V=euclid(den, N); | |
int invden=V[0]; | |
int g=V[2]; | |
int m=num*invden; | |
R.x=(m*m-P.x-Q.x) % N; | |
R.y=-(m*(P.x-R-x)-P.y) % N; | |
return R; | |
} | |
int main(){ | |
punt P; | |
P.x=0; | |
P.y=1; | |
P.z=0; | |
punt Q; | |
Q.x=7; | |
Q.y=16; | |
Q.z=0; | |
cout << add(P, Q, 17, 23) << endl;; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment