int main() { int t,r,n,mod,ans,alfa,array; int a,d; long long int first,sum; //printf("%d\n",mulmod(223631,147020,8550886)); scanf("%d",&t); while(t--) { scanf("%d %d %d",&a,&d,&r); scanf("%d %d",&n,&mod); if(n == 1) { ans = a % mod; printf("%d\n",ans); continue; } alfa = (n-1)/2; array = findSum(r,alfa,mod);//r^alfa + ... + 1 sum = array; first = a%mod; first = (first*modular_pow(r,alfa,mod))%mod;//(a*r^alfa)%mod if(n%2 == 1) sum = (sum - 1%mod)%mod; sum = ((d%mod)*sum)%mod; sum = (first + sum)%mod; printf("%lld\n",sum); } return 0; }
static int __create_fca_comm(mca_coll_fca_module_t *fca_module) { int rc, ret; int result = MPI_UNEQUAL; mca_coll_fca_c_cache_item_t *c_item = NULL, *c_item_new = NULL; mca_coll_fca_component_t *cm = &mca_coll_fca_component; int comm_size = ompi_comm_size(fca_module->comm); ompi_communicator_t *comm = fca_module->comm; opal_list_t *c_cache; struct timeval start, end, seq_start, seq_end, par_start, par_end; int act_deep; if(mca_coll_fca_component.fca_verbose == 10) { gettimeofday(&start, NULL); } if(mca_coll_fca_component.fca_enable_cache) { c_cache = &cm->c_cache; if(mca_coll_fca_component.fca_enable_hash){ int grank = ORTE_PROC_MY_NAME->vpid; int hash_index, part_of_hash_index; if(mca_coll_fca_component.fca_parallel_hash_calc == 1) { if(mca_coll_fca_component.fca_verbose == 10){ gettimeofday(&par_start, NULL); } part_of_hash_index = modular_pow(cm->fca_primes[grank % cm->fca_number_of_primes], grank, cm->fca_hash_size); rc = comm->c_coll.coll_allreduce(&part_of_hash_index, &hash_index, 1, MPI_INT, MPI_SUM, comm, comm->c_coll.coll_allreduce_module); if (rc != OMPI_SUCCESS) { FCA_ERROR("Failed to reduce hash_index: %d", rc); return rc; } if(mca_coll_fca_component.fca_verbose == 10){ gettimeofday(&par_end, NULL); mca_coll_fca_component.fca_work_time_parallel =+ par_end.tv_sec - par_start.tv_sec + 1e-6 * (par_end.tv_usec - par_start.tv_usec); } }else{ if(mca_coll_fca_component.fca_verbose == 10){ gettimeofday(&seq_start, NULL); } hash_index = 0; int q_counter = 0; int q_comm_size = ompi_comm_size (comm); for(q_counter = 0; q_counter < q_comm_size; q_counter++) { hash_index += modular_pow(cm->fca_primes[q_counter % cm->fca_number_of_primes], q_counter, cm->fca_hash_size); } if(mca_coll_fca_component.fca_verbose == 10){ gettimeofday(&seq_end, NULL); mca_coll_fca_component.fca_work_time_sequency =+ seq_end.tv_sec - seq_start.tv_sec + 1e-6 * (seq_end.tv_usec - seq_start.tv_usec); } } if(cm->fca_hash[hash_index % cm->fca_hash_size] != NULL) { c_cache = cm->fca_hash[hash_index % cm->fca_hash_size]; if(mca_coll_fca_component.fca_verbose == 10) { gettimeofday(&end, NULL); mca_coll_fca_component.fca_total_work_time =+ end.tv_sec - start.tv_sec + 1e-6 * (end.tv_usec - start.tv_usec); mca_coll_fca_component.fca_hash_hit += 1; } }else { if(mca_coll_fca_component.fca_verbose == 10) { mca_coll_fca_component.fca_hash_miss += 1; } c_cache = OBJ_NEW(opal_list_t); cm->fca_hash[hash_index % cm->fca_hash_size] = c_cache; } } act_deep = 0; for( c_item = (mca_coll_fca_c_cache_item_t *)opal_list_get_first(c_cache); c_item != (mca_coll_fca_c_cache_item_t *)opal_list_get_end(c_cache); c_item = (mca_coll_fca_c_cache_item_t *)opal_list_get_next((opal_list_item_t *) c_item)){ act_deep++; /* first check the size */ if( c_item && (comm_size == c_item->size)) { /* then we have a potential cache hit */ ompi_comm_compare(comm, c_item->comm, &result); if( MPI_CONGRUENT == result) { /* cache hit! Return the context and be done with it */ /* first bump the score */ ret = fca_comm_get_caps(c_item->fca_comm_wrap->fca_comm, &fca_module->fca_comm_caps); if (ret < 0) { FCA_ERROR("GET_COMM_CAPS failed: %s", fca_strerror(ret)); return OMPI_ERROR; } fca_module->fca_comm = c_item->fca_comm_wrap->fca_comm; if(mca_coll_fca_component.fca_verbose == 10) { gettimeofday(&end, NULL); mca_coll_fca_component.fca_total_work_time =+ end.tv_sec - start.tv_sec + 1e-6 * (end.tv_usec - start.tv_usec); mca_coll_fca_component.fca_cache_hit += 1; if(act_deep>mca_coll_fca_component.fca_max_deep_in_cache) mca_coll_fca_component.fca_max_deep_in_cache = act_deep; } return OMPI_SUCCESS; } } } } rc = __fca_comm_new(fca_module); if (OMPI_SUCCESS != rc) { return rc; } #if OMPI_FCA_VERSION < 30 /* allocate comm_init_spec */ FCA_MODULE_VERBOSE(fca_module, 1, "Starting COMM_INIT comm_id %d proc_idx %d num_procs %d", fca_module->fca_comm_desc.comm_id, fca_module->local_proc_idx, fca_module->num_local_procs); ret = mca_coll_fca_comm_init(mca_coll_fca_component.fca_context, fca_module->rank, ompi_comm_size(fca_module->comm), fca_module->local_proc_idx, fca_module->num_local_procs, &fca_module->fca_comm_desc, &fca_module->fca_comm); if (ret < 0) { FCA_ERROR("COMM_INIT failed: %s", fca_strerror(ret)); return OMPI_ERROR; } #endif /* get communicator capabilities */ ret = fca_comm_get_caps(fca_module->fca_comm, &fca_module->fca_comm_caps); if (ret < 0) { FCA_ERROR("GET_COMM_CAPS failed: %s", fca_strerror(ret)); return OMPI_ERROR; } /* by this point every rank in the communicator is set up */ FCA_MODULE_VERBOSE(fca_module, 1, "Initialized FCA communicator, comm_id %d", fca_module->fca_comm_desc.comm_id); if(mca_coll_fca_component.fca_enable_cache) { c_item_new = OBJ_NEW(mca_coll_fca_c_cache_item_t); c_item_new->fca_comm_wrap = OBJ_NEW(mca_coll_fca_comm_wrap_t); OBJ_RETAIN(comm); c_item_new->size = comm_size; c_item_new->comm = comm; c_item_new->fca_comm_wrap->fca_comm = fca_module->fca_comm; c_item_new->fca_comm_wrap->rank = fca_module->rank; c_item_new->fca_comm_wrap->comm_id = fca_module->fca_comm_desc.comm_id; opal_list_append(c_cache,(opal_list_item_t *) c_item_new); } if(mca_coll_fca_component.fca_verbose == 10) { gettimeofday(&end, NULL); mca_coll_fca_component.fca_total_work_time =+ end.tv_sec - start.tv_sec + 1e-6 * (end.tv_usec - start.tv_usec); mca_coll_fca_component.fca_cache_miss += 1; } return OMPI_SUCCESS; }
int main() { // b = 4, e = 13, and m = 497 return modular_pow(4, 13, 497); }