Cod sursa(job #1529549)

Utilizator tamionvTamio Vesa Nakajima tamionv Data 21 noiembrie 2015 00:22:41
Problema Indep Scor 15
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.4 kb
#include <fstream>
#include <array>
#include <algorithm>
using namespace std;

class bignum{
	static constexpr int baza = 1000000000;
	int sz;
	array<int, 30> cifre;
public:
	bignum(): sz(0), cifre({}){}
	friend ofstream& operator<<(ofstream& lhs, const bignum& rhs);
	void operator+=(const bignum& rhs){
		long long x = 0;
		for(int i = 0; i < 30; ++i){
			x = cifre[i] + rhs.cifre[i];
			cifre[i] = x%baza;
			x /= baza; }
		for(int i = 0; i < 30; ++i){
			if(cifre[i] != 0){
				sz = i+1; } } }
	void operator+=(const long long rhs){
		long long x = rhs;
		for(int i = 0; i < 30; ++i){
			x += cifre[i];
			cifre[i] = x%baza;
			x /= baza; }
		for(int i = 0; i < 30; ++i){
			if(cifre[i] != 0){
				sz = i+1; } } } };

ofstream& operator<<(ofstream& lhs, const bignum& rhs){
	string str;
	for(int i = 0; i <= rhs.sz; ++i){
		for(int j = rhs.cifre[i]; j; j /= 10){
			str.push_back(j%10 + '0'); } }
	reverse(begin(str), end(str));
	lhs << str;
	return lhs; }

constexpr int gcd(const int a, const int b){
	return b == 0 ? a : gcd(b, a%b); }

void add_num(const int x, array<bignum, 1001>& info){
	static array<bignum, 1001> tmp;
	tmp = info;
	for(int i = 1; i <= 1000; ++i){
		info[gcd(i, x)] += tmp[i]; }
	info[x] += 1; }

int main(){
	ifstream f("indep.in");
	ofstream g("indep.out");
	int n;
	f >> n;
	array<bignum, 1001> info = {};
	for(int i = 0, x; i < n; ++i){
		f >> x;
		add_num(x, info); }
	g << info[1] << '\n';
	return 0; }