首页 > 其他分享 >P1303 A*B Problem

P1303 A*B Problem

时间:2024-04-13 15:55:48浏览次数:17  
标签:node 10 int len times P1303 Problem 2010

P1303 A*B Problem

题目

给出两个非负整数,求它们的乘积。

输入

输入共两行,每行一个非负整数。

输出

输出一个非负整数表示乘积。

样例

输入

1 
2

输出

2

提示

每个非负整数不超过 \(10^{2000}\)。


思路

根据题意,乘数的数据最大范围是 \(10^{2000}\),需要使用高精度乘高精度的算法。将每个乘数以字符串读取并倒序存储在整型数组中,参考竖式乘法运算的过程进行计算。

设两个乘数分别为 \(a_3a_2a_1\) 和 \(b_2b_1\),可以得到以下竖式

其中:

\[c_{11}=b_1 \times a_1,c_{12} = b_1 \times a_2,c_{13}=b_1 \times a_3 \]

\[c_{21}=b_2 \times a_1,c_{22}=b_2 \times a_2,c_{23}=b_2 \times a_3 \]

\[c_1=c_{11},c_2=c_{12}+c_{21},c_3=c_{13}+c_{22},c_4=c_{23} \]

做完乘法后可以总结出规律:乘积第 \((i+j-1)\) 位的值为 \(c_{i+j-1}=\)“\((\sum (a_i \times b_j))/10+\)进位”,其中 \(1 \le i \le lena,1 \le j \le lenb\);乘积位 \(c_{i+j-1}\) 计算后大于等于 \(10\),则向 \(c_{i+j}\) 进位,\(c_{i+j-1}\) 位留下除以 \(10\) 的余数;乘积的长度最大值为 \(lena\) 与 \(lenb\) 的和,另外在乘积输出前需要将前导零去掉。

for (int i = 1; i <= lena; i ++ )
{
    x = 0;
    for (int j = 1; j <= lenb; j ++ )
    {
        x = a[i] * b[j] + x / 10 + c[i + j - 1];
        c[i + j - 1] = x % 10;
    }
    c[i + lenb] = x / 10;
}
lenc = lena + lenb;
while (c[lenc] == 0 && lenc > 1) // 去掉多余的前导 0
    lenc --;

代码一

#include <bits/stdc++.h>

using namespace std;

char a1[2010], b1[2010];
int a[2010], b[2010], c[4000010], lena, lenb, lenc, x;

void read()
{
	scanf("%s %s", a1, b1);
	lena = strlen(a1);
	lenb = strlen(b1);
	for (int i = 0; i <= lena - 1; i ++ )
		a[lena - i] = a1[i] - 48;
	for (int i = 0; i <= lenb - 1; i ++ )
		b[lenb - i] = b1[i] - 48;
}

void mul()
{
	for (int i = 1; i <= lena; i ++ )
	{
		x = 0;
		for (int j = 1; j <= lenb; j ++ )
		{
			x = a[i] * b[j] + x / 10 + c[i + j - 1];
			c[i + j - 1] = x % 10;
		}
		c[i + lenb] = x / 10;
	}
	lenc = lena + lenb;
	while (c[lenc] == 0 && lenc > 1) // 去掉前导 0
		lenc --;
}

void print()
{
	for (int i = lenc; i >= 1; i -- )
		cout << c[i];
}

int main()
{
	read();
	mul();
	print();
	return 0;
}

代码二:重载运算符

#include <bits/stdc++.h>

using namespace std;

char str[2010];

struct node
{
	int len, s[4010];
	node()
	{
		len = 0;
		memset(s, 0, sizeof(s));
	}
};

node operator * (const node &a, const node &b)
{
	node c;
	c.len = a.len + b.len - 1;
	if ((a.len == 1 && a.s[1] == 0) || (b.len == 1 && b.s[1] == 0))
	{
		c.len = 1;
		c.s[1] = 0;
		return c;
	}
	for (int i = 1; i <= a.len; i ++ )
	{
		for (int j = 1; j <= b.len; j ++ )
		{
			c.s[i + j - 1] += a.s[i] * b.s[j];
			c.s[i + j] += c.s[i + j - 1] / 10;
			c.s[i + j - 1] %= 10;
		}
	}
	if (c.s[c.len + 1])
	{
		int x = c.s[c.len + 1], len = c.len;
		while (x)
		{
			c.s[++ len] = x % 10;
			x /= 10;
		}
		c.len = len;
	}
	return c;
}

node read()
{
	scanf("%s", str);
	int len = strlen(str);
	node a;
	a.len = len;
	for (int i = 0; i < len; i ++ )
		a.s[len - i] = str[i] - '0';
	return a;
}

void print(node a)
{
	for (int i = a.len; i >= 1; i -- )
		printf("%d", a.s[i]);
}

int main()
{
	node a, b, c;
	a = read();
	b = read();
	c = a * b;
	print(c);
	return 0;
}

标签:node,10,int,len,times,P1303,Problem,2010
From: https://www.cnblogs.com/IronMan-PZX/p/18132969

相关文章

  • CF1618G Trader Problem 题解
    CF1618GTraderProblem题解题目链接:CF|洛谷提供一个在线做法。分析1我们不妨把\(a\)和\(b\)合并为一个序列,称合并后的序列为\(c\),并将其不降序排序。把玩样例后不难发现:对于一个物品序列\(c_1,c_2,\cdots,c_l\),满足\(\foralli<l,c_{i+1}-c_i\lek\)(即任意......
  • 52 Things: Number 11: What are the DLP, CDH and DDH problems?
    52Things:Number11:WhataretheDLP,CDHandDDHproblems?52件事:数字11:DLP、CDH和DDH问题是什么? Thisisthelatestinaseriesofblogpoststoaddressthelistof'52ThingsEveryPhDStudentShouldKnowToDoCryptography':asetofquestion......
  • 52 Things: Number 10: What is the difference between the RSA and strong-RSA prob
    52Things:Number10:WhatisthedifferencebetweentheRSAandstrong-RSAproblem?52件事:数字10:RSA和强RSA问题有什么区别?Thisisthelatestinaseriesofblogpoststoaddressthelistof'52ThingsEveryPhDStudentShouldKnowToDoCryptography......
  • CF1748E Yet Another Array Counting Problem の Solution
    Link有些人还是啥都不会。看到题目应该能想到这是笛卡尔树的性质,因为每一对\((l,r)\)都满足最左端最大值位置相同,所以说明在笛卡尔树上,每一对点的lca相同,说明\(a\)和\(b\)序列的笛卡尔树相同。我们以下标为键,\(a_i\)为值建立大根笛卡尔树,现在题目就转换成在这个树上填......
  • P1865 A % B Problem
    原题链接题解1.筛一筛就行code#include<bits/stdc++.h>usingnamespacestd;intheshu[1000006]={0},f[1000007]={0};intmain(){intn,m;cin>>n>>m;for(longlongi=2;i<=m;i++){if(!heshu[i])for(longlongk=i*i;k<=m;......
  • Randomness Is All You Need: Semantic Traversal of Problem-Solution Spaces with L
    本文是LLM系列文章,针对《RandomnessIsAllYouNeed:SemanticTraversalofProblem-SolutionSpaceswithLargeLanguageModels》的翻译。随机性就是你所需要的:具有大型语言模型的问题解决空间的语义遍历摘要1引言2相关工作3模型4算法5评估6实现7结论摘......
  • CF30D King's Problem? 题解
    CF30D题意有\(n+1\)个点,其中的\(n\)个点在数轴上。求以点\(k\)为起点走过所有点的最短距离,允许重复。思路有两种情况:\(k\)在数轴上(如图1)。\(k\)在第\(n+1\)个点上(如图2)。图1:图2:像第一种情况:一定存在数轴上某点\(k\),使得人先走遍\(1\simk\),回来,再走遍......
  • CF1934B Yet Another Coin Problem 题解
    CF1934BYetAnotherCoinProblem题解题意目前有\(5\)种硬币,面值分别为\(1,3,6,10,15\)。给你一个数字\(n\),求出可以凑出\(n\)的最少的硬币的数量。思路这道题,大多数的人大概会想到动态规划的方法。但是,我们应该有敢于创新的精神。于是我就想到了一个简单的数学方法......
  • 【2021.6.26 NOI模拟】Problem B. 简单题 another solution
    ProblemDescriptionInput从文件b.in中读入数据。一个正整数n。Output输出到文件b.out中。一个整数表示答案。SampleDataInput#1Copy5Output#1Copy31Input#2Copy50Output#2Copy2885DataConstraint首先,我们从小到大枚举\(n\),假设当前枚举......
  • cURL error 60: SSL certificate problem: unable to get local issuer certificate
    阿里云短信window报cURLerror60:SSLcertificateproblem:unabletogetlocalissuercertificate原文链接:https://blog.csdn.net/qq_41408081/article/details/124309075序:帮客户接一个阿里云短信验证码提醒,新版的SDK,一下,折磨简单,在Windows上搞的差点心力交瘁,差点怀疑......