
function codes() {
  this.n;
  this.k;
  this.rmax;
  this.nnodes;
  this.nsols;
  this.sol = new Array();
  this.anodes = new Array();
  this.knodes = new Array();
  this.tab = new Array();

  function read_data() {
    this.n=parseInt(document.ndes.n.value);
    this.k=parseInt(document.ndes.k.value);
    this.rmax = Math.floor((this.n*this.k -1)/(this.k -1));
    this.nnodes=(this.rmax * (2 * this.k*this.n - (this.k - 1) * (this.rmax + 1))) / 2;
    var s= document.ndes.c.value;
    s = s.replace(/[,;]+/g," ");
    s = s.replace(/\s+/g," ");
    s = s.replace(/^\s+/g,"");
    s = s.replace(/\s+$/g,"");
    this.sol = s.split(/\s/);
    this.nsols = this.sol.length;
  }
  this.read_data = read_data;

  function set_nodes() {
    var nv=0, i, j, v, k=this.k, kn= this.k * this.n;
    for (i = 1; i <= this.rmax; i++) {
        v = kn- (k - 1) * i;
        for (j = 0; j < v; j++) {
            this.knodes[nv] = i;
            this.anodes[nv] = j;
            nv++;
        }
    }
  }
  this.set_nodes=set_nodes;

  function solution() {
    var prog="", rais="", pav="", nd, a, r, i,j;
    for(i=0;i<this.k * this.n;i++)
      this.tab[i] = "_";

    for(i=0;i<this.sol.length;i++) {
      nd = parseInt(this.sol[i]);
      a = this.anodes[nd-1];
      r = this.knodes[nd-1];
      rais += r +" ";
      prog += "(";
      for(j=0;j< this.k; j++) {
        prog += a+j*r;
        this.tab[a+j*r] = i;
        if(j<this.k-1)
          prog +=","
        else
          prog +=") ";
      }
      
    }
//    alert(sr)
//    alert(prog)
    for(i=0;i<this.k * this.n;i++)
      pav += this.tab[i]+" ";
//    alert(pav)
    document.ndes.t.value = "Pavage :\n";
    document.ndes.t.value += pav + "\n";
    document.ndes.t.value += "Raisons :\n";
    document.ndes.t.value += rais +"\n";
    document.ndes.t.value += "Progressions :\n";
    document.ndes.t.value += prog + "\n";
    
  }
  this.solution = solution;

  function cherche() {
    this.read_data();
    this.set_nodes();
    this.solution();
  }
  this.cherche = cherche;
}

function nodes_calc() {
  var n = new codes();
  n.cherche();
}

function nodes_clr() {
  document.ndes.c.value = ""
  document.ndes.t.value = ""
}

var n_exos = new Array( "5","2","5 21 33 41 45",
"5","2","5 21 33 41 45",
"10","2","10 46 78 106 130 150 166 178 186 190",
"15","2","15 71 123 171 215 255 291 323 351 375 395 411 423 431 435",
"20","2","20 96 168 236 300 360 416 468 516 560 600 636 668 696 720 740 756 768 776 780",
"50","2","50 246 438 626 810 990 1166 1338 1506 1670 1830 1986 2138 2286 2430 2570 2706 2838 2966 3090 3210 3326 3438 3546 3650 3750 3846 3938 4026 4110 4190 4266 4338 4406 4470 4530 4586 4638 4686 4730 4770 4806 4838 4866 4890 4910 4926 4938 4946 4950",
"7","3","14 36 40 52 67 81 98",
"8","3","15 45 97 64 125 42 114 127",
"9","3","2 48 64 74 95 113 143 150 154",
"10","3","202 209 80 33 183 149 198 134 112 75",
"15","3","30 56 119 126 189 201 237 307 384 385 430 439 467 473 477",
"20","3","57 101 212 222 286 373 441 490 509 539 565 608 664 703 717 756 766 793 822 833",
"15","4","10 59 159 182 245 264 336 354 401 438 481 523 532 541 566",
"16","4","69 172 175 243 309 357 387 406 531 549 575 599 603 620 635 646",
"17","4","57 98 134 344 346 395 459 491 546 586 637 643 692 699 720 727",
"18","4","58 133 140 226 265 359 389 513 526 559 600 673 694 739 768 807 827",
"28","5","130 150 269 508 560 657 761 946 1076 1104 1227 1365 1427 1467 1602 1691 1711 1811 1874 1928 1981 2024 2143 2166 2277 2378",
"32","5","7 171 453 727 862 944 1022 1160 1294 1603 1635 1863 1965 2017 2192 2272 2350 2380 2464 2540 2639 2646 2713 2837 2945 2992 3034 3039 3108",
"20","6","10 224 244 372 498 615 617 739 847 995 1161 1227 1241 1295 1334 1378",
"30","6","2140 174 1260 183 660 828 3122 3068 2629 3017 689 2881 2825 1006 1651 3111 1442 2407 2323 2521 1833 477 2278 2745",
"20","7","131 247 264 490 531 614 884 959 993 1085 1174 1218 1424 1425 1483 1527",
"30","8","229 238 680 913 1187 1449 1656 1869 2296 2407 2612 2670 2934 3135 3251 3494 3586 3808 3861 3886 3944 3965"
);

function n_exple(n) {
  document.ndes.n.value = n_exos[3*n];
  document.ndes.k.value = n_exos[3*n+1];
  document.ndes.c.value = n_exos[3*n+2];
  document.ndes.t.value = "";
}

