Ejemplo n.º 1
0
static S64 FindFactor(S64 n, U32 seed)
{
	// Pollard's rho algorithm.
	U64 n2 = (S64)n;
	for (; ; )
	{
		if (n2 % 2 == 0)
			return 2;
		if (_prime((S64)n2))
			return (S64)n2;
		U64 a = (U64)XorShift(&seed) << 32;
		a |= (U64)XorShift(&seed);
		U64 y = a % (n2 + 1);
		U64 c = (U64)XorShift(&seed) + 1;
		U64 m = (U64)XorShift(&seed) + 1;
		U64 g;
		U64 r = 1;
		U64 q = 1;
		U64 ys = 0;
		U64 x;
		U64 i;
		do
		{
			x = y;
			for (i = 0; i < r; i++)
				y = (ModMul(y, y, n2) + c) % n2;
			U64 k = 0;
			do
			{
				ys = y;
				U64 min_value = m < r - k ? m : r - k;
				for (i = 0; i < min_value; i++)
				{
					y = (ModMul(y, y, n2) + c) % n2;
					q = ModMul(q, x > y ? x - y : y - x, n2);
				}
				g = _gcd((S64)q, (S64)n2);
				k += m;
			} while (k < r && g <= 1);
			r *= 2;
		} while (g <= 1);
		if (g == n2)
		{
			do
			{
				ys = (ModMul(ys, ys, n2) + c) % n2;
				g = _gcd((S64)(x > ys ? x - ys : ys - x), (S64)n2);
			} while (g <= 1);
		}
		if (g == n2)
			seed++;
		else
			n2 = g;
	}
}
Ejemplo n.º 2
0
Archivo: GCDQ.c Proyecto: da5/C
int _gcd(int a, int b)
{
   while (a != b)
    {
        if (a > b)
        {
            return _gcd(a - b, b);
        }
        else
        {
            return _gcd(a, b - a);
        }
    }
    return a;
}
Ejemplo n.º 3
0
int main(void){ 
    int n;
    int arr[128];
    scanf("%d", &n);
    for(int i = 0 ;i < n;i++) {
        scanf("%d", arr + i);
        if (i) {
            gcd[arr[i]] = _gcd(arr[0], arr[i]);
            printf("%d/%d\n", arr[0] / gcd[arr[i]], arr[i] / gcd[arr[i]]);
        }
    }

    return 0; 
}
Ejemplo n.º 4
0
EXPORT S64 _lcm(S64 a, S64 b)
{
	if (a == 0)
	{
		THROWDBG(b == 0, 0xe9170006);
		return 0;
	}
	if (b == 0)
		return 0;
	if (a < 0)
		a = -a;
	if (b < 0)
		b = -b;
	return a / _gcd(a, b) * b;
}
Ejemplo n.º 5
0
int main(){
    int n, g; 
    scanf("%d %d", &n, &g);
    Metal metal[n];
    for(int i=0;i<n;i++){
        scanf("%d %d", &metal[i].price, &metal[i].quantity);
    }
    qsort(metal,n,sizeof(Metal),cmpM);
    ll suffix_cost[n];
    memset(suffix_cost,0,sizeof(suffix_cost));
    for(int i = n - 2; i >= 0; i -= 1){
        suffix_cost[i] = suffix_cost[i + 1] + metal[i + 1].price;
    }
    ll total_weight = 0;
    for(int i=0;i<n;i++){Metal itr = metal[i];
        total_weight += itr.quantity;
    }
    ll best_ans  = 0;
    ll total_ans = 0;
    for (int i = 0, G = g; i < n; i += 1){
        G -= metal[i].quantity;
        total_ans += 1LL * G * metal[i].price;
    }
    for (int i = 0, G = g; i < n; i += 1){
        G -= metal[i].quantity;
        ll current_ans = total_ans;
        current_ans -= 1LL * G * metal[i].price;
        current_ans += 1LL * suffix_cost[i] * metal[i].quantity;
        current_ans += 1LL * metal[i].price * (g - total_weight + metal[i].quantity);
        if (best_ans < current_ans) {
            best_ans = current_ans;
        }
    }
    ll gcd = _gcd(best_ans, 1LL * n);
    printf("%lld %lld\n", best_ans/gcd, n/gcd);
}
Ejemplo n.º 6
0
uint32_t SWTimer_Store(SWTimer_t *timer, Int_Func func,
            uint32_t time_delay, uint8_t repeat, uint32_t *mr_id) {

    if (time_delay == 0) Show_Error();

    uint32_t slot = _find_best_slot(timer, time_delay);
    uint32_t id = _add_sch(timer, slot);

    if (id == 0) {
        timer->slots[slot].reload = time_delay;
    } else {
        // Find new gcd
        uint32_t old = timer->slots[slot].reload;
        uint32_t gcd = _gcd(old, time_delay);

        // Store updated values
        if (gcd != old) {
            uint32_t factor = old / gcd;
            timer->slots[slot].reload = gcd;
            timer->slots[slot].tc *= factor;
            uint32_t i;
            for (i = 0; i < (timer->slots[slot].length-1); i++) {
                timer->slots[slot].sch[i].reload *= factor;
                timer->slots[slot].sch[i].expect *= factor;
            }
        }
    }

    // Store this function
    time_delay = time_delay / timer->slots[slot].reload;
    _sch_store(&(timer->slots[slot].sch[id]), func, time_delay, 
        timer->slots[slot].tc, repeat);

    if (mr_id != NULL) *mr_id = id;
    return slot;
}
Ejemplo n.º 7
0
ll _gcd(ll u, ll v ){
  return v != 0?_gcd(v, u%v):u;
}
Ejemplo n.º 8
0
  Z GCDLehmer::gcd(Z u, Z v)
  {
    if( u.isNegative() ){
      u.hacerPositivo();
    }
    if( v.isNegative() ){
      v.hacerPositivo();
    }
    if( u.isZero() ){
      return v;
    }
    if( v.isZero() ){
      return u;
    }

    // "u" ha de ser >= que "v"
    if( u < v ){
      std::swap(u,v);
    }

    Digit uHat;
    Digit vHat;
    SignedDigit A,B,C,D,T;
    Digit q,q2;
    Digit THat;
    Z t;
    Z w;
    Digit uPrima;
    Digit vPrima;
    Digit uPrimaSegunda;
    Digit vPrimaSegunda;


    while(true){
      if( v.longitud() == 1){
        return _gcd(u,v[0]);
      }
      else{
        const int p = u.getBitLength() - Constants::BITS_EN_CIFRA;

        uHat = u.getBitsInADigit(p);
        vHat = v.getBitsInADigit(p);


        A=1; B=0; C=0; D=1;

        while(true){
          uPrima = uHat + B;
          vPrima = vHat + D; /* OF! */ bool vPrimaOF = false;
          if( vPrima < vHat ){
            vPrimaOF = true;
          }

          uPrimaSegunda = uHat + A; /* OF! */ bool uPrimaSegundaOF = false;
          if( uPrima < uHat )  {
            uPrimaSegundaOF = true;
          }

          vPrimaSegunda = vHat + C;

          if( vPrima == 0 || vPrimaSegunda == 0){
            break;
          }

          if ( vPrimaOF ){
            q = 0; // el hecho de que el divisor sea mayor que la base 
                  //(en la que SI esta dividendo) implica necesariamente 
                  //que el floor del cociente es 0 
          }
          else{
            q = uPrima / vPrima;
          }

          if( uPrimaSegundaOF ){
            if( vPrimaSegunda == 1 ){
              break; 
              // ya que el cociente uPrimaSegunda / vPrimaSegunda 
              //seria igual a la base y dado que uPrima / vPrima es 
              //siempre < base, es imposible que sean  iguales 
            }
            else{
              Z uZ = Z(Constants::DIGIT_MAX); uZ++;
              q2 = (uZ/vPrimaSegunda)[0]; /* el resultado de esa div sera siempre < base
                                             ya que uZ == base y vPrimaSegunda > 1 */
            }
          }
          else{
            q2 = uPrimaSegunda / vPrimaSegunda;
          }

          if( q != q2 ){
            break;
          }
          else{
            T = A - q*C;
            A = C;
            C = T;

            T = B - q*D;
            B = D;
            D = T;

            THat = uHat - q*vHat;
            uHat = vHat;
            vHat = THat;
          }
        }
        // q != q2
        if( B == 0 ){
          t = u % v;
          u = v;
          v = t;
        }
        else{
          t = A*u;
          t += B*v;

          w = C*u;
          w += D*v;

          u = t;
          v = w;
        }
      }
    }
  }
Ejemplo n.º 9
0
/* least common multiple */
static unsigned
_lcm(unsigned a, unsigned b)
{
  return (((unsigned long)a * b) / _gcd(a, b));
}