2015年4月21日 星期二

TIOJ 1156 . 5.高中運動會

我覺得我該停止寫水題._.

#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;
int main(){
    int t, ans = 0; scanf("%d", &t);
    while(t--){
        int n; scanf("%d", &n);
        ans = __gcd(ans, n);
    }
    printf("%d\n", ans);
    return 0;

}

TIOJ 1044 . [Interactive] Guess My Number

#include <cstdio>
#include "lib1044.h"
int main(void){
    int n = Initialize();
    int mm = 1, MM = n;
    while(mm < MM){
        int test = (mm+MM)/2;
        if(Guess(test))
            MM = test;
        else
            mm = test+1;
    }
    Report(mm);
    return 0;

}

TIOJ 1833 . Problem B 陽炎眩亂

#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;

int fat[100001];

void init(int n){
    for(int lx = 0;lx < n;lx++)
        fat[lx] = lx;
    return;
}

int gfat(int a){
    return (fat[a] == a) ? a : fat[a] = gfat(fat[a]) ;
}

void join(int a, int b){
    int fa = gfat(a);
    int fb = gfat(b);
    fat[fb] = fa;
    return;
}

int main(){
    int n, q; scanf("%d %d", &n, &q);
    init(n);
    char buf[100];
    while(q--){
        scanf("%s", buf);
        if(buf[0] == 'M'){
            int a, b; scanf("%d %d", &a, &b);
            join(a-1, b-1);
        }else{
            int a; scanf("%d", &a);
            printf("%d\n", gfat(a-1)+1);
        }
    }
    return 0;

}

TIOJ 1832 . Problem A 和諧的共鳴頻率 Submit Status Ranklist Back to Problems List

#include <cstdio>
#include <cstdlib>

using namespace std;

typedef long long int int64;

int64 arr[1000001];

int main(){
    int n, q; scanf("%d %d", &n, &q);
    arr[0] = 0;
    for(int lx = 1;lx <= n;lx++){
        int64 inp;
        scanf("%lld", &inp);
        arr[lx] = arr[lx-1]^inp;
    }
    while(q--){
        int a, b; scanf("%d %d", &a, &b);
        printf("%lld\n", arr[a-1]^arr[b]);
    }
    return 0;

}

TIOJ 1043 . F.名偵探蚵男


#include <cstdio>
#include <cstdlib>
typedef long long int int64;
int main(){
    int n, p;
    for(;;){
        scanf("%d %d", &n, &p);
        if(n == 0 and p == 0) break;
        int64 cc[10001][2] = {{1}, {0}};
        int now = 0;
        for(int lx = 0;lx < n;lx++){
            int64 inp; scanf("%lld", &inp);
            for(int y = 0;y <= p;y++)
                cc[y][1^now] = 0;
            for(int y = 0;y <= p;y++)
                for(int k = 0;y+inp*k <= p;k++)
                    cc[y+inp*k][1^now] += cc[y][now];
            now ^= 1;
        }
        printf("%lld\n", cc[p][now]);
    }
    return 0;
}

TIOJ 1040 . C.連分數


#include <cstdio>
#include <cstdlib>

void dfs(int a, int b){
    if(a%b == 0){
        printf("%d", a/b);
        return;
    }
    printf("%d+1/",a/b);
    if(b%(a%b) != 0) printf("{");
    dfs(b, a%b);
    if(b%(a%b) != 0) printf("}");
    return;
}

int main(){
    int T; scanf("%d", &T);
    while(T--){
        int a, b; scanf("%d %d", &a, &b);
        printf("%d/%d = ", a, b);
        dfs(a, b);
        puts("");
    }
    return 0;

}

TIOJ 1398 . 霍夫快編碼

n=1 special case炸爛gg

#include <cstdio>
#include <cstdlib>
#include <queue>

typedef long long int int64;

using namespace std;

int main(){
    int n; 
    while(scanf("%d", &n) == 1){
        priority_queue<int64> prc;
        for(int lx = 0;lx < n;lx++){
            int64 inp; scanf("%lld", &inp);
            prc.push(-inp);
        }
        int64 sum = 0;
        if(n == 1) sum = prc.top();
        if(n%2 == 0){
            int64 c = 0;
            c += prc.top(); prc.pop();
            c += prc.top(); prc.pop();
            prc.push(c);
            sum += c;
        }
        while(prc.size() >= 3){
            int64 c = 0;
            c += prc.top(); prc.pop();
            c += prc.top(); prc.pop();
            c += prc.top(); prc.pop();
            prc.push(c);
            sum += c;
        }
        printf("%lld\n", -sum);
    }
    return 0;

}

2015年4月17日 星期五

HOJ 401 - 完美小矩陣




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
 
using namespace std;
 
int sqrroot(int n){
    if(n < 0) return -1;
    int sqr1 = max(0, (int)(sqrt(n) - 2));
    for(int lx = sqr1;lx*lx <= n;lx++)
        if(lx*lx == n)
            return lx;
    return -1;
}
 
int main(){
    
    int n, k;
    while(scanf("%d %d", &n, &k) != EOF){
        if(n%2 == 0){
            printf("22222\n");
            for(int lx = 0;lx < n;lx++){
                for(int ly = 0;ly < n;ly++){
                    if(ly == lx-1 and ly%2 == 0) printf("1 ");
                    else if(ly == lx+1 and ly%2 == 1) printf("%d ", k);
                    else printf("0 ");
                }
                puts("");
            }
            continue;
        }
        int res = sqrroot(k);
        if(res == -1){
            puts("11111");
            continue;
        }
        puts("22222");
        for(int lx = 0;lx < n;lx++){
            for(int ly = 0;ly < n;ly++)
                printf("%d ", (lx==ly)*res);
            puts("");
        }
    }
    return 0;
}