/*
* File: main.c
* Author: xxxx
*
* Created on 2013/02/12, 12:09
*/
#include <stdio.h>
#include <stdlib.h>
#define MAXDECIMAL (24*1000000)
#define MAXBINARY (60000000)
#define TABLEWIDTH (1000000)
void makemultable();
int* makelongdecimal();
void calc(int);
void decans(void);
void clearmemory(void);
void dispans(void);
void dispans2(int);
char *b;
int *ans;
int blength = 0, anslength = 1;
int multable[TABLEWIDTH][2];// 2倍しかしない
int main(int argc, char** argv) {
printf("start makemultable\n"); makemultable();
printf("start makelongdecimal(ans)\n"); ans = makelongdecimal();
printf("start calc(578851)\n"); calc(578851);
decans();
dispans();
printf("start clearmemory\n"); clearmemory();
return (EXIT_SUCCESS);
}
void makemultable(void){
int j, val;
for(j = 0; j < TABLEWIDTH; j++){
val = 2 * j;
if(val > TABLEWIDTH){
val -= TABLEWIDTH;
multable[j][1] = 1;
} else
multable[j][1] = 0;
multable[j][0] = val;
}
}
int* makelongdecimal(){
int *ptr;
ptr
= (int*)calloc(MAXDECIMAL
/ 6, sizeof(int)); if(ptr == NULL){
fprintf(stderr
, "Can't allocate memory!\n"); }
return ptr;
}
void calc(int val){
int i, j, flg = 0, *base, tmp;
*ans = 1;
for(j = 0; j < val; j++){
//printf("Loop %d: ", j); dispans();
// ansを2倍する
base = ans;
for(i = 0; i < anslength; i++) {
tmp = multable[*base][0]; // *base = ans * 2; (6桁のみ)
if(flg == 1)
++tmp;
flg = multable[*base][1];
if(flg == 1 && i == anslength - 1) //最上位桁があふれしたらansを一桁増やす
++anslength;
*base = tmp;
++base;
}
}
}
void decans(void){
int i, *base = ans;
for(i = 0; i < anslength; i++){
if(*base == 0)
*base = 999999;
else{
*base -= 1;
break;
}
++base;
}
}
void dispans(){
int i, k;
for(i = anslength - 1; i >= 0; --i){
k = *(ans + i);
if(k > 9999){
dispans2(k / 10000);
k %= 10000;
}else
dispans2(0);
if(k > 99){
dispans2(k / 100);
k %= 100;
}else
dispans2(0);
dispans2(k);
}
printf(" length:%d \n", anslength
); }
void dispans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W50Zigic3RhcnQgY2xlYXJtZW1vcnlcbiIpOwogICAgY2xlYXJtZW1vcnkoKTsKICAgIHJldHVybiAoRVhJVF9TVUNDRVNTKTsKfQp2b2lkIG1ha2VtdWx0YWJsZSh2b2lkKXsKICAgIGludCBqLCB2YWw7CiAgICAgICAgZm9yKGogPSAwOyBqIDwgVEFCTEVXSURUSDsgaisrKXsKICAgICAgICAgICAgdmFsID0gMiAqIGo7CiAgICAgICAgICAgIGlmKHZhbCA+IFRBQkxFV0lEVEgpewogICAgICAgICAgICAgICAgdmFsIC09IFRBQkxFV0lEVEg7CiAgICAgICAgICAgICAgICBtdWx0YWJsZVtqXVsxXSA9IDE7CiAgICAgICAgICAgIH0gZWxzZQogICAgICAgICAgICAgICAgbXVsdGFibGVbal1bMV0gPSAwOwogICAgICAgICAgICBtdWx0YWJsZVtqXVswXSA9IHZhbDsKICAgICAgICB9Cn0KaW50KiBtYWtlbG9uZ2RlY2ltYWwoKXsKICAgIGludCAqcHRyOwogICAgcHRyID0gKGludCopY2FsbG9jKE1BWERFQ0lNQUwgLyA2LCBzaXplb2YoaW50KSk7CiAgICBpZihwdHIgPT0gTlVMTCl7CiAgICAgICAgZnByaW50ZihzdGRlcnIsICJDYW4ndCBhbGxvY2F0ZSBtZW1vcnkhXG4iKTsKICAgICAgICBleGl0KC0xKTsKICAgIH0KICAgIHJldHVybiBwdHI7Cn0Kdm9pZCBjYWxjKGludCB2YWwpewogICAgaW50IGksIGosIGZsZyA9IDAsICpiYXNlLCB0bXA7CiAgICAqYW5zID0gMTsKICAgIGZvcihqID0gMDsgaiA8IHZhbDsgaisrKXsKICAgICAgICAvL3ByaW50ZigiTG9vcCAlZDogIiwgaik7IGRpc3BhbnMoKTsKICAgICAgICAvLyBhbnPjgpIy5YCN44GZ44KLCiAgICAgICAgYmFzZSA9IGFuczsKICAgICAgICBmb3IoaSA9IDA7IGkgPCBhbnNsZW5ndGg7IGkrKykgewogICAgICAgICAgICAgICAgdG1wID0gbXVsdGFibGVbKmJhc2VdWzBdOyAgLy8gKmJhc2UgPSBhbnMgKiAyOyAoNuahgeOBruOBvykKICAgICAgICAgICAgICAgIGlmKGZsZyA9PSAxKQogICAgICAgICAgICAgICAgICAgICAgICArK3RtcDsKICAgICAgICAgICAgICAgIGZsZyAgID0gbXVsdGFibGVbKmJhc2VdWzFdOwogICAgICAgICAgICAgICAgaWYoZmxnID09IDEgJiYgaSA9PSBhbnNsZW5ndGggLSAxKSAvL+acgOS4iuS9jeahgeOBjOOBguOBteOCjOOBl+OBn+OCiWFuc+OCkuS4gOahgeWil+OChOOBmQogICAgICAgICAgICAgICAgICAgICAgICArK2Fuc2xlbmd0aDsKICAgICAgICAgICAgICAgICpiYXNlID0gdG1wOwogICAgICAgICAgICAgICAgKytiYXNlOwogICAgICAgIH0KICAgIH0KfQp2b2lkIGRlY2Fucyh2b2lkKXsKICAgIGludCBpLCAqYmFzZSA9IGFuczsKICAgIGZvcihpID0gMDsgaSA8IGFuc2xlbmd0aDsgaSsrKXsKICAgICAgICAgICAgaWYoKmJhc2UgPT0gMCkKICAgICAgICAgICAgICAgICpiYXNlID0gOTk5OTk5OwogICAgICAgICAgICBlbHNlewogICAgICAgICAgICAgICAgKmJhc2UgLT0gMTsKICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICB9CiAgICAgICAgICAgICsrYmFzZTsKICAgIH0KfQp2b2lkIGRpc3BhbnMoKXsKICAgIGludCBpLCBrOwogICAgZm9yKGkgPSBhbnNsZW5ndGggLSAxOyBpID49IDA7IC0taSl7CiAgICAgICAgayA9ICooYW5zICsgaSk7CiAgICAgICAgaWYoayA+IDk5OTkpewogICAgICAgICAgICBkaXNwYW5zMihrIC8gMTAwMDApOwogICAgICAgICAgICBrICU9IDEwMDAwOwogICAgICAgIH1lbHNlCiAgICAgICAgICAgIGRpc3BhbnMyKDApOwogICAgICAgIGlmKGsgPiA5OSl7CiAgICAgICAgICAgIGRpc3BhbnMyKGsgLyAxMDApOwogICAgICAgICAgICBrICU9IDEwMDsKICAgICAgICB9ZWxzZQogICAgICAgICAgICBkaXNwYW5zMigwKTsKICAgICAgICBkaXNwYW5zMihrKTsKICAgIH0KICAgIHByaW50ZigiIGxlbmd0aDolZCBcbiIsIGFuc2xlbmd0aCk7Cn0Kdm9pZCBkaXNwYW5zMihpbnQgdmFsKXsKICAgIGlmKHZhbCA+IDkpewogICAgICAgIHByaW50ZigiJWQiLCB2YWwgLyAxMCk7CiAgICAgICAgdmFsICU9IDEwOwogICAgfWVsc2UKICAgICAgICBwcmludGYoIjAiKTsKICAgIHByaW50ZigiJWQiLCB2YWwpOwp9CnZvaWQgY2xlYXJtZW1vcnkodm9pZCl7CiAgICBmcmVlKGIpOwogICAgZnJlZShhbnMpOwp9