加入收藏 | 设为首页 | 会员中心 | 我要投稿 温州站长网 (https://www.0577zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

POJ2389 FFT 大数乘法

发布时间:2021-01-21 01:46:13 所属栏目:大数据 来源:网络整理
导读:Sample Input 222222222211112222222222 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


Sample Input

22222222221111
2222222222

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,1);
			for (int i = 0; i < len; i++)
				a[i] = a[i].mult(b[i]);
			fft(a,-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

22222222221111
2222222222

Sample Output

12345679011110987654321

(编辑:温州站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读