HDU 4485 B-Casting

B-Casting

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 310 Accepted Submission(s): 150


Problem Description
Casting around for problems leads us to combine modular arithmetic with different integer bases, particularly the problem of computing values modulo b-1, where b is the base in which the value is represented. For example,

782910 mod 9 = 8,
377777777777777738 mod 7 = 6
1234567 mod 6 = 3

(Note that 377777777777777738 = 112589990684261910 and 1234567 = 2287510.)

Your job is to write a program that reads integer values in various bases and computes the remainder after dividing these values by one less than the input base.

Input
The first line of input contains a single integer P, (1 <= P <= 1000) , which is the number o data sets that follow. Each data set should be processed identically and independently.

Each data set consists of a single line of input containing three space-separated values. The first is an integer which is the data set number. The second is an integer which is the number, B (2 <= B <= 10), denoting a numeric base. The third is an unsigned number, D, in base B representation. For this problem, the number of numeric characters in D will be limited to 10,000,000.

Output
For each data set there is a single line of output. It contains the data set number followed by a single space which is then followed by the remainder resulting from dividing D by (B-1).

Sample Input
  
  
4 1 10 7829 2 7 123456 3 6 432504023545112 4 8 37777777777777773

Sample Output
  
  
1 8 2 3 3 1 4 6
  
  

贴两种风格的代码,供大家参考

#include <stdio.h>
#include <ctype.h>
int main()
{
    int T, i, b, c;
    scanf ( "%d", &T );

    for ( i = 1; i <= T; i++ )
    {
        scanf ( "%d %d ", &i, &b );
        int ans = 0;
      
		while ( isdigit ( c = getchar() ) )
        {
            ans = ( ans + c - '0' ) % ( b - 1 );
        }

        printf ( "%d %d\n", i, ans );
    }
    return 0;
}



#include<stdio.h>
#include<string.h>
#include<assert.h>
#define LL __int64
char num[10000005];
LL slove(LL n,char num[])
{
    LL temp=1,sum=0;
    int l=strlen(num);
    for(int i=l-1;i>=0;i--)
    {
        sum=(sum+((num[i]-'0')*temp))%(n-1);
        temp=(temp*n)%(n-1);
    }
    return sum%(n-1);
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int t,n;
        scanf("%d%d%s",&t,&n,num);
        printf("%d %I64d\n",t,slove(n,num));
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值