UVA 138 Street Numbers

/*
题意:m个数,找出n,使得[1,n-1]和[n+1,m]所有数之和相等,输出10组数据!
*/
//打表程序
#include <cstdio>
__int64 bsearch(__int64 l, __int64 r)
{
	__int64 left = l;
	__int64 right = r + 1;
	while(left < right )
	{
		__int64 mid = left + (right - left ) / 2;
		__int64 a = (1 + mid - 1) * (mid - 1) / 2;
		__int64 b = (mid + 1 + r) * (r - mid) / 2;
		if( a == b) return mid;
		else if( a > b) right = mid;
		else left = mid + 1;
	}
	return -1;
}
int main()
{
	freopen("f://data.out", "w", stdout);
	const __int64 nMax = 10000000000;
	__int64 i;
	int k = 0;
	for(i = 1; i < nMax; ++ i)
	{
		__int64 p = bsearch(2 , i);
		if(p != -1)
		{
			++ k;
			printf("%10I64d%10I64d\n", p, i);
		}
		if(k >= 10)
			break;
	}
	return 0;
}

//主程序
#include <cstdio>
int main()
{
printf("         6         8\n");
printf("        35        49\n");
printf("       204       288\n");
printf("      1189      1681\n");
printf("      6930      9800\n");
printf("     40391     57121\n");
printf("    235416    332928\n");
printf("   1372105   1940449\n");
printf("   7997214  11309768\n");
printf("  46611179  65918161\n");
return 0;
}

你可能感兴趣的:(UVA 138 Street Numbers)