大家对指数运算都非常熟悉,定义f(n,k) = n ^ k (n的k次方), 例如f(2, 3) = 8,
f(3,4) = 81。
我们定义f(0,0) = 1。
我们的目标是给定整数n1,k1,n2,k2,n,求f(f(n1,k1),f(n2,k2)) % n。
(%是取余数运算)。
输入格式
多组数据,每组数据就一行包含5个整数n1,k1,n2,k2,n。 (0<=n1,k1,n2,k2<=1000000000, 1 <= n <=10000000)
输出格式
每组数据一行,表示最终结果。
------------------------------------------
答题说明
输入样例
1 2 3 4 5
5 4 3 2 1
输出样例
1
0
------------------------------------------
用java完成了,我觉得没有问题,运行也没问题,不过没有通过,有错误,欢迎指出
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Test3 {
public static final int N = 3;
public static void main(String[] args) {
List list = new ArrayList();
Scanner input = null;
for(int i =0;i
input = new Scanner(System.in);
int n1 = input.nextInt();
int k1 = input.nextInt();
int n2 = input.nextInt();
int k2 = input.nextInt();
int n = input.nextInt();
IntNode node = new IntNode();
node.setValues(n1, n2, n, k1, k2);
list.add(node);
}
for(IntNode node: list){
int n1 = node.getValueOfn1();
int n2 = node.getValueOfn2();
int n = node.getValueOfn();
int k1 = node.getValueOfk1();
int k2 = node.getValueOfk2();
System.out.println(node.getResult(n1, k1, n2, k2, n));
}
}
}
class IntNode{
private int n1;
private int n2;
private int n;
private int k1;
private int k2;
public void setValues(int n1,int n2,int n,int k1,int k2){
this.n1 = n1;
this.n2 = n2;
this.n = n;
this.k1 = k1;
this.k2 = k2;
}
public int getValueOfn1(){ return this.n1 ;}
public int getValueOfn2(){ return this.n2 ;}
public int getValueOfn(){ return this.n;}
public int getValueOfk1(){ return this.k1 ;}
public int getValueOfk2(){ return this.k2 ;}
public int f(int value1,int value2){
if(value2 == 0)
return 1;
return value1 * f(value1,value2 - 1);
}
public int getResult(int n1,int k1,int n2,int k2,int n){
int result=0;
result = f(f(n1,k1),f(n2,k2)) % n;
return result;
}
}
用c语言实现输入有问题,如果输入
1 2 3 4 5
5 4 3 2 1
必须再输入一个东西才能出结果 :
#include
#define N 2
typedef struct node{
int n1,n2,k1,k2,n;
}IntNode;
int f(int value1,int value2)
{
if(value2 == 0)
return 1;
return value1 * f(value1,value2-1);
}
main()
{
IntNode node[N] ;
int i=0,result;
while(i < N){
scanf("%d %d %d %d %d
",&node[i].n1,&node[i].k1,&node[i].n2,&node[i].k2,&node[i].n);
i++;
}
for(i=0;i
{
result = f(f(node[i].n1,node[i].k1),f(node[i].n2,node[i].k2)) % node[i].n;
printf("%d
",result);
}
printf("
");
}