#include "..\testlib.h"
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cassert>

using namespace std;

FILE* out;

void openTest(int i){
    char buf[10];
    sprintf(buf,"..\\tests\\%02d",i);
    out = fopen(buf,"w");
    fprintf(stderr,"Generating %02d\n",i);
}

void closeTest(){
    fclose(out);
}

int x[110000];
int add[110000];

int cnt[3];

void gen1(){
    cnt[0]++;
    fprintf(out,"1 1 3\n");
    fprintf(out,"2 1\n");
}

void gen2(){
    cnt[2]++;
    fprintf(out,"2 2 3\n");
    fprintf(out,"20 1\n");
    fprintf(out,"10 1\n");
}

void gen51(){
    fprintf(out,"1 2 1000000000\n");
    fprintf(out,"1000000000 1000000000\n");
}

void gen52(){
    cnt[0]++;                                    
    int n = 100000,maxc = 1000000000;
    int y = rnd.next(1,maxc-2);
    int h = rnd.next(y+2,maxc);
    for (int i = 0; i < n; i++){
        add[i] = rnd.next(1,((y==1)?maxc:(h-1)/(y-1)));
        int tmp = h-add[i]*(y-1);           
        assert(tmp > 1);
        x[i] = rnd.next(1,tmp-1);      
    }
    fprintf(out,"%d %d %d\n",n,y,h);
    for (int i = 0; i < n; i++){
        fprintf(out,"%d %d\n",x[i],add[i]);
        assert(x[i] <= maxc);
    }   
}




void genany(int n,int maxc,int res){

    assert(maxc <= 1000000000); 

    if (res%3 == 0)
        res = -1;
    else if (res%3 == 1)
        res = 0;
    else
        res = 1;

    cnt[res+1]++;


    if (n <= 100)
        maxc = min(maxc, 20000);

    int y,h;

    if (res == 1){
        y = rnd.next(1,maxc);
        h = rnd.next(1,maxc);   
        for (int i = 0; i < n; i++) 
            x[i] = rnd.next(1,maxc),add[i] =rnd.next(1,maxc);
    }
    else if (res == 0){
        y = rnd.next(1,maxc-1);
        h = rnd.next(y+1,maxc);

        for (int i = 0; i < n; i++){
            add[i] = rnd.next(1,((y==1)?maxc:(h-1)/(y-1)));
            x[i] = h-add[i]*(y-1);
        }
    }
    else {
        assert(res == -1);        
        h = rnd.next(2,maxc);   
        y = rnd.next(1,h-1);
        for (int i = 0; i < n; i++) 
            x[i] = rnd.next(1,maxc),add[i] =rnd.next(1,maxc);

        for (int i = 0; i < 100; i++){
            int v = rnd.next(0,n-1);
            while (h - add[v]*1LL*(y-1) - 1 < 1)
                add[v] = rnd.next(1,(y == 1)?(h-1):((h-1)/(y-1)));
            x[v] = rnd.next(1,h - add[v]*(y-1) - 1);
            assert(x[v] <= maxc);
        }
    }

    fprintf(out,"%d %d %d\n",n,y,h);
    for (int i = 0; i < n; i++){
        fprintf(out,"%d %d\n",x[i],add[i]);
        assert(x[i] <= maxc);
    }
}


int main(int argc,char** argv){
    registerGen(argc,argv);
    cnt[0] = cnt[1] = cnt[2] = 0;
    system("mkdir ..\\tests 2>nul");
    for (int i = 1; i <= 52; i++){  
        openTest(i);
        switch (i) {
            case 1: gen1();
                    break;
            case 2: gen2();
                    break;
            case 51: gen51();
                    break;  
            case 52: gen52();
                    break;          
            default: 
                    if (i >= 40)
                        genany(100000,rnd.next(int(pow(10.0,(i%3+1)*3)-1),int(pow(10.0,(i%3+1)*3))),rnd.next(1,100));
                    else
                        genany(rnd.next(int(pow(10.0,i/10)),int(pow(10.0,i/10+2))),rnd.next(int(pow(10.0,(i%3+1)*3)-1),int(pow(10.0,(i%3+1)*3))),rnd.next(1,100));
                    break;
        }
        closeTest();
    }
    
    fprintf(stderr,"-1 - %d\n0 - %d\n1 - %d\n",cnt[0],cnt[1],cnt[2]);
    return 0;
}

