计算余数问题/poj2305 Basic remains

Basic remains
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5029 Accepted: 2127

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 consists of a number of cases. Each case is represented by a line containing three unsigned integers. The first, b, is a decimal number between 2 and 10. The second, p, contains up to 1000 digits between 0 and b-1. The third, m, contains up to 9 digits between 0 and b-1. The last case is followed by a line containing 0.

Output

For each test case, print a line giving p mod m as a base-b integer.

Sample Input

2 1100 101
10 123456789123456789123456789 1000
0

Sample Output

10
789

Source

Waterloo local 2003.09.20


解析:进制转换。

            将 p 逐位转换为10进制,若大于m,则对m取余。

代码:

#include<cstdio>
#include<cstring>
#define maxn1 1000
#define maxn2 9
using namespace std;

char s1[maxn1+50],s2[maxn2+5];
int b,p,m,ans[maxn2+5];

void redirect()
{
  freopen("1.in","r",stdin);
  freopen("1.out","w",stdout);
}

void work()
{
  int i,j,k,x,y;
  scanf("%s%s",s1,s2);
  for(m=i=0,k=strlen(s2);i<k;i++)
    m=m*b+s2[i]-'0';
  for(p=i=0,k=strlen(s1);i<k;i++)
    p=p*b+s1[i]-'0',p%=m;
  if(p==0){printf("0\n");return;}  
  ans[0]=0;while(p>0)ans[++ans[0]]=p%b,p/=b;
  for(i=ans[0];i>=1;i--)printf("%d",ans[i]);
  printf("\n");  
}

int main()
{
  redirect();
  while(scanf("%d",&b) && b!=0)work();
  return 0;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值