int main() { for ( unsigned int i = 2; i <= 10; ++i ) { nacci_t nacci( i ); // fibonacci sequence std::cout << "nacci( " << i << " ): "; for ( int j = 0; j < 10; ++j ) std::cout << " " << nacci(); std::cout << std::endl; } for ( unsigned int i = 2; i <= 10; ++i ) { nacci_t lucas( i, 2, 1 ); // Lucas sequence std::cout << "lucas( " << i << " ): "; for ( int j = 0; j < 10; ++j ) std::cout << " " << lucas(); std::cout << std::endl; } }
void test_lehmer_thm(void) { BIGNUM *v = BN_new(), *v2 = BN_new(), *h = BN_new(), *n = BN_new(), *p = BN_new(), *q = BN_new(), *g = BN_new(); BN_CTX *ctx = BN_CTX_new(); BN_dec2bn(&v, "2"); BN_dec2bn(&p, "181857351165158586099319592412492032999818333818932850952491024" "131283899677766672100915923041329384157985577418702469610834914" "6296393743554494871840505599"); BN_dec2bn(&q, "220481921324130321200060036818685031159071785249502660004347524" "831733577485433929892260897846567483448177204481081755191897197" "38283711758138566145322943999"); BN_mul(n, p, q, ctx); /* p + 1 */ BN_dec2bn(&h, "181857351165158586099319592412492032999818333818932850952491024" "131283899677766672100915923041329384157985577418702469610834914" "6296393743554494871840505600"); lucas(v, h, n, ctx); BN_sub(v2, v, BN_value_two()); BN_gcd(g, v2, n, ctx); assert(!BN_is_one(g)); /* another test */ BN_dec2bn(&v, "3"); BN_dec2bn(&p, "181857351165158586099319592412492032999818333818932850952491024" "131283899677766672100915923041329384157985577418702469610834914" "62963937435544948718405055999"); BN_generate_prime(q, 512, 1, NULL, NULL, NULL, NULL); BN_mul(n, p, q, ctx); BN_sub(h, p, BN_value_one()); BN_mul(h, h, BN_value_two(), ctx); lucas(v, h, n, ctx); BN_mod_sub(v2, v, BN_value_two(), n, ctx); BN_gcd(g, v2, n, ctx); assert(!BN_is_one(g)); assert(BN_cmp(g, n)); BN_free(q); BN_free(p); BN_free(v); BN_free(v2); BN_free(h); BN_CTX_free(ctx); }
void test_lucas(void) { BIGNUM *v = BN_new(), *h = BN_new(), *n = BN_new(), *vcheck = BN_new(); BN_CTX *ctx = BN_CTX_new(); BN_dec2bn(&v, "5"); BN_dec2bn(&n, "100000000000"); /* <V₃, V₂> */ BN_dec2bn(&h, "3"); BN_dec2bn(&vcheck, "110"); lucas(v, h, n, ctx); assert(!BN_cmp(vcheck, v)); /* <V₆, V₅> */ BN_dec2bn(&h, "2"); BN_dec2bn(&vcheck, "12098"); lucas(v, h, n, ctx); assert(!BN_cmp(vcheck, v)); /* another sequence */ BN_dec2bn(&v, "5"); BN_dec2bn(&n, "100"); BN_dec2bn(&h, "11"); BN_dec2bn(&vcheck, "45"); lucas(v, h, n, ctx); assert(!BN_cmp(v, vcheck)); BN_dec2bn(&h, "9"); BN_dec2bn(&vcheck, "30"); lucas(v, h, n, ctx); assert(!BN_cmp(v, vcheck)); BN_dec2bn(&h, "3"); BN_dec2bn(&vcheck, "10"); lucas(v, h, n, ctx); assert(!BN_cmp(v, vcheck)); BN_free(v); BN_free(h); BN_free(vcheck); BN_CTX_free(ctx); }
int main(int argc, char const *argv[]) { /* while(~scanf("%I64d%I64d",&N ,&M)){ printf("%I64d\n", lucas(N, M)); } */ int T; scanf("%d",&T); while(T--){ scanf("%I64d%I64d%I64d",&N,&M,&mod); printf("%I64d\n", lucas(N+M,M)); } return 0; }
int main(void){ long long int n,k; int i; step[0]=1; for(i=1;i<mod;i++){ step[i]=(step[i-1]*i)%mod; } inv[1] = 1; for (i = 2; i < mod; i++) inv[i] = inv[mod % i] * (mod - mod / i) % mod; while(scanf("%lld%lld",&n,&k)!=EOF){ //(2n,n-k) long long int a=2*n; long long int b=n-k; printf("%lld\n",lucas(a,b)); } return 0; }
//@ Main Function int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); int _, __ = 1; for(std::cin >> _; _; --_, ++__) { //std::cout << "Case #" << __ << ": "; int n, m, p; cin >> n >> m >> p; fac[0] = 1; rep(i, 1, p) fac[i] = fac[i-1] * i % p; inv[p-1] = csl::pow(ll(1), fac[p-1], p-2, ll(p)); per(i, 1, p) inv[i-1] = inv[i] * i % p; cout << lucas(n + m, m, p) << endl; } return 0; }
/******************************************* C(n, k) mod p^t generalized Lucas's theorem for combination mod p O(p^t * lgn/lgp), p^2t < typecMAX *******************************************/ typec generalizedLucas(typec n, typec k, typec p, typec t) { if(k > n) return 0; if(n - k < k) k = n - k; if(t == 1) return lucas(n, k, p); typec pt = power(p, t); typec c = 1, pcnt = 0, ktable[100], ntable[100], ltable[100]; int cnt = 0; for(; n || k; cnt++) { ktable[cnt] = k, ntable[cnt] = n, ltable[cnt] = k % pt; n -= k % pt, k -= k % pt, n /= p, k /= p; } for(--cnt; c && cnt >= 0; cnt--) productQuotient(ntable[cnt], ktable[cnt], ltable[cnt], p, pt, c, pcnt); if(!c || pcnt >= t) return 0; return c * power(p, pcnt) % pt; }
i64 lucas(i64 n , i64 m){ if( m == 0) return 1; return get_cm(n%mod, m%mod) * lucas(n/mod, m/mod) % mod; }
int main() { /* factoring program using Williams (p+1) method */ int k,phase,m,nt,iv,pos,btch; long i,p,pa,interval; big b,q,n,fp,fvw,fd,fn,t; static big fu[1+MULT/2]; static BOOL cp[1+MULT/2]; mip=mirsys(30,0); b=mirvar(0); q=mirvar(0); n=mirvar(0); t=mirvar(0); fp=mirvar(0); fvw=mirvar(0); fd=mirvar(0); fn=mirvar(0); gprime(LIMIT1); for (m=1;m<=MULT/2;m+=2) if (igcd(MULT,m)==1) { fu[m]=mirvar(0); cp[m]=TRUE; } else cp[m]=FALSE; printf("input number to be factored\n"); cinnum(n,stdin); if (isprime(n)) { printf("this number is prime!\n"); return 0; } for (nt=0,k=3;k<10;k++) { /* try more than once for p+1 condition (may be p-1) */ convert(k,b); /* try b=3,4,5.. */ convert((k*k-4),t); if (egcd(t,n,t)!=1) continue; /* check (b*b-4,n)!=0 */ nt++; phase=1; p=0; btch=50; i=0; printf("phase 1 - trying all primes less than %d\n",LIMIT1); printf("prime= %8ld",p); forever { /* main loop */ if (phase==1) { /* looking for all factors of p+1 < LIMIT1 */ p=mip->PRIMES[i]; if (mip->PRIMES[i+1]==0) { /* now change gear */ phase=2; printf("\nphase 2 - trying last prime less than %ld\n" ,LIMIT2); printf("prime= %8ld",p); copy(b,fu[1]); copy(b,fp); mad(b,b,b,n,n,fd); decr(fd,2,fd); negify(b,t); mad(fd,b,t,n,n,fn); for (m=5;m<=MULT/2;m+=2) { /* store fu[m] = Vm(b) */ negify(fp,t); mad(fn,fd,t,n,n,t); copy(fn,fp); copy(t,fn); if (!cp[m]) continue; copy(t,fu[m]); } convert(MULT,t); lucas(b,t,n,fp,fd); iv=(int)(p/MULT); if (p%MULT>MULT/2) iv++; interval=(long)iv*MULT; p=interval+1; convert(iv,t); lucas(fd,t,n,fp,fvw); negify(fp,fp); subtract(fvw,fu[p%MULT],q); marks(interval); btch*=100; i++; continue; } pa=p; while ((LIMIT1/p) > pa) pa*=p; convert((int)pa,t); lucas(b,t,n,fp,q); copy(q,b); decr(q,2,q); } else { /* phase 2 - looking for last large prime factor of (p+1) */ p+=2; pos=(int)(p%MULT); if (pos>MULT/2) { /* increment giant step */ iv++; interval=(long)iv*MULT; p=interval+1; marks(interval); pos=1; copy(fvw,t); mad(fvw,fd,fp,n,n,fvw); negify(t,fp); } if (!cp[pos]) continue; /* if neither interval+/-pos is prime, don't bother */ if (!plus[pos] && !minus[pos]) continue; subtract(fvw,fu[pos],t); mad(q,t,t,n,n,q); /* batching gcds */ } if (i++%btch==0) { /* try for a solution */ printf("\b\b\b\b\b\b\b\b%8ld",p); fflush(stdout); egcd(q,n,t); if (size(t)==1) { if (p>LIMIT2) break; else continue; } if (compare(t,n)==0) { printf("\ndegenerate case"); break; } printf("\nfactors are\n"); if (isprime(t)) printf("prime factor "); else printf("composite factor "); cotnum(t,stdout); divide(n,t,n); if (isprime(n)) printf("prime factor "); else printf("composite factor "); cotnum(n,stdout); return 0; } } if (nt>=NTRYS) break; printf("\ntrying again\n"); } printf("\nfailed to factor\n"); return 0; }
long long int lucas(long long int a,long long int b){ if(b==0) return 1; return (lucas(a/mod,b/mod)*C(a%mod,b%mod))%mod; }
inline ll lucas(int n, int m, int p) { if (m == 0) return 1; return com(n%p, m%p, p) * lucas(n/p, m/p, p) % p; }