当前位置:网站首页>372. 超级次方
372. 超级次方
2022-08-01 07:44:00 【Mr Gao】
372. 超级次方
你的任务是计算 ab 对 1337 取模,a 是一个正整数,b 是一个非常大的正整数且会以数组形式给出。
示例 1:
输入:a = 2, b = [3]
输出:8
示例 2:
输入:a = 2, b = [1,0]
输出:1024
示例 3:
输入:a = 1, b = [4,3,3,8,5,2]
输出:1
示例 4:
输入:a = 2147483647, b = [2,0,0]
输出:1198
这题写了两种解法,解题代码如下:
int de_pow(int a,int n){
int i=0;
int p=a;
if(n==0){
return 1;
}
a=1;
for(i=0;i<n;i++){
a=a*p;
a=a%1337;
}
return a;
}
int superPow(int a, int* b, int bSize){
a=a%1337;
if(a==1){
return 1;
}
printf("%d ",a);
int i;
int p=a;
a=de_pow(a,b[i]);
for(i=1;i<bSize;i++){
a=de_pow(a,10);
// printf("--%d ",a);
// printf("a %d b[i] %d",a,b[i-1]);
a=(de_pow(p,b[i])*a)%1337;
// printf("%d ",a);
}
// printf("%d %d",a,i);
return a;
}
解法二:
int de_pow(int a,int n){
int i=0;
int p=a;
if(n==0){
return 1;
}
a=1;
for(i=0;i<n;i++){
a=a*p;
a=a%1337;
}
return a;
}
int superPow(int a, int* b, int bSize){
a=a%1337;
if(a==1){
return 1;
}
int p=a;
// printf("%d ",p);
int i;
a=de_pow(a,b[bSize-1]);
for(i=bSize-2;i>=0;i--){
p=de_pow(p,10);
// printf("%d ",p);
int bz=de_pow(p,b[i]);
// printf("%d ",a);
a=(a*bz)%1337;
}
// printf("%d %d",a,i);
return a;
}
边栏推荐
猜你喜欢
随机推荐
LabVIEW RT中的用户界面更新速度
关于App不同方式更新的测试点归纳
[Tear AHB-APB Bridge by hand]~ Why aren't the lower two bits of the AHB address bus used to represent the address?
升级为重量级锁,锁重入会导致锁释放?
Offer刷题——1
flink sql-client,怎么处理源端与目标增加端,sql-client包括映射表与JOB如
云原生FAQ
Vim简介
pytest interface automation testing framework | single/multiple parameters
监听父元素宽高,自适应插件大小
Golang:go获取url和表单属性值
Create, modify and delete tables
Go 支持 OOP: 用 struct 代替 class
Datagrip error "The specified database userpassword combination is rejected..."Solutions
好的plm软件有哪些?plm软件排行榜
C语言中编译时出现警告C4013(C语言不加函数原型产生的潜在错误)
【手撕AHB-APB Bridge】~ AHB地址总线的低两位为什么不用来表示地址呢?
表的创建、修改与删除
pytest interface automation testing framework | parametrize source code analysis
How to use Photoshop to composite star trail photos, post-processing method of night sky star trail photos