AcWing 90. 64位整数乘法 c++和Java

18 篇文章 0 订阅
17 篇文章 0 订阅

题目

求 a 乘 b 对 p 取模的值。

输入格式
第一行输入整数a,第二行输入整数b,第三行输入整数p。

输出格式
输出一个整数,表示 a * b mod p 的值。

数据范围
1 ≤ a,b,p ≤ 1018

输入样例:

3
4
5

输出样例:

2

解题思路

暴力

首先最容易想到的是直接计算 a * b mod p,但由于数据范围非常大,这会导致没有数据类型能够装下 a * b,所以需要将中间结果限制在一定范围内。(代码略)

数学优化

由数论中的结论可知:
(A * B) % p = (A % p * B % p) % p

且乘法可以转化成加法,即通过循环每次加 a,共循环 b 次来达到乘 b 的效果

java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Main {
	
	public static void main(String[] args) throws IOException {
		InputReader in = new InputReader(System.in);
		PrintWriter out = new PrintWriter(System.out);
		
		long a = in.nextLong();
		long b = in.nextLong();
		long p = in.nextLong();
		
		long res = 0;

		for(int i = 0; i < b; i++) {
			res += a;
			res %= p;
		}
		
		out.print(res);
		out.flush();
	}
	
	public static class InputReader{
		BufferedReader reader;
		StringTokenizer tokenizer;
		
		public InputReader(InputStream in) {
			reader = new BufferedReader(new InputStreamReader(in));
		}
		
		public String next() throws IOException {
			while(tokenizer == null || !tokenizer.hasMoreTokens()) {
				tokenizer = new StringTokenizer(reader.readLine());
			}
			return tokenizer.nextToken();
		}
		
		public Long nextLong() throws IOException {
			return Long.parseLong(next());
		}
	}

}

c++

#include<bits/stdc++.h>

using namespace std;

int main()
{
    long long a, b, p;
    scanf("%lld%lld%lld", &a, &b, &p);

    long long res = 0;

    for(int i = 0; i < b; i++)
    {
        res += a;
        res %= p;
    }

    printf("%lld", res);
    return 0;
}

但此时由于 b 的值范围非常大,而该思想时间复杂度又为O(n),所以在最坏情况下必然 TLE。

二进制优化

例如计算 3 ∗ 5 3 * 5 35

首先将 5 转成二进制数 0101B
5 = 2 2 + 2 0 5 = 2^{2} + 2^{0} 5=22+20

所以原式可以化成下式:
3 ∗ 5 = 3 ∗ 2 2 + 3 ∗ 2 0 3 * 5 = 3 * 2^{2} + 3 * 2^{0} 35=322+320

而根据(A + B) % p = (A % p * B % p) % p
2 n   m o d   p = 2 n − 1   m o d   p ∗ 2   m o d   p 2^{n} \bmod p = 2^{n-1} \bmod p * 2 \bmod p 2nmodp=2n1modp2modp

这不仅可以将每次加的数控制在允许范围内,还可以将时间复杂度降低到O(logn)。

AC代码

Java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Main90 {
	
	public static void main(String[] args) throws IOException {
		InputReader in = new InputReader(System.in);
		PrintWriter out = new PrintWriter(System.out);
		
		long a = in.nextLong();
		long b = in.nextLong();
		long p = in.nextLong();
		
		long res = 0;
		
		while(b != 0) {
			if((b & 1) == 1) {
				res += a;
				res %= p;
			}
			
			a %= p;
			a = (2 * a) % p;
			b >>= 1;
		}
		
		out.print(res);
		out.flush();
	}
	
	public static class InputReader{
		BufferedReader reader;
		StringTokenizer tokenizer;
		
		public InputReader(InputStream in) {
			reader = new BufferedReader(new InputStreamReader(in));
		}
		
		public String next() throws IOException {
			while(tokenizer == null || !tokenizer.hasMoreTokens()) {
				tokenizer = new StringTokenizer(reader.readLine());
			}
			return tokenizer.nextToken();
		}
		
		public Long nextLong() throws IOException {
			return Long.parseLong(next());
		}
	}

}

c++

#include<bits/stdc++.h>

using namespace std;

int main()
{
    long long a, b, p;
    scanf("%lld%lld%lld", &a, &b, &p);

    long long res = 0;

    while(b)
    {
        if(b & 1)
        {
            res += a;
            res %= p;
        }
        a %= p;
        a = (2 * a) % p;
        b >>= 1;
    }

    printf("%lld", res);
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值