void RGBtoXYZ(int R, int G, int B, double *x, double *y, double *z) { // convert 0..255 into 0..1 double r = R / 255.0; double g = G / 255.0; double b = B / 255.0; // assume sRGB if (r <= 0.04045) { r = r / 12.92; } else { r = fpow(((r + 0.055) / 1.055), 2.4); } if (g <= 0.04045) { g = g / 12.92; } else { g = fpow(((g + 0.055) / 1.055), 2.4); } if (b <= 0.04045) { b = b / 12.92; } else { b = fpow(((b + 0.055) / 1.055), 2.4); } r *= 100.0; g *= 100.0; b *= 100.0; // [X Y Z] = [r g b][M] *x = (r * M[0][0]) + (g * M[0][1]) + (b * M[0][2]); *y = (r * M[1][0]) + (g * M[1][1]) + (b * M[1][2]); *z = (r * M[2][0]) + (g * M[2][1]) + (b * M[2][2]); }
void XYZtoLAB(double X, double Y, double Z, double *l, double *a, double *b) { double x = X / whitePoint[0]; double y = Y / whitePoint[1]; double z = Z / whitePoint[2]; if (x > 0.008856) { x = fpow(x, 1.0 / 3.0); } else { x = (7.787 * x) + (16.0 / 116.0); } if (y > 0.008856) { y = fpow(y, 1.0 / 3.0); } else { y = (7.787 * y) + (16.0 / 116.0); } if (z > 0.008856) { z = fpow(z, 1.0 / 3.0); } else { z = (7.787 * z) + (16.0 / 116.0); } *l = (116.0 * y) - 16.0; *a = 500.0 * (x - y); *b = 200.0 * (y - z); }
// a.k.a Tonelli_Shanks algorithm int ressol(int n, int p) { n %= p; if (n == 0) return 0; if (legendre(n, p) != 1) return -1; int q = p - 1, s = 0, z, c; while (q % 2 == 0) q /= 2, ++s; while (true) { z = rand() % (p - 1) + 1; if (legendre(z, p) == p - 1) break; } c = fpow(z, q, p); int r = fpow(n, (q + 1) / 2, p), t = fpow(n, q, p), m = s; while (true) { if (t == 1) return r; for (int i = 0, tmp = t; i < m; ++i, tmp = 1ll * tmp * tmp % p) { if (tmp == 1) { int b = fpow(c, 1 << (m - i - 1), p); c = 1ll * b * b % p; r = 1ll * r * b % p; t = 1ll * t * c % p; m = i; break; } } } return r; }
void LABtoXYZ(double L, double a, double b, double *xo, double *yo, double *zo) { double y = (L + 16.0) / 116.0; double y3 = fpow(y, 3.0); double x = (a / 500.0) + y; double x3 = fpow(x, 3.0); double z = y - (b / 200.0); double z3 = fpow(z, 3.0); if (y3 > 0.008856) { y = y3; } else { y = (y - (16.0 / 116.0)) / 7.787; } if (x3 > 0.008856) { x = x3; } else { x = (x - (16.0 / 116.0)) / 7.787; } if (z3 > 0.008856) { z = z3; } else { z = (z - (16.0 / 116.0)) / 7.787; } *xo = x * whitePoint[0]; *yo = y * whitePoint[1]; *zo = z * whitePoint[2]; }
int XYZtoRGB(double X, double Y, double Z, int *R, int *G, int *B) { double x = X / 100.0; double y = Y / 100.0; double z = Z / 100.0; // [r g b] = [X Y Z][Mi] double r = (x * Mi[0][0]) + (y * Mi[0][1]) + (z * Mi[0][2]); double g = (x * Mi[1][0]) + (y * Mi[1][1]) + (z * Mi[1][2]); double b = (x * Mi[2][0]) + (y * Mi[2][1]) + (z * Mi[2][2]); // assume sRGB if (r > 0.0031308) { r = ((1.055 * fpow(r, 1.0 / 2.4)) - 0.055); } else { r = (r * 12.92); } if (g > 0.0031308) { g = ((1.055 * fpow(g, 1.0 / 2.4)) - 0.055); } else { g = (g * 12.92); } if (b > 0.0031308) { b = ((1.055 * fpow(b, 1.0 / 2.4)) - 0.055); } else { b = (b * 12.92); } #if 0 if (r < 0) { r = 0; } if (g < 0) { g = 0; } if (b < 0) { b = 0; } if (r > 1) { r = 1; } if (g > 1) { g = 1; } if (b > 1) { b = 1; } if (r <= 0 || g <= 0 || b <= 0) { return 0; } if (r > 1 || g > 1 || b > 1) { return 0; } if (isnan(r) || isnan(g) || isnan(b)) { return 0; } #endif *R = r * 255; *G = g * 255; *B = b * 255; return 1; }
// add minibrick volume void minilod::addbrick(char *brickname, float brad, unsigned int lods, float stagger) { int i; minibrickdata *newbricks; float dist; if (brad<0.0f || stagger<=1.0f) ERRORMSG(); if (brad==0.0f) lods=1; if (BRICKS==NULL) { BMAX=1; BRICKS=new minibrickdata[BMAX]; } else if (BNUM>=BMAX) { newbricks=new minibrickdata[2*BMAX]; for (i=0; i<BNUM; i++) newbricks[i]=BRICKS[i]; delete[] BRICKS; BRICKS=newbricks; BMAX*=2; } BRICKS[BNUM].bname=strdup(brickname); BRICKS[BNUM].brick=new minibrick[lods]; BRICKS[BNUM].brad=brad; BRICKS[BNUM].lods=lods; BRICKS[BNUM].stagger=stagger; for (i=0; i<lods; i++) { // set brick pager BRICKS[BNUM].brick[i].setloader(DBavailable_callback,&BRICKS[BNUM],DBload_callback, OFFSETLAT,OFFSETLON,SCALEX,SCALEY,SCALEELEV); // set iso spectrum BRICKS[BNUM].brick[i].addiso(0.5f,1.0f,1.0f,1.0f,1.0f); // set render method BRICKS[BNUM].brick[i].configure_renderpasses(CONFIGURE_BRICKPASSES); // calculate staggered distance dist=0.999f*brad*fpow(stagger,i); // extract meshes BRICKS[BNUM].brick[i].setdistance(dist); BRICKS[BNUM].brick[i].extract(0.0f,0.0f,0.0f,-brad,dist,90.0f,1.0f); BRICKS[BNUM].brick[i].release(); } BNUM++; }
// discrete-logarithm, finding y for equation k = x^y % mod int discrete_logarithm(int x, int mod, int k) { if (mod == 1) return 0; int s = 1, g; for (int i = 0; i < 64; ++i) { if (s == k) return i; s = (1ll * s * x) % mod; } while ((g = gcd(x, mod)) != 1) { if (k % g) return -1; mod /= g; } static unordered_map<int, int> M; M.clear(); int q = int(sqrt(double(euler(mod)))) + 1; // mod-1 is also okay for (int i = 0, b = 1; i < q; ++i) { if (M.find(b) == M.end()) M[b] = i; b = (1ll * b * x) % mod; } int p = fpow(x, q, mod); for (int i = 0, b = 1; i <= q; ++i) { int v = (1ll * k * inverse(b, mod)) % mod; if (M.find(v) != M.end()) { int y = i * q + M[v]; if (y >= 64) return y; } b = (1ll * b * p) % mod; } return -1; }
float mag_analytic(float temp) /* analytic solution for magnetization */ { int i; float mag; if (temp >= Tc) mag = 0.0; else mag = fpow( (1.0-pow(sinh(2.0/temp),-4)) , 0.125); return(mag); }
int main(){ int n, x; printf("Insira o numero x:"); scanf("%d", &x); printf("Insira o numero n, para x^n:"); scanf("%d", &n); printf("%d^%d=%d\n",x, n, fpow(x, n)); system("PAUSE"); return 0; }
float get_max_float() { /* * IEEE 754 * * float: * sign: 1bit, exp: 8bit, frac: 23bit * * F = (-1)^sign * frac * 2^exp */ int exp_bit = 8; int frac_bit = 23; float frac = 1 - fpow(0.5, frac_bit); float true_frac = 1 + frac; int exp = xpow(2, exp_bit - 1) - 1; float times = fpow(2.0, exp); float max = true_frac * times; return max; }
bool miller_rabin(LL n, LL b) { // miller-rabin prime test LL m = n - 1, cnt = 0; while (m % 2 == 0) m >>= 1, ++cnt; LL ret = fpow(b, m, n); if (ret == 1 || ret == n - 1) return true; --cnt; while (cnt >= 0) { ret = mult64(ret, ret, n); if (ret == n - 1) return true; --cnt; } return false; }
int init(int n) { // assert(k <= 19); int k = 1, N = 2, p; while (N < n) N <<= 1, ++k; p = 7 * 17; for (int i = 1; i <= 23 - k; ++i) p *= 2; E1 = fpow(3, p, P1); F1 = fpow(E1, P1 - 2, P1); I1 = fpow(N, P1 - 2, P1); p = 9 * 211; for (int i = 1; i <= 19 - k; ++i) p *= 2; E2 = fpow(5, p, P2); F2 = fpow(E2, P2 - 2, P2); I2 = fpow(N, P2 - 2, P2); return N; }
ull fpow(int a,int n) { if(n==0) return 1; if(n==1) return a; else { ull temp=fpow(a,n/2); if(n%2) return (((temp*temp)%MOD)*a)%MOD; else return (temp*temp)%MOD; } }
// primtive root, finding the number with order p-1 int primtive_root(int p) { vector<int> factor; int tmp = p - 1; for (int i = 2; i * i <= tmp; ++i) { if (tmp % i == 0) { factor.push_back(i); while (tmp % i == 0) tmp /= i; } } if (tmp != 1) factor.push_back(tmp); for (int root = 1; ; ++root) { bool flag = true; for (int i = 0; i < factor.size(); ++i) { if (fpow(root, (p - 1) / factor[i], p) == 1) { flag = false; break; } } if (flag) return root; } }
int main() { int t,n,m,k,i; ull val=0; ull inverse[100001],factorial[300001]; factorial[0]=1; for(i=1;i<=300000;i++) { factorial[i]=factorial[i-1]*i; if(factorial[i]>MOD) factorial[i]%=MOD; } inverse[0]=0; inverse[1]=1; for(i=2;i<=100000;i++) { inverse[i]=inverse[i-1]*fpow(i,MOD-2); if(inverse[i]>MOD) inverse[i]%=MOD; } scanf("%d",&t); while(t--) { scanf("%d%d%d",&n,&m,&k); val=factorial[n+m+k]; val*=inverse[n]; if(val>=MOD) val%=MOD; val*=inverse[m]; if(val>=MOD) val%=MOD; val*=inverse[k]; if(val>=MOD) val%=MOD; printf("%lld\n",val); } return 0; }
void output(uint32_t presum[], int n) { omp_set_num_threads(4); int MM = 4; uint32_t hash[4] = {}; #pragma omp parallel for for (int i = 0; i < MM; i++) { int l = (i*n)/MM+1; int r = ((i+1)*n)/MM; uint32_t h = 0; for (int j = l; j <= r; j++) h = h * SKEY + presum[j]; hash[i] = h; } uint32_t ret = 0; for (int i = 0; i < MM; i++) { int l = i * n / MM+1; int r = (i+1)*n/ MM; ret = ret * fpow(SKEY, r - l+ 1) + hash[i]; } printf("%" PRIu32 "\n", ret); }
int main(){ int t; scanf("%d",&t); for (int cases = 1; cases <= t ; ++cases) { printf("Case #%d:\n",cases); scanf("%d%d",&n,&m); for (int i = 1; i <= n; ++i) scanf("%s",op[i]); while (m--){ int tp; scanf("%d",&tp); if (tp == 1){ int x; scanf("%d",&x); int tmp ; for (int i = 1; i <= n; ++i) { sscanf(op[i] + 1,"%d",&tmp); if (op[i][0] == '*'){ x = x*tmp%MOD; } else if (op[i][0] == '+'){ x = (x+tmp)%MOD; } else{ x = fpow(x,tmp); } } printf("%d\n",x); } else{ int pos; scanf("%d",&pos); scanf("%s",op[pos]); } } } }
int inverse(int x, int mod) { // multiplicative inverse int a = 0, b = 0; if (exgcd(x, mod, a, b) != 1) return -1; return (a % mod + mod) % mod; // C1: x & mod are co-prime return fpow(x, mod - 2, mod); // C2: mod is prime }
int legendre(int n, int p) { return fpow(n, (p - 1) / 2, p); }
void calculate(double *photoperiod, double *mean_air_temp, double *daily_precipitation, double *popdens, double *param, double *n0, double *n10, double *n1, double *n2, double *n3, double *n4fj, double *n4f, double *nBS, double *K, double *d4, double *d4s, double *F4, double *egg, double *percent_strong, int TIME) { double par[2]; if (TIME==-1) { (*n0) = param[alpha_dp_egg]; (*n10) = 0.0; (*n1) = param[alpha_0]; (*n2) = param[alpha_1]; (*n3) = param[alpha_2]; (*n4fj) = 0.0; (*n4f) = param[alpha_3]; (*nBS) = 0.0; (*K) = 0.0; (*d4) = 0.0; (*d4s) = 0.0; (*F4) = 0.0; (*egg) = (*n0) + (*n10) + (*n1); (*percent_strong) = 0.0; if ((*n10) > 0) incubator_add(&conn10,(*n10),0.0); if ((*n1) > 0) incubator_add(&conn1,(*n1),0.0); if ((*n2) > 0) incubator_add(&conn2,(*n2),0.0); if ((*n3) > 0) incubator_add(&conn3,(*n3),0.0); if ((*n4f) > 0) incubator_add(&conn4,(*n4f),0.0); return; } // --------------------- // modelDelayAalbopictus // --------------------- double deltaT = param[alpha_deltaT]; double Ta = mean_air_temp[TIME]; double Tw = Ta+deltaT; // Number of breeding sites (*nBS) = param[alpha_BS_pdens]*(*popdens) + param[alpha_BS_dprec]*daily_precipitation[TIME] + param[alpha_BS_nevap]*(*nBS); (*K) = param[alpha_BS_nevap]==1.0 ? (*nBS)/(TIME+1.0) : (*nBS)*(param[alpha_BS_nevap]-1.0)/(pow(param[alpha_BS_nevap],(TIME+1))-1.0); // Density of the immature stages // Assume uniform distribution across all breeding sites double n23dens = ((incubator_sum(conn2)+incubator_sum(conn3))/(*K)); // Fecundity double bigF4 = poly(Ta,param[alpha_F4_1],param[alpha_F4_2],param[alpha_F4_3]); (*F4) = bigF4; double densd = expd(n23dens,param[alpha_n23_surv]); // Egg survival (diapausing eggs) double p0_Ta = flin(Ta,param[alpha_p0_1],param[alpha_p0_2]); // Egg survival (non-diapausing eggs) double p1_Tw = dsig(Tw,param[alpha_p1_1],param[alpha_p1_2],param[alpha_p1_3]); // Larval survival double p2_Tw = dsig(Tw,param[alpha_p2_1],param[alpha_p2_2],param[alpha_p2_3])*densd; // Pupal survival double p3_Tw = dsig(Tw,param[alpha_p3_1],param[alpha_p3_2],param[alpha_p3_3])*densd; double densdev = fpow(n23dens,param[alpha_n23_1],param[alpha_n23_2]*poly(Tw,param[alpha_n23_3],param[alpha_n23_4],param[alpha_n23_5])); // Egg development time double d1 = poly(Tw,param[alpha_d1_1],param[alpha_d1_2],param[alpha_d1_3]); // Larval development time double d2 = poly(Tw,param[alpha_d2_1],param[alpha_d2_2],param[alpha_d2_3])*densdev; // Pupal development time double d3 = poly(Tw,param[alpha_d3_1],param[alpha_d3_2],param[alpha_d3_3])*densdev; // Time to first blood meal double alpha_blood = poly(Ta,param[alpha_tbm_1],param[alpha_tbm_2],param[alpha_tbm_3]); // Adult lifetime (from emergence) double dd4 = dsig2(Ta,param[alpha_d4_1],param[alpha_d4_2],param[alpha_d4_3]); double dd4s = gamma_matrix_sd*dd4; (*d4) = dd4; (*d4s) = dd4s; // Update development times and survival proportions // of the immature stages and juvenile adults // // Diapausing eggs (*n0) = p0_Ta*(*n0); // // Normal and tagged eggs par[0] = p1_Tw; par[1] = 1.0/max(1.0,d1); incubator_update(conn1,update,par); incubator_update(conn10,update,par); // // Larvae par[0] = p2_Tw; par[1] = 1.0/max(1.0,d2); incubator_update(conn2,update,par); // // Pupae par[0] = p3_Tw; par[1] = 1.0/max(1.0,d3); incubator_update(conn3,update,par); // // Adult females incubator_develop_survive(&conn4,-1,0,dd4,dd4s,alpha_blood,n4fj,n4f,0,gamma_mode); // // Check if it is winter or summer char short_days = photoperiod[TIME] < param[alpha_dp_thr]; char cold_days = Ta < param[alpha_ta_thr]; // // Lay eggs (*egg) = bigF4*(*n4f); // Total number of eggs that will be laid that day double hatch = 0; // if (short_days && cold_days) { // DIAPAUSE // The fraction of tagged eggs increases linearly (*percent_strong) = min(1.0,(*percent_strong)+param[alpha_rate_strong]); // incubator_add(&conn10,bigF4*(*n4f)*(*percent_strong),0.0); // Tagged eggs incubator_add(&conn1,bigF4*(*n4f)*(1.0-(*percent_strong)),0.0); // Normal eggs // } else { // NO DIAPAUSE if (!short_days && !cold_days) { // EXIT FROM DIAPAUSE // Prepare diapausing eggs for hatching double vn0_n10 = (*n0)*param[alpha_rate_normal]; (*n0) -= vn0_n10; // Diapausing eggs hatch += vn0_n10; // Eggs to hatch } // Lay normal eggs incubator_add(&conn1,bigF4*(*n4f),0.0); // Normal eggs // (*percent_strong) = 0.0; } // Develop double nn1; double nn2; double nn3; incubator_remove(&conn3,&nn3); incubator_remove(&conn2,&nn2); incubator_add(&conn3,nn2,0.0); incubator_remove(&conn1,&nn1); hatch += nn1; // Harvest developed tagged eggs double vn10_hn0; incubator_remove(&conn10,&vn10_hn0); // Tagged eggs always become diapausing eggs (*n0) = (*n0) + vn10_hn0; // All eggs, which are ready to hatch, become larvae incubator_add(&conn2,hatch,0.0); // // Update immature stage counts (*n3) = incubator_sum(conn3); (*n2) = incubator_sum(conn2); (*n1) = incubator_sum(conn1) + incubator_sum(conn10); // // Add newly developed females to adults nn3 *= 0.5; incubator_add(&conn4,nn3,0.0); (*n4f) += nn3; }
int fpow (int x, int n){ if (n == 0) return 1; if (n == 1) return x; else return x*fpow(x,n-1); }