poj 2305 Basic remains 高精度取余

题意:

裸的高精度取余。

分析:

http://blog.csdn.net/sepnine/article/details/44092055有poj 1220任意进制转换的代码,这题用到其中的一部分,可作对比。

代码:

//poj 2305
//sep9
#include <iostream>
using namespace std;
int b,m;
char s1[1024],s2[16],ans[16];
int p[1024];

int main()
{
	while(scanf("%d",&b)==1&&b){
		scanf("%s%s",s1,s2);
		int k,i;
		k=strlen(s2);
		for(m=i=0;i<k;++i)
			m=m*b+s2[i]-'0';
		k=strlen(s1);
		for(i=k-1;i>=0;--i)
			p[k-1-i]=s1[i]-'0';
		for(i=k-1;i>0;--i){
			p[i-1]+=p[i]%m*b;
			p[i]/=m;
		}
		int l=0,a=p[0]%m;
		if(!a){
			puts("0");
			continue;
		}
		while(a){
			ans[l++]=a%b;
			a/=b;
		}
		for(i=l-1;i>=0;--i)
			printf("%d",ans[i]);
		puts("");
	}
	return 0;
} 
时间: 2024-08-05 10:45:58

poj 2305 Basic remains 高精度取余的相关文章

poj 2305 Basic remains java

import java.math.BigDecimal; import java.math.BigInteger; import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner cin= new Scanner(System.in); int b=0; while(cin.hasNextInt())

POJ 2305 Basic remains(JAVA练习)

Description Given a base b and two non-negative base b integers p and m, compute p mod m and print the result as a base b integer. p mod m is defined as the smallest non-negative integer k such that p = a*m + k for some integer a. Input Input consist

POJ 1745 线性和差取余判断

POJ 1745 线性和差取余判断 题目大意:每个数都必须取到,相加或相减去,问所有的方案最后的得数中有没有一个方案可以整除k 这个题目的难点在于dp数组的安排上面 其实也就是手动模仿了一下 比如 一个数,不用说,第一个数之前不用加符号就是本身,那么本身直接对K取余, 那么取17的时候有个余数为2----基础然后来了一个5,(2 + 5)对7取余为0----层层延伸 (2 - 5)对7取余为4(将取余的负数变正) 那么前2个数有余数0和4再来一个-21(0+21)对7取余为0(0-21)对7取余

poj 3349:Snowflake Snow Snowflakes(哈希查找,求和取余法+拉链法)

Snowflake Snow Snowflakes Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 30529   Accepted: 8033 Description You may have heard that no two snowflakes are alike. Your task is to write a program to determine whether this is really true. Y

POJ 3070 + 51Nod 1242 大斐波那契数取余

POJ 3070 #include "iostream" #include "cstdio" using namespace std; class matrix { public: int a[2][2]; matrix() { a[0][0]=a[1][0]=a[0][1]=1; a[1][1]=0; } }; matrix multi(matrix a,matrix b) { matrix temp; int i,j,k; for(i=0;i<2;i++)

poj 1845 Sumdiv (同余定理,快速幂取余)

链接:poj 1845 题意:求A^B的所有因子的和对9901取余后的值 如:2^3=8,8的因子有 1,2,4,8,所有和为15,取余后也是15 应用定理主要有三个: (1)整数的唯一分解定理: 任意正整数都有且只有一种方式写出其素因子的乘积表达式. A=(p1^k1)*(p2^k2)*(p3^k3)*....*(pn^kn)   其中pi均为素数 (2)约数和公式: 对于已经分解的整数A=(p1^k1)*(p2^k2)*(p3^k3)*....*(pn^kn) 有A的所有因子之和为 S = 

POJ 2635-The Embarrassed Cryptographer(高精度求模+同余模定理)

The Embarrassed Cryptographer Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2635 Appoint description:  System Crawler  (2015-05-28) Description The young and very promising cryptographer Odd E

【题解】 P2613 【模板】有理数取余

题目 题目描述 给出一个有理数 \(c=\frac{a}{b}\),求 c mod 19260817 的值. 输入格式 一共两行. 第一行,一个整数 a. 第二行,一个整数 b. 输出格式 一个整数,代表求余后的结果.如果无解,输出 "Angry!". 输入输出样例 输入 233 666 输出 18595654 说明/提示 对于所有数据 \(0 <= a,b <= 10^{10001}\) 分析 模意义下,除以一个数等于乘它的逆元 高精度??输入long long化 因为可

加/减/乘/除 下的取余

x(负数) mod y(正数) = z 其中x<z<=0 , 且(z-x) mod y=0. 求法:z=x+abs(x)/y*y (这里的除法结果向下取整) 所以当求一个数经过各种计算后的取余,只需 ans=(ans+x*y)%yu ans=(ans-x*y)%yu ans=ans*x%yu 除法:用线性逆元 最后 ans=(ans+yu)%yu (要是之前有减法运算)