概述
Sample Input
11111111111111 1111111111
Sample Output
12345679011110987654321
import static java.lang.Math.PI;
import static java.lang.Math.cos;
import static java.lang.Math.sin;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) {
new POJ2389().main();
}
}
class POJ2389 {
InputReader in = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
final int maxn = 111;
class complex {
double r, i;
complex(double _r, double _i) {
this.r = _r;
this.i = _i;
}
complex add(complex b) {
return new complex(r + b.r, i + b.i);
}
complex sub(complex b) {
return new complex(r - b.r, i - b.i);
}
complex mult(complex b) {
return new complex(r * b.r - i * b.i, r * b.i + i * b.r);
}
};
void change(complex[] x, int len) {
for (int i = 1, j = len / 2; i < len - 1; i++) {
if (i < j) {
complex t = x[i];
x[i] = x[j];
x[j] = t;
}
int k = len / 2;
while (j >= k) {
j -= k;
k /= 2;
}
if (j < k)
j += k;
}
}
void fft(complex[] x, int len, int sta) {
change(x, len);
for (int m = 2; m <= len; m <<= 1) {
complex Wn = new complex(cos(-sta * 2 * PI / m), sin(-sta * 2 * PI / m));
for (int i = 0; i < len; i += m) {
complex W = new complex(1, 0);
for (int j = i; j < i + m / 2; j++) {
complex x1 = x[j], x2 = W.mult(x[j + m / 2]);
x[j] = x1.add(x2);
x[j + m / 2] = x1.sub(x2);
W = W.mult(Wn);
}
}
}
if (sta == -1)
for (int i = 0; i < len; i++)
x[i].r /= len;
}
char[] s1 = new char[maxn];
char[] s2 = new char[maxn];
complex[] a = new complex[2 * maxn];
complex[] b = new complex[2 * maxn];
int[] ans = new int[2 * maxn];
void main() {
while (in.hasNext()) {
s1 = in.next().toCharArray();
s2 = in.next().toCharArray();
int len1 = s1.length, len2 = s2.length, len = 1;
while (len < len1 * 2 || len < len2 * 2)
len <<= 1;
for (int i = 0; i < len1; i++)
a[i] = new complex(s1[len1 - i - 1] - '0', 0);
for (int i = len1; i < len; i++)
a[i] = new complex(0, 0);
for (int i = 0; i < len2; i++)
b[i] = new complex(s2[len2 - i - 1] - '0', 0);
for (int i = len2; i < len; i++)
b[i] = new complex(0, 0);
fft(a, len, 1);
fft(b, len, 1);
for (int i = 0; i < len; i++)
a[i] = a[i].mult(b[i]);
fft(a, len, -1);
for (int i = 0; i < len; i++)
ans[i] = (int) (a[i].r + 0.5);
for (int i = 0; i < len; i++) {
ans[i + 1] += ans[i] / 10;
ans[i] %= 10;
}
len = len1 + len2 - 1;
while (ans[len] <= 0 && len > 0)
len--;
for (int i = len; i >= 0; i--)
out.print(ans[i]);
out.println();
}
out.flush();
}
}
class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = new StringTokenizer("");
}
private void eat(String s) {
tokenizer = new StringTokenizer(s);
}
public String nextLine() {
try {
return reader.readLine();
} catch (Exception e) {
return null;
}
}
public boolean hasNext() {
while (!tokenizer.hasMoreTokens()) {
String s = nextLine();
if (s == null)
return false;
eat(s);
}
return true;
}
public String next() {
hasNext();
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public BigInteger nextBigInteger() {
return new BigInteger(next());
}
}
Sample Input
11111111111111 1111111111
Sample Output
12345679011110987654321
最后
以上就是热情煎蛋为你收集整理的POJ2389 FFT 大数乘法的全部内容,希望文章能够帮你解决POJ2389 FFT 大数乘法所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复