UVA 138 Street Numbers

來源:互聯網
上載者:User

UVA_138

    這個題目讓我第一次過了一把打表的癮,自從悠然告訴我真正的打表法該怎麼用了以後,這是第一次實踐。

    當然後來又學了一種佩爾方程解法,根據已知我們可以化簡得到一個二元一次方程(2n+1)^2-8*m^2=1,然後找到基本解2n+1=3和m=1,依據遞推公式找到後面的10對m、n列印出來即可。

//打表程式
#include<stdio.h>
#include<string.h>
int main()
{
freopen("UVA_138(1).cpp", "w", stdout);
long long int i, j, min, mid, max;
printf("#include<stdio.h>\n");
printf("int main()\n");
printf("{\n");
for(i = 6; i <= 65918161; i ++)
{
min = 1;
max = i + 1;
for(;;)
{
mid = (min + max) / 2;
if(mid == min)
break;
if(mid * mid * 2 > i * (i + 1))
max = mid;
else
min = mid;
}
if(mid * mid * 2 == i * (i + 1))
printf("\tprintf(\"%10lld%10lld\\n\");\n", mid, i);
}
printf("\treturn 0;\n");
printf("}\n");
return 0;
}

 

#include<stdio.h>
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;
}


#include<stdio.h>
#include<string.h>
int main()
{
int i = 0, j, x1, y1, x, y, newx, newy;
x = x1 = 3, y = y1 = 1;
for(i = 0 ;i < 10; i ++)
{
newx = x * x1 + 8 * y * y1;
newy = y * x1 + x * y1;
x = newx, y = newy;
printf("%10d%10d\n", y, (x - 1) / 2);
}
return 0;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.