Cod sursa(job #1709442)

Utilizator ubb_912codersUBB Badila Bereczki Bodea ubb_912coders Data 28 mai 2016 12:18:40
Problema Consecutive Scor 0
Compilator cpp Status done
Runda ONIS 2016 - Runda - 2 - ACM ICPC Romanian Programming Contest Marime 1.38 kb
#include <fstream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;

struct S
{
	int a, b;
	bool operator ==(S X)
	{
		if((a==0 && X.a==1) || (X.a==0 && a==1))
			return true;
		return a==X.a && b==X.b;
	}
};
vector<S> v1;
int t, i, q, j;
bool comp(S a, S b)
{
	return (a.b-a.a<b.b-b.a);
}

int main()
{
	ifstream f("consecutive.in");
	ofstream g("consecutive.out");
	f>>t;
	for(i=1; i<=t; i++)
	{
		v1.clear();
		//v2.clear();
		f>>q;
		int p=sqrt(q);
		for(j=2; j<=q; j++)
		{
			if(j%2==0)
			{
				if((double)q/(double)j - (int)q/j==0.5)
				{
					int st=q/j-(j/2)+1;
					if(st<0)
						break;
					int dr=q/j+(j/2);
					if(st>=0 && dr>=0)
					{
					S A;
							A.a=st;
							A.b=dr;
						v1.push_back(A);
					}
				}
			}
			if(j%2==1 && q%j==0)
			{
				int st=q/j-j/2;
				int dr=q/j+j/2;
				if(st<0)
					break;
				if(st>=0 && dr>=0)
				{
				S A;
							A.a=st;
							A.b=dr;
						v1.push_back(A);
				}
			}
			
		}
		sort(v1.begin(), v1.end(), comp);
		int sz=0;
		for(j=0; j<v1.size(); j++)
		{
			sz++;
			while(j+1<v1.size() && v1[j]==v1[j+1])
				j++;
		}
		g<<sz<<"\n";
		for(j=0; j<v1.size(); j++)
		{
			if(v1[j].a==0)
				g<<1<<" "<<v1[j].b<<"\n";
			else
			{
			g<<v1[j].a<<" "<<v1[j].b<<"\n";
			}
			while(j+1<v1.size() && v1[j]==v1[j+1])
				j++;
		}
	}
	
}