Page 3 of 9
Posted: Sun Aug 17, 2003 7:14 am
by Master
To calculate m^n you don't need to calculate m^n

but just calculate (
last digit of m)^n

. And the last digit can only be from 0 to 9. For each digit there is a repeated sequence.

your task is to find that and then just use a switch statement.
M H Rasel
CUET Old Sailor
I got AC .
Posted: Thu Sep 11, 2003 5:10 am
by yan4546
2 digits is not enough and 3 digits can do .
test cases!
Posted: Thu Sep 11, 2003 5:19 am
by yan4546
Input:
12 10000
1 1000
2313242341234 100000
32872342144876231874687236872364987164876239874698721649823149823749854354356490487239759843 4359000000000000004354354356463464356346435643574839579473950739480702750847d239586345
33333333333333333333333333333333333333333333333333333333333333333333 3222222222234433222222222222222222222342342344444444444
1 1
0 0
output:
6
1
6
3
1
1
Posted: Thu Oct 16, 2003 4:00 pm
by Iwashere
Anyone can help me with this one? Thanks.
[c]
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char m[103], n[103], ml[2], nl[2], i;
int mlast, nlast, mlen, nlen;
int main(){
for (;;){
for (i=0; i<103; i++){
m='\0';
n='\0';
}
scanf("%s %s",m,n);
if ((m[0]=='0')&&(m[1]=='\0')&&(n[0]=='0')&&(n[1]=='\0')){
break;
}
mlen=strlen(m)-1;
nlen=strlen(n)-1;
ml[0]=m[mlen];
nl[0]=n[nlen];
mlast=atoi(ml);
nlast=atoi(nl);
if ((n[0]='0')&&(n[1]='\0')){
printf ("0\n");
}
else if (mlast==1){
printf ("1\n");
}
else if (mlast==2){
if (nlast%4==0){
printf("6\n");
}
else if (nlast%4==1){
printf("2\n");
}
else if (nlast%4==2){
printf("4\n");
}
else if (nlast%4==3){
printf("8\n");
}
}
else if (mlast==3){
if (nlast%4==0){
printf("1\n");
}
else if (nlast%4==1){
printf("3\n");
}
else if (nlast%4==2){
printf("9\n");
}
else if (nlast%4==3){
printf("7\n");
}
}
else if (mlast==4){
if (nlast%2==0){
printf("6\n");
}
else if (nlast%2==1){
printf("4\n");
}
}
else if (mlast==5){
printf("5\n");
}
else if (mlast==6){
printf("6\n");
}
else if (mlast==7){
if (nlast%4==0){
printf("1\n");
}
else if (nlast%4==1){
printf("7\n");
}
else if (nlast%4==2){
printf("9\n");
}
else if (nlast%4==3){
printf("3\n");
}
}
else if (mlast==8){
if (nlast%4==0){
printf("6\n");
}
else if (nlast%4==1){
printf("8\n");
}
else if (nlast%4==2){
printf("4\n");
}
else if (nlast%4==3){
printf("2\n");
}
}
else if (mlast==9){
if (nlast%2==0){
printf("1\n");
}
else if (nlast%2==1){
printf("9\n");
}
}
else if (mlast==0){
printf("0\n");
}
}
return 0;
}
[/c]
Posted: Wed Oct 22, 2003 5:19 pm
by Towhid
Try for the data
15 0
The output should be 1
Posted: Sat Oct 25, 2003 2:40 pm
by Towhid
Oh! I forgot another input for Iwashere.
It is:
2 12
the output is 6.
and so on.............
Posted: Sun Oct 26, 2003 7:56 am
by zubair
i don't know what's the probs here? i have tested many times but invain
can any body halp me on my code with some test data
[cpp]
arios
[/cpp]
Posted: Mon Oct 27, 2003 11:54 am
by Iwashere
Found what's wrong.
Some caculation problem and I did not know that n^0=1.
Thank you very much, Towhid
Posted: Mon Oct 27, 2003 2:13 pm
by zubair
got accepted. thnx
Posted: Mon Nov 03, 2003 7:43 pm
by yeameen
I got TLE? whats wrong with the following code?
[c]
#include <stdio.h>
int main(void)
{
int result, m, n;
while(scanf("%d%d", &m, &n) != EOF) {
if(m == 0)
break;
result = 1;
while(n--)
result = (result * m) % 10;
printf("%d\n", result);
}
return 0;
}
[/c]
10515 why wrong ans plz help me
Posted: Tue Nov 04, 2003 8:20 am
by bayzid
#include<stdio.h>
void main()
{
long int m,n; char c;
int l,p,a2[4]={6,2,4,8},a3[4]={1,3,9,7},a4[2]={6,4},a7[4]={1,7,9,3},a8[4]={6,8,4,2},a9[2]={1,9};
int mod2,mod4;
while(2)
{
while((c=getchar())!=' '&&c!='\n'&&c!='\t') m=c-48;
while((c=getchar())!=' '&&c!='\n'&&c!='\t') n=c-48;
if(m==0&&n==0)
break;
mod2=n%2;
mod4=n%4;
p=m%10;
if(n==0)
l=1;
else if(p==0||p==1||p==5||p==6)
l=p;
else if(p==2)
l=a2[mod4];
else if(p==3)
l=a3[mod4];
else if(p==4)
l=a4[mod2];
else if(p==7)
l=a7[mod4];
else if(p==8)
l=a8[mod4];
else if(p==9)
l=a9[mod2];
printf("%d\n",l);
}
}
Posted: Tue Nov 04, 2003 10:35 am
by Towhid
Your idea is wrong. 10^101 is a quite big number having 100 digits. It can't be fitted in integer.
Posted: Wed Nov 05, 2003 10:05 am
by Towhid
Try the inputs
2 12
3 10
and match them with original output.
Find the outputs yourself

Posted: Wed Nov 05, 2003 5:38 pm
by osan
Dear bayzid
Input
Your output
Output should be
I think you should hold at least last 2 digit of n.
Your code
Code: Select all
m=7 & n=1;
1%4= 1;
For the m=7 & n=1
Result = 7.
My code
Code: Select all
m=7 & n=11
11%4=3;
For m=7 & n=3
Result = 3.
i think you got that whatever i wanna say
Please remove your code from the forum
Good Luck!!!!
help me about 10515 h to get input two digit of n at a time
Posted: Mon Nov 10, 2003 12:05 am
by bayzid
help me how to get input two digit at a time in the below of code.
because input is 10^101.i think this is not possible to handle in array.
while((c=getchar())!=' '&&c!='\n'&&c!='\t') n=c-48;