Niestety, nie byliśmy w stanie w pełni poprawnie wyświetlić tego pliku, ponieważ nie jest zakodowany w UTF-8.
Możesz pobrać ten plik i spróbować otworzyć go samodzielnie.
#include<iostream> #include<cmath> #include<vector> using namespace std; string generate1(int n) { string sb="1"; for(int i=0;i<n-1;i++) { sb+="+1"; } return sb; } vector<int> factorise(int n) { vector<int> result; const int sqrty = sqrt(n); int k = 2; while(n>1 && k<=sqrty) { while (n%k == 0) { result.push_back(k); n /= k; } ++k; } if(n > 1) { result.push_back(n); } return result; } int sumOfFactors(vector<int> list) { int sum = 0; for(int el : list) { sum += el; } return sum; } void printFactorisation(vector<int> values) { //only for sum of values <100 for(int i=0;i<values.size()-1;i++) { cout<<"("<<generate1(values[i])<<")*"; } cout<<"("<<generate1(values[values.size()-1])<<+")"; } void generateTwos(int n) { if(n==0) { cout<<"1"; } else if(n==1) { cout<<"(1+1)"; } else { for(int i=0;i<n-1;i++) { cout<<"(1+1)*"; } cout<<"(1+1)"; } } void printTwos(vector<int> n) { // for(int i=0;i<n.size()-1;i++) { generateTwos(n[i]); cout<<"+"; } generateTwos(n[n.size()-1]); } void printResult(int n) { if(n<=100) { //print 1+1+1... cout<<generate1(n); } else { vector<int> factors = factorise(n); int sum = 0; for(int arg : factors) { sum += arg; } if(sum <= 100) { printFactorisation(factors); } else { int add=1; const int n2 = n; n--; while(add<100) { factors = factorise(n); int x = sumOfFactors(factors) + add; if(x <= 100) { printFactorisation(factors); cout<<"+("; cout<<generate1(add); cout<<")"; return; } add++; n--; } //dw�jkami spr�bujmy n = n2; // np. n2=1009 int x; int count_x; vector<int> twos; int suma = 0; while(n!=0 && suma<100) { // x=1; count_x=0; while(x < n) { x<<=1; count_x++; } if(x > n) { x>>=1; count_x--; } twos.push_back(count_x); suma+=x; n=n-x; } if(n==0) { printTwos(twos); cout<<"\n"; } else { cout<<"NIE"; } } } } int main() { ios_base::sync_with_stdio(false); int t; cin>>t; vector<int> tab(t); for(int i=0;i<t;i++) { cin>>tab[i]; } for(int arg : tab) { printResult(arg); cout<<"\n"; } return 0; }
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | #include<iostream> #include<cmath> #include<vector> using namespace std; string generate1(int n) { string sb="1"; for(int i=0;i<n-1;i++) { sb+="+1"; } return sb; } vector<int> factorise(int n) { vector<int> result; const int sqrty = sqrt(n); int k = 2; while(n>1 && k<=sqrty) { while (n%k == 0) { result.push_back(k); n /= k; } ++k; } if(n > 1) { result.push_back(n); } return result; } int sumOfFactors(vector<int> list) { int sum = 0; for(int el : list) { sum += el; } return sum; } void printFactorisation(vector<int> values) { //only for sum of values <100 for(int i=0;i<values.size()-1;i++) { cout<<"("<<generate1(values[i])<<")*"; } cout<<"("<<generate1(values[values.size()-1])<<+")"; } void generateTwos(int n) { if(n==0) { cout<<"1"; } else if(n==1) { cout<<"(1+1)"; } else { for(int i=0;i<n-1;i++) { cout<<"(1+1)*"; } cout<<"(1+1)"; } } void printTwos(vector<int> n) { // for(int i=0;i<n.size()-1;i++) { generateTwos(n[i]); cout<<"+"; } generateTwos(n[n.size()-1]); } void printResult(int n) { if(n<=100) { //print 1+1+1... cout<<generate1(n); } else { vector<int> factors = factorise(n); int sum = 0; for(int arg : factors) { sum += arg; } if(sum <= 100) { printFactorisation(factors); } else { int add=1; const int n2 = n; n--; while(add<100) { factors = factorise(n); int x = sumOfFactors(factors) + add; if(x <= 100) { printFactorisation(factors); cout<<"+("; cout<<generate1(add); cout<<")"; return; } add++; n--; } //dw�jkami spr�bujmy n = n2; // np. n2=1009 int x; int count_x; vector<int> twos; int suma = 0; while(n!=0 && suma<100) { // x=1; count_x=0; while(x < n) { x<<=1; count_x++; } if(x > n) { x>>=1; count_x--; } twos.push_back(count_x); suma+=x; n=n-x; } if(n==0) { printTwos(twos); cout<<"\n"; } else { cout<<"NIE"; } } } } int main() { ios_base::sync_with_stdio(false); int t; cin>>t; vector<int> tab(t); for(int i=0;i<t;i++) { cin>>tab[i]; } for(int arg : tab) { printResult(arg); cout<<"\n"; } return 0; } |