è¿é æµè¡ã®ãã£ããããæ°åããã®1000é
ç®ã誤差ãªã表示ããã
64 bit int ã§ã100é
ç®ã§ãªã¼ãã¼ããã¼ãããã§ç°¡åãããªãããã¡ãªã¿ã«ï¼ï¼ï¼æ¡ç¨åº¦ã
64 bit ã§100é
ç®ã§è¶³ããªãããã double ã§ãããããã¯èª¤å·®ãåºããã
ã¾ãå¤åé·æ¼ç®ã©ã¤ãã©ãªãæ¾ã£ã¦ããã°çµããã ãã©ãã
ãããã¨ãããã¾ãï¼
- http://d.hatena.ne.jp/kokarage/20071203/p1 ã¦ããï¼åé²æ°å¤åé·ï¼ENIACï¼
- http://d.hatena.ne.jp/smoking186/20071203/1196670148 ãªã¼ãã¼Log(n)æé
第äºå
ã§ã¯å¤åé·ã¨ã使ããæ°æ¥½ã«ãã«ãè¨ç®ã§ããåé¡ï¼
10ã®10ä¹é
ç®ã10é²æ°ã§è¡¨ããæã®ä¸ï¼æ¡ã表示ããã
ã¡ãªã¿ã«æ¢ã«å¨æçã«ãªã£ã¦ãããããgoogol (10ã®100ä¹)é ç®ãä¸ï¼æ¡ã¯åããgoogolplexé ç®ã¯O(log n)ã§ãã£ã¦ãå®å®ãçµããã
第ä¸å
ããã¦ããã¯ããªãé 使ããªãã¨ã§ããªããã
f(1)=3, f(2)=5, f(n+2)=f(n+1)*f(n) ã®å ´åã«10ã®10ä¹é
ç®ã10é²æ°ã§è¡¨ããæã®ä¸ï¼æ¡ã表示ããã
ããããã¯æã£ã¦ã以ä¸ã«é¢åã ãf(1)=3, f(2)=7ã®æ¹ãç°¡åãªã®ã§ãã¡ããå
ã«ã
第åå
第ä¸åã«é¢é£ãã¦ã
- 1. æ°å f(n) = a^n mod M, ã«ã¤ãã¦ãã®å¨ææ§ãæ±ããã
- 2. f(n+2) = (f(n)+f(n+1))mod M ã®å¨æãP(M)ã¨ç½®ããé常ã«å¤§ããªMã«ã¤ãã¦P(M)ãè¨ç®ããå¹ççãªæ¹æ³ã¯ãããï¼
ã¡ãªã¿ã«çãã¯ç¥ãã¾ããã2.ã¯å¤ãªåãããã¾ãã
M | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
P(M) | 3 | 8 | 6 | 20 | 24 | 16 | 12 | 24 | 60 | 10 | 24 | 28 | 48 | 40 | 24 | 36 | 24 | 18 | 60 |
ï¼ããã«é¢ããèå¯ã¯id:ita:20071204:p2 ã«ç§»å)
ããã¡ããã¨ååãã¤ãã¦ãã®ãhttp://mathworld.wolfram.com/PisanoPeriod.html ã§ã¯å¤©ä¸ä¸æ°å¤è¨ç®æ¦éä¼éå¬ï¼ã§ããã ã大ããP(M)/Mãæ±ããï¼
追è¨ï¼ããããä¸ããæ¼ãããããã ãæ¦éä¼çµäºãhttp://d.hatena.ne.jp/maehara/20071205/1196782700
ã¦ãã¨ã¯O(M)åã®ç¬ç«ãªè»éãããã®ãããããå¥ã®åé¡ï¼è»éã®åæ°Q(M)ã«ã¤ãã¦Q(M)/Mã®æ大å¤ãæ±ããã
ãã¾ãï¼ä¸è¨1.ã¨2.ãé«éã«è¨ç®ããã«ã¼ãã³ãããã¨ãã¦ãããã使ã£ã¦ç¬¬ä¸åãé«éã«è§£ãã³ã¼ãã示ãã
以ä¸ã¯1.ãæ°å¤çã«èª¿ã¹ãã³ã¼ã
#include <stdio.h> #include <stdlib.h> #include <strings.h> #define M 1000000 static int buf[M/2]; // table of (base^n)%M and its period, etc. typedef struct PowTab { int base; // if gcd(M, base)!=1, find max n such that M%(base^n)=0 // and store it to ofs // if gcd=1, ofs=0 int ofs; // period = (gcd==1)? min n such that (base^n)%M=1 : // min n such that (base^(ofs+n))%M = (base^ofs)%M int prd; // tab[i] = (base^i)%M int *tab; } PowTab; // gcd=1 case void init_tab_plain(int base, PowTab *ptab) { int n; int x=1; buf[0]=1; for(n=1;n<M;n++) { x = (x*base)%M; buf[n]=x; if (x==1) { ptab->prd=n; break; } } ptab->base = base; ptab->ofs=0; ptab->tab = (int *)malloc(ptab->prd*sizeof(int)); for(n=0;n<ptab->prd;n++) { ptab->tab[n] = buf[n]; } printf("Tab initialized: Base=%d p=%d ofs=%d\n",base, ptab->prd,ptab->ofs); } int gcd(int m, int n) { if (n==0) return m; else return gcd(n, m % n); } // pre-calculate (base^n)%M table and its period void init_tab(int base, PowTab *ptab) { int g=gcd(M, base); printf("initializing: base=%d gcd=%d\n",base,g); if(g==1) { init_tab_plain(base, ptab); return; } int ofs=0; int m0=M; int x0=1; int n; while(m0 % g==0) { buf[ofs]=x0; x0 = (x0*base)%M; m0/=g; ofs++; } int x=1; for(n=ofs;n<M;n++) { buf[n]=x0*x; x = (x*base)%m0; if(x==1) break; } ptab->base = base; ptab->ofs = ofs; ptab->prd = n+1-ofs; ptab->tab = (int *)malloc((n+1)*sizeof(int)); for(n=0;n<ptab->prd + ptab->ofs;n++) { ptab->tab[n] = buf[n]; } printf("Tab initialized: p=%d ofs=%d\n",ptab->prd,ptab->ofs); } // return (base^exp)%M int ModPow(PowTab *ptab, int exp) { int p=ptab->prd; int o=ptab->ofs; exp -= o; if (exp<0) exp+=p; return ptab->tab[o+exp%p]; }