Esempio n. 1
0
void solve(int a, int b, int p)
{
    if (!b) {
        outDate(0);
        return;
    }
    int g = PriRoot(p);
    int x = BSGS(g, b, p);
    LL y, t, d = ext_gcd(a, p-1, y, t);
    if (x == -1 || x%d) {
        puts("Transmission error");
        return;
    }
/*    
    y=(y*(x/d))%(p-1);
    d=(p-1)/d;
    int ti = 0;
    for (y=(y%d+d)%d;y<p-1;y+=d)
        rec[ti++] = powMod(g,y,p);
    d = ti;
    */

y %= (p-1)/d;
    if (y < 0) y += (p-1)/d;
    
    for (int i = 0; i < d; ++i) {
        LL ty = y*(x/d)+(p-1)/d*i;
        rec[i] = powMod(g, ty, p);
    }

    std::sort(rec, rec + d);
    for (int i = 0; i < d; ++i) outDate(rec[i]);
}
Esempio n. 2
0
int main(void)
{
    int test;
    
    calc_com();
    scanf("%d", &test);
    while (test--)
    {
    	LL n,k;
    	scanf("%lld%lld", &n, &k);
        LL ans[MAXK];
		ans[0]=n%mod; ans[1]=n*(n+1)/2%mod;
        for (int i=2; i<=k; i++)
		{
			LL ret = power(n,i+1);
            for (int j=2; j<=i+1; j++)
            {
            	if (j&1) ret=(ret-C[i+1][j]*ans[i+1-j]%mod+mod)%mod;
                else ret=(ret+C[i+1][j]*ans[i+1-j]%mod+mod)%mod;
            }
            LL a=C[i+1][1], b=ret, x, y;
            ext_gcd(a, mod, x, y);
            x=(x%mod+mod)%mod;
            ans[i]=(x*b+mod)%mod;
        }
        printf("%lld\n", ans[k]);
    }
    return 0;
}
Esempio n. 3
0
// find x such that a * x % n = 1, (a, n) = 1
ll mod_inv(ll a, ll n) {
	ll x, y;
	ext_gcd(a, n, x, y);
	x %= n;
	if (x < 0) x += n;
	return x;
}
Esempio n. 4
0
ll inv_mod(ll a, ll m){
	ll x, y;
	ext_gcd(a, m, x, y);
	x %= m;
	if(x < 0) x += m;
	return x;
}
Esempio n. 5
0
ll ext_gcd(ll a, ll b, ll &x, ll &y){
	if(b == 0){
		x = 1, y = 0;
		return a;
	}
	ll d = ext_gcd(b, a % b, y, x);
	y -= a / b * x;
	return d;
}
Esempio n. 6
0
// a * x + b * y = gcd(a, b)
ll ext_gcd(ll a, ll b, ll &x, ll &y) {
	if (!b) {
		x = 1, y = 0;
		return a;
	}	
	ll ret = ext_gcd(b, a%b, x, y);
	ll t = x;
	x = y, y = t - a / b * y;
	return ret;
}
Esempio n. 7
0
File: BSGS.cpp Progetto: chyyuu/ACM
//方程ax + by = c有c / gcd(a, b)个解,且等间距分布
template <class T>T ext_gcd(T a,T b,T& x,T& y){
	T t,ret;
	if (!b){
		x=1,y=0;
		return a;
	}
	ret=ext_gcd(b,a%b,x,y);
	t=x,x=y,y=t-a/b*y;
	return ret;
}
Esempio n. 8
0
LL ext_gcd(LL a, LL b, LL& x, LL& y)
{
    if (!b) {
        x = 1, y = 0;
        return a;
    }
    LL ret = ext_gcd(b, a%b, y, x);
    y -= a/b*x;
    return ret;
}
Esempio n. 9
0
LL ext_gcd(LL a, LL b, LL& x, LL& y)
{
	LL t,ret;
    if (!b)
    {
		x=1,y=0;
        return a;
	}
	ret=ext_gcd(b,a%b,x,y);
	t=x, x=y, y=t-a/b*y;
    return ret;
}
Esempio n. 10
0
static VALUE t_search(VALUE self, VALUE lng, VALUE lat)
{
  double min_dist = 10000000.0;
  double work_dist = 0.0;
  point_data neigh;
  neigh.id = Qnil;
  int i;
  for(i = 0; i < global_dot_count; i++){
    work_dist = ext_gcd(NUM2DBL(lng), NUM2DBL(lat), NUM2DBL(p_dotes[i].lng), NUM2DBL(p_dotes[i].lat));
    if(work_dist < min_dist){
      min_dist = work_dist;
      neigh = p_dotes[i];
    }
  }
  return neigh.id;
}
ll chinese_remain(vector<pair<ll, ll> > &vec)
{
    ll m1, a1, m2, a2;
    ll y, z, d; 
    a1 = vec[0].first;
    m1 = vec[0].second;
    for(int i=1; i<vec.size(); i++)
    {
        a2 = vec[i].first;
        m2 = vec[i].second;
        ext_gcd(m1, m2, d, y, z);
        if((a2 - a1) % d) return -1; //无解
        y = (a2 - a1)/d * y;
        a1 = a1 + m1 * y;
        m1 = m1  / d * m2; 
        a1 = a1 % m1;
    }
    a1 = (a1 % m1 + m1) % m1;
    return a1;
}
Esempio n. 12
0
void solve(int a, int b, int p)
{
    if (!b) {
        outDate(0);
        return;
    }
    int g = PriRoot(p);
    int x = BSGS(g, b, p);
    LL y, t, d = ext_gcd(a, p-1, y, t);
    y %= (p-1)/d;
    if (y < 0) y += (p-1)/d;
//    printf("%d %d %d %d\n", g, x, y, d);
    if (x == -1 || x%d) {
        puts("Transmission error");
        return;
    }
    for (int i = 0; i < d; ++i) {
        LL ty = y*(x/d)+(p-1)/d*i;
        rec[i] = powMod(g, ty, p);
    }
    std::sort(rec, rec + d);
    for (int i = 0; i < d; ++i) outDate(rec[i]);
}
int mul_inverse(int a, int m) {
    int x, y;
    ext_gcd(a, m, x, y);
    return (x + m) % m;
}
Esempio n. 14
0
File: BSGS.cpp Progetto: chyyuu/ACM
int inval(int a,int b,int n){
	int x,y,e;
	ext_gcd(a,n,x,y);
	e=(LL)x*b%n;
	return e<0?e+n:e;
}
Esempio n. 15
0
long long get_ni(long long a,long long p){ //·µ»Øx,ax=1 (mod p) ap»¥ÖÊ
	long long x,y;
	ext_gcd(a,p,x,y);
	return x;
}
Esempio n. 16
0
long long ext_gcd(long long a,long long b,long long &x,long long &y){
	if(b==0){  x=1,y=0;  return a;  }
	long long d=ext_gcd(b,a%b,x,y) , temp=x;
	x=y,y=temp-a/b*y;  return d;
}
Esempio n. 17
0
/*
 * find the inverse of n modular p
 */
ll mod_inverse(ll n, ll p){
	ll x, y;
	ll d = ext_gcd(n, p, x, y);
	return (p + x % p) % p;
}