Hdu 3304 interesting Yang Yui Triangle
Test instructions
Given P,n, how many of the Fibonacci number modulo p of the nth row are not equal to 0?
Limit:
P < 1000,n <= 10^9
Ideas:
Lucas theorem,
If:
n = a[k]*p^k + a[k-1]*p^ (k-1) + ... + a[1]*p + a[0]
m = b[k]*p^k + b[k-1]*p^ (k-1) + ... + b[1]*p + b[0]
The
C (n,m) = PE (I=0~K,C (a[i],b[i))%p wherein the PE represents a hyphen symbol.
Since N has been determined, so A[i] (0 <= i <= k) has been determined, so we just need to find out how many kinds of b[i each a[i], so C (A[i],b[i])%p!=0, violence once again.
/*hdu 3304 Interesting Yang Yui Triangle test instructions: Give p,n, ask the nth line Fibonacci number modulo p is not equal to 0 how many? Limitations: P < 1000,n <= 10^9 ideas: Lucas theorem, if: N = a[k]*p^k + a[k-1]*p^ (k-1) + ... + a[1]*p + a[0] m = b[k]*p^k + b[k -1]*p^ (k-1) + ... + b[1]*p + b[0]: C (n,m) = PE (I=0~K,C (a[i],b[i)))%p where PE represents a hyphen symbol. Since N has been determined, so A[i] (0 <= i <= k) has been determined, so we just need to find out how many kinds of b[i each a[i], so C (A[i],b[i])%p!=0, violence once again. */#include <iostream> #include <cstdio>using namespace std; #define LL long longconst int mod=10000;const int N =105;int a[n];int Cnt=0;int Ny[n]; LL INV (ll A,ll m) {ll p=1,q=0,b=m,c,d;while (b>0) {c=a/b;d=a; a=b; b=d%b;d=p; p=q; q=d-c*q;} return p<0?p+m:p;} void Predo (int p) {ny[0]=1;for (int i=1;i<p;++i) {NY[I]=INV (i,p);}} LL deal (int x,int p) {ll ret=0; LL cur=1%p;if (cur) ++ret;for (int i=1;i<=x;++i) {cur=cur*ny[i]%p* (x-i+1)%p;if (cur) ++ret;} return ret;} void Gao (int p, int n) {cnt=0;while (n) {a[cnt++]=n%p;n/=p;} LL ans=1;for (int i=0;i<cnt;++i) {ans=ans*deal (a[i],p)%mod;} printf ("%04lld\n", ans);} int main () {int P, N;int cas=0;while (scanf ("%d%d", &p, &n) && (p| | N) {Predo (P);p rintf ("Case%d:", ++cas); Gao (P, n);} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Hdu 3304 interesting Yang Yui Triangle