osipyonok

polynomial gcd

Jun 2nd, 2017
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.04 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. #define INF 1000010000
  4. #define nl '\n'
  5. #define pb push_back
  6. #define ppb pop_back
  7. #define mp make_pair
  8. #define fi first
  9. #define se second
  10. #define pii pair<int,int>
  11. #define pdd pair<double,double>
  12. #define all(c) (c).begin(), (c).end()
  13. #define SORT(c) sort(all(c))
  14. #define sz(c) (c).size()
  15. #define rep(i,n) for( int i = 0; i < n; ++i )
  16. #define repi(i,n) for( int i = 1 ; i <= n; ++i )
  17. #define repn(i,n) for( int i = n - 1 ; i >= 0 ; --i )
  18. #define repf(j,i,n) for( int j = i ; j < n ; ++j )
  19. #define die(s) {std::cout << s << nl;}
  20. #define dier(s) {std::cout << s; return 0;}
  21. #define vi vector<int>
  22. typedef long long ll;
  23.  
  24. using namespace std;
  25.  
  26. /*
  27.  
  28. INPUT FORMAT
  29.  
  30. p (you need to find gcd(a , b) in Zp)
  31. n1 - degree of the first polynom
  32. a_n1 ... a_0 - coefficients of a
  33. n2 - degree of the second polynom
  34. a_n2 ... a_0 - coefficients of b
  35.  
  36. EXAMPLE
  37. You need to find gcd of 1x^3  +  -2x^2  +  1x^1  +  -2x^0
  38. and 1x^4  +  -2x^3  +  0x^2  +  7x^1  +  -14x^0 in Zp
  39.  
  40. input:
  41.  
  42. 2
  43. 3
  44. 1 -2 1 -2
  45. 4
  46. 1 -2 0 7 -14
  47.  
  48.  
  49. */
  50.  
  51.  
  52.  
  53.  
  54. int mod = 2;
  55.  
  56.  
  57. int modInverse(int a, int m){
  58.     int m0 = m, t, q;
  59.     int x0 = 0, x1 = 1;
  60.  
  61.     if(m == 1)
  62.         return 0;
  63.  
  64.     while(a > 1){
  65.         q = a / m;
  66.         t = m;
  67.  
  68.         m = a % m, a = t;
  69.         t = x0;
  70.         x0 = x1 - q * x0;
  71.         x1 = t;
  72.     }
  73.  
  74.     while(x1 < 0)
  75.        x1 += m0;
  76.     return x1;
  77. }
  78.  
  79. struct polynom{
  80.     int deg;
  81.     vi a;
  82.     polynom(){}
  83.    
  84.     polynom(int n , vi v){
  85.         deg = n + 1;
  86.  
  87.         a = v;
  88.     }
  89.    
  90.     polynom operator -(const polynom & other){
  91.         polynom res;
  92.         res.deg = max(deg , other.deg);
  93.         vi v;
  94.         rep(i , res.deg){
  95.             int x = deg - i - 1;
  96.             int y = other.deg - i - 1;
  97.             int cur_x = x >= 0 ? a[x] : 0;
  98.             int cur_y = y >= 0 ? other.a[y] : 0;
  99.             v.pb(cur_x - cur_y);
  100.         }
  101.         while(sz(v) && v[sz(v) - 1] == 0)v.ppb();
  102.         if(sz(v) == 0)v = {0};
  103.         res.deg = sz(v);
  104.         reverse(all(v));
  105.         res.a  = v;
  106.         return res;
  107.     }
  108.    
  109.     polynom operator %(const polynom & other){
  110.         polynom res;
  111.         if(other.deg > deg)
  112.             return *this;
  113.            
  114.         polynom tmp = *this;
  115.        
  116.         while(tmp.deg >= other.deg){
  117.             int cur = tmp.deg - other.deg;
  118.             int k = (tmp.a[0] * modInverse(other.a[0] , mod)) % mod;
  119.             vi tmpa(tmp.deg);
  120.             rep(i , tmp.deg){
  121.                 tmpa[i] = i < other.deg ? k * other.a[i] : 0;
  122.             }
  123.             polynom ct(tmp.deg - 1 , tmpa);
  124.             tmp = tmp - ct;
  125.            
  126.         //  tmp.println();
  127.            
  128.             rep(i , tmp.a.size()){
  129.                 while(tmp.a[i] < 0)
  130.                     tmp.a[i] += mod;
  131.                 tmp.a[i] %= mod;
  132.             }
  133.            
  134.             reverse(all(tmp.a));
  135.             while(sz(tmp.a) && tmp.a[sz(tmp.a) - 1] == 0){
  136.                 tmp.a.ppb();
  137.             }
  138.             if(sz(tmp.a) == 0)tmp.a = {0};
  139.             tmp.deg = sz(tmp.a);
  140.             reverse(all(tmp.a));
  141.         }
  142.        
  143.         return tmp;
  144.     }
  145.    
  146.     void field(){
  147.         rep(i , sz(a)){
  148.             while(a[i] < 0)
  149.                 a[i] += mod;
  150.             a[i] %= mod;
  151.         }
  152.     }
  153.    
  154.     void println(){
  155.         rep(i , deg){
  156.             if(i != 0)
  157.                 cout << "  +  ";
  158.             cout << a[i] << "x^" << deg - i - 1;
  159.         }
  160.         cout << nl;
  161.     }
  162. };
  163.  
  164. vector<pair<polynom , polynom>> st;
  165.  
  166.  
  167.  
  168. polynom gcd(polynom a , polynom b){
  169.     a.field();
  170.     b.field();
  171.     st.pb({a , b});
  172.     if(b.deg == 1){
  173.         if(b.a[0] != 0){
  174.             return polynom(0 , {1});
  175.         }
  176.         return a;
  177.     }else{
  178.         return gcd(b , a % b);
  179.     }
  180. }
  181.  
  182.  
  183. int main() {
  184.     ios_base::sync_with_stdio(false);
  185.     cin.tie(NULL);
  186.     cout.precision(0);
  187.     cin >> mod;
  188.     int n1 , n2;
  189.     cin >> n1;
  190.    
  191.     vi a(n1 + 1);
  192.    
  193.     rep(i , n1 + 1){
  194.         cin >> a[i];
  195.     }
  196.    
  197.     cin >> n2;
  198.    
  199.     vi b(n2 + 1);
  200.    
  201.     rep(i , n2 + 1){
  202.         cin >> b[i];
  203.     }
  204.    
  205.  
  206.     polynom pa(n1 , a);
  207.     polynom pb(n2 , b);
  208.    
  209.     cout << "Your input\n";
  210.     cout << "a :  ";
  211.     pa.println();
  212.    
  213.     cout << "b :  ";
  214.     pb.println();
  215.     cout << "Which equals to the following in Z" << mod << nl;
  216.    
  217.     pa.field();
  218.     pb.field();
  219.    
  220.     cout << "a :  ";
  221.     pa.println();
  222.    
  223.     cout << "b :  ";
  224.     pb.println();
  225.    
  226.    
  227.     auto m = gcd(pb , pa);
  228.    
  229. //  m.println();
  230.  
  231.     cout << "Euclid: \n";
  232.    
  233.     rep(i , sz(st)){
  234.         cout << "Step " << i + 1 << nl;
  235.         cout << "a :  ";
  236.         st[i].fi.println();
  237.         cout << "b :  ";
  238.         st[i].se.println();
  239.         cout << nl;
  240.     }
  241.    
  242.     cout << "Thus, GCD(a , b) = ";
  243.     m.println();
  244.  
  245.     return 0;
  246. }
Advertisement
Add Comment
Please, Sign In to add comment