UVaOJ 748 - Exponentiation


—— by A Code Rabbit


Description

输入R和n,求出R^n。


Type

Big Number


Analysis

一道各个OJ几乎都有的高精度题目。

可以直接用 Java 的 BigDecimal 类来撸。

要学习一定的 Java 基础。

两个比较少用的函数要注意:

stripTrailingZeros( ) 如函数名所意,可以用来去除尾部的零,

但是对形如0.00这样的数无效。

replaceAll( )函数可以用来把特殊的字符串替换成另一个字符串,

且可以用正则表达式,如 “^0” 的意思,就是首位0。


Solution

/*
 * UVaOJ 748
 * Exponentiation
 * by A Code Rabbit
 */

import java.util.Scanner;
import java.math.BigDecimal;

public class Main {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        while (cin.hasNext()) {
            BigDecimal r = cin.nextBigDecimal();
            int n = cin.nextInt();
            BigDecimal ans = r.pow(n).stripTrailingZeros();
            System.out.println(ans.toPlainString().replaceAll("^0", ""));
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Miller-Rabin 素性检验算法是一种基于随机化的素性检验算法。下面是一个用 C 语言实现的 Miller-Rabin 素性检验算法的示例代码: ``` #include <stdio.h> #include <stdlib.h> #include <time.h> #include <math.h> int modular_exponentiation(int base, int exponent, int modulus) { int result = 1; while (exponent > 0) { if (exponent % 2 == 1) { result = (result * base) % modulus; } base = (base * base) % modulus; exponent = exponent / 2; } return result; } int miller_rabin(int n, int k) { if (n <= 1 || n == 4) { return 0; } if (n <= 3) { return 1; } int d = n - 1; while (d % 2 == 0) { d = d / 2; } for (int i = 0; i < k; i++) { int a = 2 + rand() % (n - 3); int x = modular_exponentiation(a, d, n); if (x == 1 || x == n - 1) { continue; } int j; for (j = 0; j < (int)log2(n - 1); j++) { x = (x * x) % n; if (x == 1) { return 0; } if (x == n - 1) { break; } } if (j == (int)log2(n - 1)) { return 0; } } return 1; } int main() { srand(time(NULL)); int n, k; printf("Enter a number to check if it is prime: "); scanf("%d", &n); printf("Enter the number of iterations for the test: "); scanf("%d", &k); if (miller_rabin(n, k)) { printf("%d is probably prime.\n", n); } else { printf("%d is composite.\n", n); } return 0; } ``` 这个程序中,`modular_exponentiation` 函数用于计算模,`miller_rabin` 函数用于执行 Miller-Rabin 素性检验。在 `main` 函数中,用户输入要检验的数和测试的迭代次数,然后调用 `miller_rabin` 函数进行素性检验。最终输出结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值