Archive for 六月, 2008
Posted on 六月 24th, 2008.
求逆序对个数,很经典了.可以归并排序求,可我不喜欢归并,所以我的做法是离散化之后用树状数组维护一下.复杂度一样,离散化也不是很难写,树状数组也巨简单.强烈推荐.
这题极其bt的是,答案是int64的…用longint存答案就WA on 14(还是12?)…
{ SGU 180; Inversions - sqybi’s code - 逆序对}//for my winstyprogram sgu180_sqybi; const nn = 65537;
type rec = record v, p: longint; end;
var n, i, t, r: longint; s: int64; a: array[1..nn]of rec; b, c: array[1..nn]of longint;
procedure qsort(l, r: longint); var i, j, d: longint; t: rec; […]
Read Full Post |
Make a Comment ( 2 so far )
Posted on 六月 10th, 2008.
给出n个数,判断它们的m次方能不能被k整除.对每个数和k分解质因数即可(注意对于每个数只需要分解k的因子).
{ SGU 117; Counting - sqybi’s code}//for my winstyprogram sgu117_sqybi; const nn = 10000;
var i, j, k, kk, l, n, m, s, t: longint; a, b, p: array[1..nn]of longint; f: boolean;
begin readln(n, m, k); kk := k; l := 0; for i:=2 to kk do begin if k = 1 then […]
Read Full Post |
Make a Comment ( None so far )
Posted on 六月 10th, 2008.
枚举一个素因子i,再判断n div i是不是素数就可以.数据太水了,我刚开始的程序有bug,枚举的不是素因子而是任意因子…我shaz…把自己的算法给忘了…刚才看了一下,枚举到的第一个因子一定是素因子(除了1最小的因子啊…),而只对这一个因子进行判断就可以…不用筛法,朴素判素就能AC.
{ SGU 113; Nearly prime numbers - sqybi’s code}//for my winstyprogram sgu113_sqybi; var f: boolean; i, times, cases, n: longint;
function prime(x: longint): boolean; var i: longint; begin prime := false; for i:=2 to trunc(sqrt(x)) do if x mod i = 0 then exit; prime := true; end;
begin readln(cases); for […]
Read Full Post |
Make a Comment ( None so far )
Posted on 六月 10th, 2008.
可以发现,一个数平方的最后9位只和这个数的最后9位有关.对1~9位数随便写个小程序枚举一下,就可发现:对于1位数到8位数,没有任何符合条件;对于9位数,有8个数符合条件;对于n位数(n>9),最高位有9种(1~9),后9位有8种,剩余n-10位每位10种(0~9),总共72*10^(n-10)种.
{SGU 107; 987654321 Problem- sqybi’s code}//for my winstyprogram sgu107_sqybi; var i, n: longint;
begin readln(n); if n <= 8 then writeln(0) else if n = 9 then writeln(8) else begin write(72); for i:=1 to n-10 do write(0); end; end.
阅读(76 次)
Read Full Post |
Make a Comment ( None so far )
Posted on 六月 8th, 2008.
找到最靠后的一个满足它左边的’(’比’)’数量多的’(’,改成’)’,然后在后面补(…()…)和)…).好久以前就写过的题.刚才写错了一点,在最后填括号的时候先填(…()…),再填)…).
{ SGU 179; Brackets light - sqybi’s code}//for my winstyprogram sgu179_sqybi; var i, l, r, tr, t: longint; s: ansistring;
begin readln(s); l := length(s); t := 0; r := 0; for i:=1 to l do begin if (t > 0) and (s[i] = ‘(’) then begin tr := t; r := i; end; […]
Read Full Post |
Make a Comment ( None so far )