[LightOJ] [1214] Large Division

Problem Description

Loading...

Problem Analysis

This problem looks scary at first glance given the data range
but what it actually asks is simply
whether a is divisible by b

The solution still relies on the congruence theorem
simulate the big number with a string
then apply the congruence theorem
reading the digit information from the string one character at a time
while taking the remainder modulo b
using a long long to store the value

If it divides evenly in the end
that is sum==0
then it is divisible
otherwise it is not

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<stdio.h>
#include<string.h>
int main() {
	int T,b,l;
	char s[220];
	int i,j;
	long long sum;
	scanf("%d",&T);
	for(i=1; i<=T; i++) {
		scanf("%s %d",s,&b);
		l=strlen(s);
		for(j=sum=0; j<l; j++) {
			if(s[j]!='-') {
				sum=(sum*10+s[j]-'0')%b;
			}
		}
		if(!sum)
			printf("Case %d: divisible\n",i);
		else
			printf("Case %d: not divisible\n",i);
	}
	return 0;
}

Problem link: 【Light】[1214]Large Division

Loading...