Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 92

IDCOBAN108

#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;

int t, dp[205][205], x, f[205];

int main()
{
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> t;
while(t--){
cin >> x;
f[0]=1;
f(i,1,200) f[i]=(f[i-1]*10)%x;
dp[0][0]=0;
dp[1][0]=8%x, dp[0][1]=6%x;
f(i,2,200) dp[0][i]=(dp[0][i-1]*10+6)%x, dp[i][0]=(dp[i-1]
[0]*10+8)%x;
f(i,1,200){
f(j,1,200-i){
dp[i][j]=(dp[i][0]*f[j]+dp[0][j])%x;
}
}
int ans1=500, ans2=500;
f(i,0,200){
f(j,0,200-i){
if(dp[i][j]==0 && (i+j)!=0){
if(i+j<ans1+ans2) ans1=i, ans2=j;
}
}
}
if(ans1==500) cout<<-1<<'\n';
else{
f(i,1,ans1) cout<<8;
f(i,1,ans2) cout<<6;
cout<<'\n';
}
}
return 0;
}
TEST006
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;

int t, n, m, mx;
int dem(int n, int m, int mx){
if(n==0 || mx==0) return 1;
if(n>=pow(m,mx)) return dem(n-pow(m,mx),m,mx)+dem(n,m,mx-1);
else return dem(n,m,mx-1);
}
int main()
{
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> t;
while(t--){
cin >> n >> m;
int tmp=1, mx=-1;
while(tmp<=n) tmp*=m, mx++;
cout<<dem(n,m,mx)<<'\n';
}
return 0;
}
IDGIAITHUA002
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;

int t;
ll m, n;
ll tinh(ll m){
ll ans=0, tmp=5;
while(m>=tmp){
ans+=m/tmp;
tmp*=5;
}
return ans;
}
int main()
{
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> t;
while(t--){
cin >> n;
m=4*n-1000;
ll x=0;
while(m<=4*n+1000){
if(tinh(m)>=n){x=m; break;}
m++;
}
cout<<x<<'\n';
}
return 0;
}
IDTAPCON001
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;

ll t, n, a[100005], b[100005], c;
map<ll,int>cnt;
int main()
{
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> t;
while(t--){
cin >> n;
f(i,1,n) {cin >> a[i]; cnt[a[i]]=0;}
sort(a+1,a+n+1);
c=0;
f(i,1,n){
cnt[a[i]]++;
if(cnt[a[i]]==1) b[++c]=a[i];
}
ll ans=1;
f(i,1,c) ans=(ans*(cnt[b[i]]+1))%MOD;
cout<<(ans-1+MOD)%MOD<<" ";
c=0;
f(i,1,n) if(a[i]>=0) b[++c]=a[i];
if(c>1){
cout<<"["<<b[1]<<", ";
f(i,2,c-1) cout<<b[i]<<", ";
cout<<b[c]<<"]";
cout<<'\n';}
else if(c==1){
cout<<"["<<b[1]<<"]"<<'\n';
}
else{
cout<<"["<<a[n]<<"]"<<'\n';
}
}
return 0;
}
IDBDS005
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;

int t, n, mx;
int dem(int n, int mx){
if(n==0 || mx==1) return 1;
if(n>=mx*mx) return dem(n-mx*mx,mx)+dem(n,mx-1);
else return dem(n,mx-1);
}
int main()
{
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> t;
while(t--){
cin >> n;
mx=sqrt(n);
cout<<dem(n,mx)<<'\n';
}
return 0;
}
IDBDS007
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;

int t, n, mx;
int dem(int n, int mx){
if(n==0 || mx==0) return 1;
if(n>=pow(2,mx)) return dem(n-pow(2,mx),mx)+dem(n,mx-1);
else return dem(n,mx-1);
}
int main()
{
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> t;
while(t--){
cin >> n;
mx=log2(n);
cout<<dem(n,mx)<<'\n';
}
return 0;
}
IDSTRING001
#include<bits/stdc++.h>
using namespace std;

int n , check ;

string sc , s , a , b;

int main(){
int t;
cin >> t;
while(t--){
cin >> a >> b >> sc >> a >> b >> s;
sc.pop_back();
sc.pop_back(); sc.erase(sc.begin());
s.pop_back();s.erase(s.begin());
int lnc = sc.size() , ln = s.size() ,ans = 0;
if(lnc==0){cout << 0 << '\n' ; continue ;}
if(ln==0){cout << -1 << '\n' ; continue ;}
for(int i=0; i<=ln - lnc ; i++){
if(s[i]==sc[0]){
check = 0 ;
for(int j = 1 ; j<lnc ; ++j){
if(sc[j]!=s[i+j]){check = 1 ; break;}
}
if(check==0){cout << i << '\n' ; ans = 1 ; break ;}
}
}
if(ans==0)cout << -1 << '\n';
}
return 0;
}
IDCOBAN102
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
#define VAL 29
using namespace std;

int calc(int x, int p){


int i,t;

x = x % VAL;
p = p % (VAL - 1);
if (p == 0) return 0;
else{
t = 1;
for (i = 0; i < p; i++)
t = (t * x) % VAL;

t--;
while (t % (x - 1) != 0) t += VAL;
i = t / (x - 1);
return i;
}
}

int main()
{
int t;
cin >>t;
while(t--){
int n, v;
cin >> n;
v = calc(2, 2 * n + 1);
v = (v * calc(3, n + 1)) % VAL;
v = (v * calc(167, n + 1)) % VAL;
cout << v << endl;

}
return 0;
}
TEST002
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
using namespace std;

ll t, m, n, dp[105][105], a[105][105], x, y;
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

cin >> t;
while(t--){
cin >> m >> n;
f(i,1,m) f(j,1,n) cin >> a[i][j];
cin >> x >> y;
f(i,1,n) dp[0][i]=INT_MAX, dp[m+1][i]=INT_MAX;
f(i,1,m) if(i!=x) a[i][y]=INT_MAX;
f(i,1,m) dp[i][y]=a[i][y];
f(j,y+1,n){
f(i,1,m){
dp[i][j]=min(min(dp[i-1][j-1],dp[i][j-1]),dp[i+1][j-1])+a[i]
[j];
}
}
ll ans=INT_MAX;
f(i,1,m) ans=min(ans,dp[i][n]);
cout<<ans<<'\n';
}
return 0;
}
IDBDS008
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;

ll tinh(int n){
if(n==0) return 1;
if(n%2) return (2*tinh(n-1))%MOD;
ll tmp=tinh(n/2);
return (tmp*tmp)%MOD;
}
int main()
{
int t;
cin >> t;
while(t--){
long long n;
cin >> n;
if(n%2) cout<<-1<<'\n';
else{
if(n==2) cout<<1<<'\n';
else cout<<tinh((n-2)/2)<<'\n';
}
}
return 0;
}
COBAN015
#include <bits/stdc++.h>
#define MAX 50
#define ll long long
using namespace std;

int arr[MAX];
int n, k;
ll gt(int n) {
ll s = 1;
for (int i = 1; i <= n; i++)
s *= i;
return s;
}

ll sumKey(int n, int k) {
return gt(n) / (gt(k)*gt(n - k));
}

void res() {
cout << "[";
for (int i = 1; i <= k; i++){
cout << arr[i];
if(i < k) cout << " ";
}
cout << "]";
cout << endl;
}

void key(int i) {
for (int j = arr[i-1] + 1; j <= n-k+i; j++)
{
arr[i] = j;
if (i == k) {
res();
}
else{
key(i + 1);
}
}
}

int main() {
int t;
cin >> t;
while(t--)
{
cin >> n >> k;
cout << sumKey(n, k) << endl;
key(1);
}
return 0;
}
COBAN007
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

// Function to find the first


// M digits from N^K
void findFirstM(ll N, ll K, ll M)
{
// Calculate First M digits
ll firstM;
double y = (double)K * log10(N * 1.0);
// Extract the number after decimal
y = y - (ll)y;
// Find 10 ^ y
double temp = pow(10.0, y);
// Move the Decimal Point M - 1 digits forward
firstM = temp * (1LL) * pow(10, M - 1);
// Print the result
cout << firstM << endl;
}

int main(){
int t;
cin >> t;
while(t--){
long long int N;
int K,M;
cin >> N >> K >> M;
if(N==0)
cout << "0";
else
findFirstM(N,K,M);
}
return 0;
}
IDCOBAN104
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;

int main(){
int t;
cin >>t;
while(t--){
string a,b;
cin.ignore();
cin>>a>>b;
int c1=0,d1=0,c2=0,d2=0;
for(int i=0;i<a.length();i++){
if(a[i]=='3') a[i]='5';
c1=c1*10+a[i]-48;
}
for(int i=0;i<b.length();i++){
if(b[i]=='3') b[i]='5';
c2=c2*10+b[i]-48;
}
c1+=c2;
for(int i=0;i<a.length();i++){
if(a[i]=='5') a[i]='3';
d1=d1*10+a[i]-48;
}
for(int i=0;i<b.length();i++){
if(b[i]=='5') b[i]='3';
d2=d2*10+b[i]-48;
}
d1+=d2;
cout<<d1<<" "<<c1<<'\n';
}
return 0;
}
IDLUYTHUA001
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;

const int base = 1000000000; const int base_digits = 9;


struct bigint {
vector<int> a; int sign;

bigint() :
sign(1) {
}

bigint(long long v) {
*this = v;
}

bigint(const string &s) {


read(s);
}

void operator=(const bigint &v) {


sign = v.sign;
a = v.a;
}
void operator=(long long v) {
sign = 1;
if (v < 0)
sign = -1, v = -v;
for (; v > 0; v = v / base)
a.push_back(v % base);
}

bigint operator+(const bigint &v) const {


if (sign == v.sign) {
bigint res = v;

for (int i = 0, carry = 0; i < (int) max(a.size(),


v.a.size()) || carry; ++i) {
if (i == (int) res.a.size())
res.a.push_back(0);
res.a[i] += carry + (i < (int) a.size() ? a[i] : 0);
carry = res.a[i] >= base;
if (carry)
res.a[i] -= base;
}
return res;
}
return *this - (-v);
}

bigint operator-(const bigint &v) const {


if (sign == v.sign) {
if (abs() >= v.abs()) {
bigint res = *this;
for (int i = 0, carry = 0; i < (int) v.a.size() ||
carry; ++i) {
res.a[i] -= carry + (i < (int) v.a.size() ?
v.a[i] : 0);
carry = res.a[i] < 0;
if (carry)
res.a[i] += base;
}
res.trim();
return res;
}
return -(v - *this);
}
return *this + (-v);
}

void operator*=(int v) {
if (v < 0)
sign = -sign, v = -v;
for (int i = 0, carry = 0; i < (int) a.size() || carry; ++i) {
if (i == (int) a.size())
a.push_back(0);
long long cur = a[i] * (long long) v + carry;
carry = (int) (cur / base);
a[i] = (int) (cur % base);
//asm("divl %%ecx" : "=a"(carry), "=d"(a[i]) : "A"(cur),
"c"(base));
}
trim();
}

bigint operator*(int v) const {


bigint res = *this;
res *= v;
return res;
}

friend pair<bigint, bigint> divmod(const bigint &a1, const bigint


&b1) {
int norm = base / (b1.a.back() + 1);
bigint a = a1.abs() * norm;
bigint b = b1.abs() * norm;
bigint q, r;
q.a.resize(a.a.size());

for (int i = a.a.size() - 1; i >= 0; i--) {


r *= base;
r += a.a[i];
int s1 = r.a.size() <= b.a.size() ? 0 : r.a[b.a.size()];
int s2 = r.a.size() <= b.a.size() - 1 ? 0 : r.a[b.a.size()
- 1];
int d = ((long long) base * s1 + s2) / b.a.back();
r -= b * d;
while (r < 0)
r += b, --d;
q.a[i] = d;
}

q.sign = a1.sign * b1.sign;


r.sign = a1.sign;
q.trim();
r.trim();
return make_pair(q, r / norm);
}

bigint operator/(const bigint &v) const {


return divmod(*this, v).first;
}

bigint operator%(const bigint &v) const {


return divmod(*this, v).second;
}

void operator/=(int v) {
if (v < 0)
sign = -sign, v = -v;
for (int i = (int) a.size() - 1, rem = 0; i >= 0; --i) {
long long cur = a[i] + rem * (long long) base;
a[i] = (int) (cur / v);
rem = (int) (cur % v);
}
trim();
}

bigint operator/(int v) const {


bigint res = *this;
res /= v;
return res;
}

int operator%(int v) const {


if (v < 0)
v = -v;
int m = 0;
for (int i = a.size() - 1; i >= 0; --i)
m = (a[i] + m * (long long) base) % v;
return m * sign;
}

void operator+=(const bigint &v) {


*this = *this + v;
}
void operator-=(const bigint &v) {
*this = *this - v;
}
void operator*=(const bigint &v) {
*this = *this * v;
}
void operator/=(const bigint &v) {
*this = *this / v;
}

bool operator<(const bigint &v) const {


if (sign != v.sign)
return sign < v.sign;
if (a.size() != v.a.size())
return a.size() * sign < v.a.size() * v.sign;
for (int i = a.size() - 1; i >= 0; i--)
if (a[i] != v.a[i])
return a[i] * sign < v.a[i] * sign;
return false;
}
bool operator>(const bigint &v) const {
return v < *this;
}
bool operator<=(const bigint &v) const {
return !(v < *this);
}
bool operator>=(const bigint &v) const {
return !(*this < v);
}
bool operator==(const bigint &v) const {
return !(*this < v) && !(v < *this);
}
bool operator!=(const bigint &v) const {
return *this < v || v < *this;
}

void trim() {
while (!a.empty() && !a.back())
a.pop_back();
if (a.empty())
sign = 1;
}

bool isZero() const {


return a.empty() || (a.size() == 1 && !a[0]);
}

bigint operator-() const {


bigint res = *this;
res.sign = -sign;
return res;
}

bigint abs() const {


bigint res = *this;
res.sign *= res.sign;
return res;
}

long long longValue() const {


long long res = 0;
for (int i = a.size() - 1; i >= 0; i--)
res = res * base + a[i];
return res * sign;
}

friend bigint gcd(const bigint &a, const bigint &b) {


return b.isZero() ? a : gcd(b, a % b);
}
friend bigint lcm(const bigint &a, const bigint &b) {
return a / gcd(a, b) * b;
}

void read(const string &s) {


sign = 1;
a.clear();
int pos = 0;
while (pos < (int) s.size() && (s[pos] == '-' || s[pos] ==
'+')) {
if (s[pos] == '-')
sign = -sign;
++pos;
}
for (int i = s.size() - 1; i >= pos; i -= base_digits) {
int x = 0;
for (int j = max(pos, i - base_digits + 1); j <= i; j++)
x = x * 10 + s[j] - '0';
a.push_back(x);
}
trim();
}

friend istream& operator>>(istream &stream, bigint &v) {


string s;
stream >> s;
v.read(s);
return stream;
}

friend ostream& operator<<(ostream &stream, const bigint &v) {


if (v.sign == -1)
stream << '-';
stream << (v.a.empty() ? 0 : v.a.back());
for (int i = (int) v.a.size() - 2; i >= 0; --i)
stream << setw(base_digits) << setfill('0') << v.a[i];
return stream;
}

static vector<int> convert_base(const vector<int> &a, int


old_digits, int new_digits) {
vector<long long> p(max(old_digits, new_digits) + 1);
p[0] = 1;
for (int i = 1; i < (int) p.size(); i++)
p[i] = p[i - 1] * 10;
vector<int> res;
long long cur = 0;
int cur_digits = 0;
for (int i = 0; i < (int) a.size(); i++) {
cur += a[i] * p[cur_digits];
cur_digits += old_digits;
while (cur_digits >= new_digits) {
res.push_back(int(cur % p[new_digits]));
cur /= p[new_digits];
cur_digits -= new_digits;
}
}
res.push_back((int) cur);
while (!res.empty() && !res.back())
res.pop_back();
return res;
}

typedef vector<long long> vll;

static vll karatsubaMultiply(const vll &a, const vll &b) {


int n = a.size();
vll res(n + n);
if (n <= 32) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
res[i + j] += a[i] * b[j];
return res;
}

int k = n >> 1;
vll a1(a.begin(), a.begin() + k);
vll a2(a.begin() + k, a.end());
vll b1(b.begin(), b.begin() + k);
vll b2(b.begin() + k, b.end());

vll a1b1 = karatsubaMultiply(a1, b1);


vll a2b2 = karatsubaMultiply(a2, b2);

for (int i = 0; i < k; i++)


a2[i] += a1[i];
for (int i = 0; i < k; i++)
b2[i] += b1[i];

vll r = karatsubaMultiply(a2, b2);


for (int i = 0; i < (int) a1b1.size(); i++)
r[i] -= a1b1[i];
for (int i = 0; i < (int) a2b2.size(); i++)
r[i] -= a2b2[i];

for (int i = 0; i < (int) r.size(); i++)


res[i + k] += r[i];
for (int i = 0; i < (int) a1b1.size(); i++)
res[i] += a1b1[i];
for (int i = 0; i < (int) a2b2.size(); i++)
res[i + n] += a2b2[i];
return res;
}
bigint operator*(const bigint &v) const {
vector<int> a6 = convert_base(this->a, base_digits, 6);
vector<int> b6 = convert_base(v.a, base_digits, 6);
vll a(a6.begin(), a6.end());
vll b(b6.begin(), b6.end());
while (a.size() < b.size())
a.push_back(0);
while (b.size() < a.size())
b.push_back(0);
while (a.size() & (a.size() - 1))
a.push_back(0), b.push_back(0);
vll c = karatsubaMultiply(a, b);
bigint res;
res.sign = sign * v.sign;
for (int i = 0, carry = 0; i < (int) c.size(); i++) {
long long cur = c[i] + carry;
res.a.push_back((int) (cur % 1000000));
carry = (int) (cur / 1000000);
}
res.a = convert_base(res.a, 6, base_digits);
res.trim();
return res;
}
};
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int t;
cin >> t;
while(t--){
int n, k;
bigint ans;
ans=1;
cin >> n >> k;
for(int i=1; i <= k; ++i){
ans *= n;
}
cout<<ans<<'\n';
}
return 0;
}
IDCOBAN105
#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--){
int n;
cin >> n;
int x, s = 0, mod = 1e9 + 7;
priority_queue<int, vector<int>, greater<int>> q;
for (int i = 0; i < n; i++){
cin >> x;
q.push(x);
}
while (q.size() > 1){
int s1 = q.top();
q.pop();
int s2 = q.top();
q.pop();
int k = (s1 + s2) % mod;
s = (s + k) % mod;
q.push(k);
}
cout << s << endl;
}
}
IDCOBAN107
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;

ll t, n, k, x;
struct matrix{
ll c[15][15];
};
matrix operator * (matrix a, matrix b)
{
matrix res;
for(int i=0; i<n; i++)
for(int j=0; j<n; j++){
res.c[i][j] = 0;
for (int k=0; k<n; k++)
res.c[i][j] = (res.c[i][j]+a.c[i][k]*b.c[k][j])%MOD;
}
return res;
}

matrix tinh(matrix a, ll n)
{
if (n==1) return a;
if (n%2!=0) return tinh(a,n-1)*a;
matrix tmp = tinh(a,n/2);
return tmp*tmp;
}
int main()
{
cin >> t;
while(t--){
cin >> n >> k;
matrix a;
f(i,0,n-1) f(j,0,n-1){
cin >> x;
a.c[i][j]=x;
}
a=tinh(a,k);
f(i,0,n-1) {f(j,0,n-1) cout<<a.c[i][j]%MOD<<" "; cout<<'\n';}
}
return 0;
}
IDDUONGDI101
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;

int t, m, n, dp[105][105], a[105][105];


int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

cin >> t;
while(t--){
cin >> m >> n;
f(i,1,m) f(j,1,n) cin >> a[i][j];
f(i,1,n) dp[0][i]=-INT_MAX, dp[m+1][i]=-INT_MAX;
f(i,1,m) dp[i][1]=a[i][1];
f(j,2,n){
f(i,1,m){
dp[i][j]=max(max(dp[i-1][j-1],dp[i][j-1]),dp[i+1][j-1])+a[i]
[j];
}
}
int ans=-INT_MAX;
f(i,1,m) ans=max(ans,dp[i][n]);
cout<<ans<<'\n';
}
return 0;
}
IDCOBAN104
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;

int t, a, b, x, y;
stack<int>stx1, stx2, sty1, sty2;
int main()
{
cin >> t;
while(t--){
cin >> a >> b;
x=a, y=b;
while(x){
if(x%10==5) stx1.push(3);
else stx1.push(x%10);
if(x%10==3) stx2.push(5);
else stx2.push(x%10);
x/=10;
}
while(!stx1.empty()) x=x*10+stx1.top(), stx1.pop();
while(y){
if(y%10==5) sty1.push(3);
else sty1.push(y%10);
if(y%10==3) sty2.push(5);
else sty2.push(y%10);
y/=10;
}
while(!sty1.empty()) y=y*10+sty1.top(), sty1.pop();
cout<<x+y<<" ";
x=0, y=0;
while(!stx2.empty()) x=x*10+stx2.top(), stx2.pop();
while(!sty2.empty()) y=y*10+sty2.top(), sty2.pop();
cout<<x+y<<'\n';
}
return 0;
}
IDNGUYENTO008
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;
int p, n, s, a[20], snt[505], c, k, check[505], z;
bool checknt(int n){
if(n<=1) return 0;
if(n==2) return 1;
f(i,2,sqrt(n)) if(n%i==0) return 0;
return 1;
}
void print(){
z=1;
f(i,1,n) cout<<snt[a[i]]<<" ";
cout<<'\n';
}
void Try(int k, int sum){
if(k==n+1){
if(sum==s) print();
}
else{
f(i,a[k-1]+1,c){
if(check[i]==0){
if(sum+i<=s){
check[i]=1;
a[k]=i;
Try(k+1,sum+snt[i]);
check[i]=0;
}
else break;
}
}
}
}
int main()
{
cin >> p >> n >> s;
f(i,p+1,s) if(checknt(i)) snt[++c]=i;
//f(i,1,c) cout<<snt[i]<<" ";
Try(1,0);
if(z==0) cout<<-1;
return 0;
}
COBAN005
#include <bits/stdc++.h>

using namespace std;

#define ll long long


const ll MOD = 1e9 + 7;
ll powerLL(ll x, ll n)
{
ll result = 1;
while (n) {
if (n & 1)
result = result * x % MOD;
n = n / 2;
x = x * x % MOD;
}
return result;
}

ll powerStrings(string sa, string sb)


{
ll a = 0, b = 0;
for (int i = 0; i < sa.length(); i++)
a = (a * 10 + (sa[i] - '0')) % MOD;
for (int i = 0; i < sb.length(); i++)
b = (b * 10 + (sb[i] - '0')) % (MOD - 1);
return powerLL(a, b);
}

int main()
{
int test;
cin >> test;
while (test--)
{
cin.ignore();
string sa, sb;
cin >> sa >> sb;
cout << powerStrings(sa, sb) << endl;
}
return 0;
}
IDCOBAN026
#include<bits/stdc++.h>
using namespace std;

int n , m , ans = INT_MAX ,cmin = INT_MAX;

int c[500][500], check[50], luu[50] ,truoc[500];

void Try(int k){


for(int i=2; i<=n; ++i){
if(check[i]==0 && c[truoc[k-1]][i]!=0 ){
check[i] = 1;
luu[k] = luu[k-1] + c[truoc[k-1]][i] ;
truoc[k] = i ;
if(k==(n-1)){
if(c[i][1])ans = min(ans,luu[k]+c[i][1]) ;}
else if(luu[k] + cmin*(n-k) < ans )Try(k+1);
check[i] = 0 ;
}
}
}
int main(){
int k , j ,c0;
truoc[0] = 1;
cin >> n >> m ;
for(int i=1; i<=m; ++i){
cin >> k >> j >> c0 ;
c[k][j] = c0 ;
cmin = min(cmin , c0);
}
Try(1);
cout << ans ;
return 0;
}
COBAN005
#include <bits/stdc++.h>

using namespace std;

#define ll long long


const ll MOD = 1e9 + 7;
ll powerLL(ll x, ll n)
{
ll result = 1;
while (n) {
if (n & 1)
result = result * x % MOD;
n = n / 2;
x = x * x % MOD;
}
return result;
}

ll powerStrings(string sa, string sb)


{
ll a = 0, b = 0;
for (int i = 0; i < sa.length(); i++)
a = (a * 10 + (sa[i] - '0')) % MOD;
for (int i = 0; i < sb.length(); i++)
b = (b * 10 + (sb[i] - '0')) % (MOD - 1);
return powerLL(a, b);
}

int main()
{
int test;
cin >> test;
while (test--)
{
cin.ignore();
string sa, sb;
cin >> sa >> sb;
cout << powerStrings(sa, sb) << endl;
}
return 0;
}
IDBDS008
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;

ll tinh(int n){
if(n==0) return 1;
if(n%2) return (2*tinh(n-1))%MOD;
ll tmp=tinh(n/2);
return (tmp*tmp)%MOD;
}
int main()
{
int t;
cin >> t;
while(t--){
long long n;
cin >> n;
if(n%2) cout<<-1<<'\n';
else{
if(n==2) cout<<1<<'\n';
else cout<<tinh((n-2)/2)<<'\n';
}
}
return 0;
}
IDBDS009
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;

struct matrix{
long long c[2][2];
matrix(){
c[0][0]=0;
c[0][1]=1;
c[1][0]=1;
c[1][1]=1;
}
};
matrix operator * (matrix a, matrix b)
{
matrix res;
for (int i=0; i<=1; i++)
for (int j=0; j<=1; j++)
{
res.c[i][j] = 0;
for (int k=0; k<=1; k++)
res.c[i][j] = (res.c[i][j]+a.c[i][k]*b.c[k][j])%MOD;
}
return res;
}

matrix tinh(matrix a, long long n)


{
if (n==1) return a;
if (n%2!=0) return tinh(a,n-1)*a;
matrix tmp = tinh(a,n/2);
return tmp*tmp;
}
int main()
{
int t;
cin >> t;
while(t--){
long long n;
cin >> n;
matrix a;
a=tinh(a,n);
cout << a.c[0][1]<<'\n';
}
return 0;
}
IDBDS010
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;

struct matrix{
long long c[2][2];
matrix(){
c[0][0]=0;
c[0][1]=1;
c[1][0]=1;
c[1][1]=1;
}
};
matrix operator * (matrix a, matrix b)
{
matrix res;
for (int i=0; i<=1; i++)
for (int j=0; j<=1; j++)
{
res.c[i][j] = 0;
for (int k=0; k<=1; k++)
res.c[i][j] = (res.c[i][j]+a.c[i][k]*b.c[k][j])%MOD;
}
return res;
}

matrix tinh(matrix a, long long n)


{
if (n==1) return a;
if (n%2!=0) return tinh(a,n-1)*a;
matrix tmp = tinh(a,n/2);
return tmp*tmp;
}
int main()
{
int t;
cin >> t;
while(t--){
long long n;
cin >> n;
matrix a;
a=tinh(a,n+1);
cout << a.c[0][1]<<'\n';
}
return 0;
}
IDNGUYENTO006
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;

int n, check[100005], u[100005], snt[100005], c, ans[100005];


int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
check[1]=1;
for(int i=2; i*i<=100005; ++i){
if(check[i]==0) for(int j=i*i; j<=100005; j+=i) check[j]=1;
}
f(i,1,100005) if(check[i]==0) snt[++c]=i;
for(int i=c; i>=1; i--){
for(int j=snt[i]; j<=100005; j+=snt[i])
if(u[j]==0) u[j]=snt[i];
}
f(i,1,100005){
if(u[i]>sqrt(i)) ans[i]=i;
else ans[i]=ans[i-1];
}
int t;
cin >> t;
while(t--){
cin >> n;
if(n==0) cout<<-1<<'\n';
else cout<<ans[n]<<'\n';
}
return 0;
}
IDCOBAN019
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;

ll n;
ll tinh(ll n){
if(n<=1) return n;
ll x=sqrt(n);
ll a=n%2+tinh(n/2), b=n%3+tinh(n/3), c=n-x*x+tinh(x);
return 1+min(a,min(b,c));
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int t;
cin >> t;
while(t--){
cin >> n;
cout<<tinh(n)<<'\n';
}
return 0;
}
NHIPHAN001
#include <bits/stdc++.h>
typedef unsigned long long ull;
using namespace std;

ull change (string s, int n){


ull sum = 0;

for (int i = 0; i < n; i++){


if (s[i] == '1')
sum |= 1 << (n - 1 - i);
}

return sum;
}

ull print (string a, string b , int n){


ull aa = change (a, n);
ull bb = change (b, n);
if (aa == bb)
return 0;
return (aa > bb) ? (aa - bb - 1) : (bb - aa -1);
}

int main (){


int t;
cin >> t;
while (t--){
int n;
string x, y;
cin >> n >> x >> y;
cout << print (x , y, n) << endl;
}
return 0;
}
TEST005
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;

const int base = 1000000000; const int base_digits = 9;


struct bigint {
vector<int> a; int sign;

bigint() :
sign(1) {
}
bigint(long long v) {
*this = v;
}

bigint(const string &s) {


read(s);
}

void operator=(const bigint &v) {


sign = v.sign;
a = v.a;
}

void operator=(long long v) {


sign = 1;
if (v < 0)
sign = -1, v = -v;
for (; v > 0; v = v / base)
a.push_back(v % base);
}

bigint operator+(const bigint &v) const {


if (sign == v.sign) {
bigint res = v;

for (int i = 0, carry = 0; i < (int) max(a.size(),


v.a.size()) || carry; ++i) {
if (i == (int) res.a.size())
res.a.push_back(0);
res.a[i] += carry + (i < (int) a.size() ? a[i] : 0);
carry = res.a[i] >= base;
if (carry)
res.a[i] -= base;
}
return res;
}
return *this - (-v);
}

bigint operator-(const bigint &v) const {


if (sign == v.sign) {
if (abs() >= v.abs()) {
bigint res = *this;
for (int i = 0, carry = 0; i < (int) v.a.size() ||
carry; ++i) {
res.a[i] -= carry + (i < (int) v.a.size() ?
v.a[i] : 0);
carry = res.a[i] < 0;
if (carry)
res.a[i] += base;
}
res.trim();
return res;
}
return -(v - *this);
}
return *this + (-v);
}

void operator*=(int v) {
if (v < 0)
sign = -sign, v = -v;
for (int i = 0, carry = 0; i < (int) a.size() || carry; ++i) {
if (i == (int) a.size())
a.push_back(0);
long long cur = a[i] * (long long) v + carry;
carry = (int) (cur / base);
a[i] = (int) (cur % base);
//asm("divl %%ecx" : "=a"(carry), "=d"(a[i]) : "A"(cur),
"c"(base));
}
trim();
}

bigint operator*(int v) const {


bigint res = *this;
res *= v;
return res;
}

friend pair<bigint, bigint> divmod(const bigint &a1, const bigint


&b1) {
int norm = base / (b1.a.back() + 1);
bigint a = a1.abs() * norm;
bigint b = b1.abs() * norm;
bigint q, r;
q.a.resize(a.a.size());

for (int i = a.a.size() - 1; i >= 0; i--) {


r *= base;
r += a.a[i];
int s1 = r.a.size() <= b.a.size() ? 0 : r.a[b.a.size()];
int s2 = r.a.size() <= b.a.size() - 1 ? 0 : r.a[b.a.size()
- 1];
int d = ((long long) base * s1 + s2) / b.a.back();
r -= b * d;
while (r < 0)
r += b, --d;
q.a[i] = d;
}

q.sign = a1.sign * b1.sign;


r.sign = a1.sign;
q.trim();
r.trim();
return make_pair(q, r / norm);
}

bigint operator/(const bigint &v) const {


return divmod(*this, v).first;
}

bigint operator%(const bigint &v) const {


return divmod(*this, v).second;
}

void operator/=(int v) {
if (v < 0)
sign = -sign, v = -v;
for (int i = (int) a.size() - 1, rem = 0; i >= 0; --i) {
long long cur = a[i] + rem * (long long) base;
a[i] = (int) (cur / v);
rem = (int) (cur % v);
}
trim();
}

bigint operator/(int v) const {


bigint res = *this;
res /= v;
return res;
}

int operator%(int v) const {


if (v < 0)
v = -v;
int m = 0;
for (int i = a.size() - 1; i >= 0; --i)
m = (a[i] + m * (long long) base) % v;
return m * sign;
}

void operator+=(const bigint &v) {


*this = *this + v;
}
void operator-=(const bigint &v) {
*this = *this - v;
}
void operator*=(const bigint &v) {
*this = *this * v;
}
void operator/=(const bigint &v) {
*this = *this / v;
}

bool operator<(const bigint &v) const {


if (sign != v.sign)
return sign < v.sign;
if (a.size() != v.a.size())
return a.size() * sign < v.a.size() * v.sign;
for (int i = a.size() - 1; i >= 0; i--)
if (a[i] != v.a[i])
return a[i] * sign < v.a[i] * sign;
return false;
}

bool operator>(const bigint &v) const {


return v < *this;
}
bool operator<=(const bigint &v) const {
return !(v < *this);
}
bool operator>=(const bigint &v) const {
return !(*this < v);
}
bool operator==(const bigint &v) const {
return !(*this < v) && !(v < *this);
}
bool operator!=(const bigint &v) const {
return *this < v || v < *this;
}

void trim() {
while (!a.empty() && !a.back())
a.pop_back();
if (a.empty())
sign = 1;
}

bool isZero() const {


return a.empty() || (a.size() == 1 && !a[0]);
}

bigint operator-() const {


bigint res = *this;
res.sign = -sign;
return res;
}

bigint abs() const {


bigint res = *this;
res.sign *= res.sign;
return res;
}
long long longValue() const {
long long res = 0;
for (int i = a.size() - 1; i >= 0; i--)
res = res * base + a[i];
return res * sign;
}

friend bigint gcd(const bigint &a, const bigint &b) {


return b.isZero() ? a : gcd(b, a % b);
}
friend bigint lcm(const bigint &a, const bigint &b) {
return a / gcd(a, b) * b;
}

void read(const string &s) {


sign = 1;
a.clear();
int pos = 0;
while (pos < (int) s.size() && (s[pos] == '-' || s[pos] ==
'+')) {
if (s[pos] == '-')
sign = -sign;
++pos;
}
for (int i = s.size() - 1; i >= pos; i -= base_digits) {
int x = 0;
for (int j = max(pos, i - base_digits + 1); j <= i; j++)
x = x * 10 + s[j] - '0';
a.push_back(x);
}
trim();
}

friend istream& operator>>(istream &stream, bigint &v) {


string s;
stream >> s;
v.read(s);
return stream;
}

friend ostream& operator<<(ostream &stream, const bigint &v) {


if (v.sign == -1)
stream << '-';
stream << (v.a.empty() ? 0 : v.a.back());
for (int i = (int) v.a.size() - 2; i >= 0; --i)
stream << setw(base_digits) << setfill('0') << v.a[i];
return stream;
}
static vector<int> convert_base(const vector<int> &a, int
old_digits, int new_digits) {
vector<long long> p(max(old_digits, new_digits) + 1);
p[0] = 1;
for (int i = 1; i < (int) p.size(); i++)
p[i] = p[i - 1] * 10;
vector<int> res;
long long cur = 0;
int cur_digits = 0;
for (int i = 0; i < (int) a.size(); i++) {
cur += a[i] * p[cur_digits];
cur_digits += old_digits;
while (cur_digits >= new_digits) {
res.push_back(int(cur % p[new_digits]));
cur /= p[new_digits];
cur_digits -= new_digits;
}
}
res.push_back((int) cur);
while (!res.empty() && !res.back())
res.pop_back();
return res;
}

typedef vector<long long> vll;

static vll karatsubaMultiply(const vll &a, const vll &b) {


int n = a.size();
vll res(n + n);
if (n <= 32) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
res[i + j] += a[i] * b[j];
return res;
}

int k = n >> 1;
vll a1(a.begin(), a.begin() + k);
vll a2(a.begin() + k, a.end());
vll b1(b.begin(), b.begin() + k);
vll b2(b.begin() + k, b.end());

vll a1b1 = karatsubaMultiply(a1, b1);


vll a2b2 = karatsubaMultiply(a2, b2);

for (int i = 0; i < k; i++)


a2[i] += a1[i];
for (int i = 0; i < k; i++)
b2[i] += b1[i];

vll r = karatsubaMultiply(a2, b2);


for (int i = 0; i < (int) a1b1.size(); i++)
r[i] -= a1b1[i];
for (int i = 0; i < (int) a2b2.size(); i++)
r[i] -= a2b2[i];

for (int i = 0; i < (int) r.size(); i++)


res[i + k] += r[i];
for (int i = 0; i < (int) a1b1.size(); i++)
res[i] += a1b1[i];
for (int i = 0; i < (int) a2b2.size(); i++)
res[i + n] += a2b2[i];
return res;
}

bigint operator*(const bigint &v) const {


vector<int> a6 = convert_base(this->a, base_digits, 6);
vector<int> b6 = convert_base(v.a, base_digits, 6);
vll a(a6.begin(), a6.end());
vll b(b6.begin(), b6.end());
while (a.size() < b.size())
a.push_back(0);
while (b.size() < a.size())
b.push_back(0);
while (a.size() & (a.size() - 1))
a.push_back(0), b.push_back(0);
vll c = karatsubaMultiply(a, b);
bigint res;
res.sign = sign * v.sign;
for (int i = 0, carry = 0; i < (int) c.size(); i++) {
long long cur = c[i] + carry;
res.a.push_back((int) (cur % 1000000));
carry = (int) (cur / 1000000);
}
res.a = convert_base(res.a, 6, base_digits);
res.trim();
return res;
}
};
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int t;
cin >> t;
while(t--){
int n, k;
bigint ans;
ans=1;
cin >> n >> k;
for(int i=n; i>=n-k+1; i--){
bigint x=i;
ans*=x;
}
cout<<ans<<'\n';
}
return 0;
}
IDCOBAN020
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;

ll n, cs, a, m, key[100005], mx, t, gt, ans, test[100005];


ll tinh(ll x){
ll ans=1;
while(x) ans*=(x%10), x/=10;
return ans;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
f(i,1,1e3){
if(tinh(i)>mx) mx=tinh(i), gt=i;
key[i]=gt;
}
//f(i,1,99) cout<<i<<" "<<key[i]<<'\n';
cin >> t;
while(t--){
//f(p,100,1e5){
cin >> n;
if(n<1000) {cout<<tinh(key[n])<<'\n'; continue;}
m=n, cs=0, ans=1;
while(m) cs++, m/=10;
a=n;
f(i,1,cs-3) a/=10;
m=a;
f(i,1,cs-3) m=m*10+9;
if(m==n) m=key[a];
else m=key[a-1];
f(i,1,cs-3) m=m*10+9;
cout<<tinh(m)<<'\n';
//}
}
//f(i,100,1e5) if(test[i]!=key[i]) cout<<i<<" "<<test[i]<<"
"<<key[i]<<'\n';
return 0;
}
TEST001
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;

const int base = 1000000000; const int base_digits = 9;


struct bigint {
vector<int> a; int sign;

bigint() :
sign(1) {
}

bigint(long long v) {
*this = v;
}

bigint(const string &s) {


read(s);
}

void operator=(const bigint &v) {


sign = v.sign;
a = v.a;
}

void operator=(long long v) {


sign = 1;
if (v < 0)
sign = -1, v = -v;
for (; v > 0; v = v / base)
a.push_back(v % base);
}

bigint operator+(const bigint &v) const {


if (sign == v.sign) {
bigint res = v;

for (int i = 0, carry = 0; i < (int) max(a.size(),


v.a.size()) || carry; ++i) {
if (i == (int) res.a.size())
res.a.push_back(0);
res.a[i] += carry + (i < (int) a.size() ? a[i] : 0);
carry = res.a[i] >= base;
if (carry)
res.a[i] -= base;
}
return res;
}
return *this - (-v);
}

bigint operator-(const bigint &v) const {


if (sign == v.sign) {
if (abs() >= v.abs()) {
bigint res = *this;
for (int i = 0, carry = 0; i < (int) v.a.size() ||
carry; ++i) {
res.a[i] -= carry + (i < (int) v.a.size() ?
v.a[i] : 0);
carry = res.a[i] < 0;
if (carry)
res.a[i] += base;
}
res.trim();
return res;
}
return -(v - *this);
}
return *this + (-v);
}

void operator*=(int v) {
if (v < 0)
sign = -sign, v = -v;
for (int i = 0, carry = 0; i < (int) a.size() || carry; ++i) {
if (i == (int) a.size())
a.push_back(0);
long long cur = a[i] * (long long) v + carry;
carry = (int) (cur / base);
a[i] = (int) (cur % base);
//asm("divl %%ecx" : "=a"(carry), "=d"(a[i]) : "A"(cur),
"c"(base));
}
trim();
}

bigint operator*(int v) const {


bigint res = *this;
res *= v;
return res;
}

friend pair<bigint, bigint> divmod(const bigint &a1, const bigint


&b1) {
int norm = base / (b1.a.back() + 1);
bigint a = a1.abs() * norm;
bigint b = b1.abs() * norm;
bigint q, r;
q.a.resize(a.a.size());
for (int i = a.a.size() - 1; i >= 0; i--) {
r *= base;
r += a.a[i];
int s1 = r.a.size() <= b.a.size() ? 0 : r.a[b.a.size()];
int s2 = r.a.size() <= b.a.size() - 1 ? 0 : r.a[b.a.size()
- 1];
int d = ((long long) base * s1 + s2) / b.a.back();
r -= b * d;
while (r < 0)
r += b, --d;
q.a[i] = d;
}

q.sign = a1.sign * b1.sign;


r.sign = a1.sign;
q.trim();
r.trim();
return make_pair(q, r / norm);
}

bigint operator/(const bigint &v) const {


return divmod(*this, v).first;
}

bigint operator%(const bigint &v) const {


return divmod(*this, v).second;
}

void operator/=(int v) {
if (v < 0)
sign = -sign, v = -v;
for (int i = (int) a.size() - 1, rem = 0; i >= 0; --i) {
long long cur = a[i] + rem * (long long) base;
a[i] = (int) (cur / v);
rem = (int) (cur % v);
}
trim();
}

bigint operator/(int v) const {


bigint res = *this;
res /= v;
return res;
}

int operator%(int v) const {


if (v < 0)
v = -v;
int m = 0;
for (int i = a.size() - 1; i >= 0; --i)
m = (a[i] + m * (long long) base) % v;
return m * sign;
}

void operator+=(const bigint &v) {


*this = *this + v;
}
void operator-=(const bigint &v) {
*this = *this - v;
}
void operator*=(const bigint &v) {
*this = *this * v;
}
void operator/=(const bigint &v) {
*this = *this / v;
}

bool operator<(const bigint &v) const {


if (sign != v.sign)
return sign < v.sign;
if (a.size() != v.a.size())
return a.size() * sign < v.a.size() * v.sign;
for (int i = a.size() - 1; i >= 0; i--)
if (a[i] != v.a[i])
return a[i] * sign < v.a[i] * sign;
return false;
}

bool operator>(const bigint &v) const {


return v < *this;
}
bool operator<=(const bigint &v) const {
return !(v < *this);
}
bool operator>=(const bigint &v) const {
return !(*this < v);
}
bool operator==(const bigint &v) const {
return !(*this < v) && !(v < *this);
}
bool operator!=(const bigint &v) const {
return *this < v || v < *this;
}

void trim() {
while (!a.empty() && !a.back())
a.pop_back();
if (a.empty())
sign = 1;
}
bool isZero() const {
return a.empty() || (a.size() == 1 && !a[0]);
}

bigint operator-() const {


bigint res = *this;
res.sign = -sign;
return res;
}

bigint abs() const {


bigint res = *this;
res.sign *= res.sign;
return res;
}

long long longValue() const {


long long res = 0;
for (int i = a.size() - 1; i >= 0; i--)
res = res * base + a[i];
return res * sign;
}

friend bigint gcd(const bigint &a, const bigint &b) {


return b.isZero() ? a : gcd(b, a % b);
}
friend bigint lcm(const bigint &a, const bigint &b) {
return a / gcd(a, b) * b;
}

void read(const string &s) {


sign = 1;
a.clear();
int pos = 0;
while (pos < (int) s.size() && (s[pos] == '-' || s[pos] ==
'+')) {
if (s[pos] == '-')
sign = -sign;
++pos;
}
for (int i = s.size() - 1; i >= pos; i -= base_digits) {
int x = 0;
for (int j = max(pos, i - base_digits + 1); j <= i; j++)
x = x * 10 + s[j] - '0';
a.push_back(x);
}
trim();
}

friend istream& operator>>(istream &stream, bigint &v) {


string s;
stream >> s;
v.read(s);
return stream;
}

friend ostream& operator<<(ostream &stream, const bigint &v) {


if (v.sign == -1)
stream << '-';
stream << (v.a.empty() ? 0 : v.a.back());
for (int i = (int) v.a.size() - 2; i >= 0; --i)
stream << setw(base_digits) << setfill('0') << v.a[i];
return stream;
}

static vector<int> convert_base(const vector<int> &a, int


old_digits, int new_digits) {
vector<long long> p(max(old_digits, new_digits) + 1);
p[0] = 1;
for (int i = 1; i < (int) p.size(); i++)
p[i] = p[i - 1] * 10;
vector<int> res;
long long cur = 0;
int cur_digits = 0;
for (int i = 0; i < (int) a.size(); i++) {
cur += a[i] * p[cur_digits];
cur_digits += old_digits;
while (cur_digits >= new_digits) {
res.push_back(int(cur % p[new_digits]));
cur /= p[new_digits];
cur_digits -= new_digits;
}
}
res.push_back((int) cur);
while (!res.empty() && !res.back())
res.pop_back();
return res;
}

typedef vector<long long> vll;

static vll karatsubaMultiply(const vll &a, const vll &b) {


int n = a.size();
vll res(n + n);
if (n <= 32) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
res[i + j] += a[i] * b[j];
return res;
}

int k = n >> 1;
vll a1(a.begin(), a.begin() + k);
vll a2(a.begin() + k, a.end());
vll b1(b.begin(), b.begin() + k);
vll b2(b.begin() + k, b.end());

vll a1b1 = karatsubaMultiply(a1, b1);


vll a2b2 = karatsubaMultiply(a2, b2);

for (int i = 0; i < k; i++)


a2[i] += a1[i];
for (int i = 0; i < k; i++)
b2[i] += b1[i];

vll r = karatsubaMultiply(a2, b2);


for (int i = 0; i < (int) a1b1.size(); i++)
r[i] -= a1b1[i];
for (int i = 0; i < (int) a2b2.size(); i++)
r[i] -= a2b2[i];

for (int i = 0; i < (int) r.size(); i++)


res[i + k] += r[i];
for (int i = 0; i < (int) a1b1.size(); i++)
res[i] += a1b1[i];
for (int i = 0; i < (int) a2b2.size(); i++)
res[i + n] += a2b2[i];
return res;
}

bigint operator*(const bigint &v) const {


vector<int> a6 = convert_base(this->a, base_digits, 6);
vector<int> b6 = convert_base(v.a, base_digits, 6);
vll a(a6.begin(), a6.end());
vll b(b6.begin(), b6.end());
while (a.size() < b.size())
a.push_back(0);
while (b.size() < a.size())
b.push_back(0);
while (a.size() & (a.size() - 1))
a.push_back(0), b.push_back(0);
vll c = karatsubaMultiply(a, b);
bigint res;
res.sign = sign * v.sign;
for (int i = 0, carry = 0; i < (int) c.size(); i++) {
long long cur = c[i] + carry;
res.a.push_back((int) (cur % 1000000));
carry = (int) (cur / 1000000);
}
res.a = convert_base(res.a, 6, base_digits);
res.trim();
return res;
}
};
bigint c;
bigint tinh(bigint a, bigint b){
if(b==0) return 1;
if(b%2==0){
bigint tmp=tinh(a,b/2);
return (tmp*tmp)%c;
}
else{
bigint tmp=tinh(a,b/2);
return (((tmp*tmp)%c)*a)%c;
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int t;
cin >> t;
while(t--){
bigint ans=1;
int n;
cin >> n;
f(i,1,n) ans*=2*i;
cout<<ans<<'\n';
}
return 0;
}
COBAN011
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;

const int base = 1000000000; const int base_digits = 9;


struct bigint {
vector<int> a; int sign;

bigint() :
sign(1) {
}

bigint(long long v) {
*this = v;
}

bigint(const string &s) {


read(s);
}
void operator=(const bigint &v) {
sign = v.sign;
a = v.a;
}

void operator=(long long v) {


sign = 1;
if (v < 0)
sign = -1, v = -v;
for (; v > 0; v = v / base)
a.push_back(v % base);
}

bigint operator+(const bigint &v) const {


if (sign == v.sign) {
bigint res = v;

for (int i = 0, carry = 0; i < (int) max(a.size(),


v.a.size()) || carry; ++i) {
if (i == (int) res.a.size())
res.a.push_back(0);
res.a[i] += carry + (i < (int) a.size() ? a[i] : 0);
carry = res.a[i] >= base;
if (carry)
res.a[i] -= base;
}
return res;
}
return *this - (-v);
}

bigint operator-(const bigint &v) const {


if (sign == v.sign) {
if (abs() >= v.abs()) {
bigint res = *this;
for (int i = 0, carry = 0; i < (int) v.a.size() ||
carry; ++i) {
res.a[i] -= carry + (i < (int) v.a.size() ?
v.a[i] : 0);
carry = res.a[i] < 0;
if (carry)
res.a[i] += base;
}
res.trim();
return res;
}
return -(v - *this);
}
return *this + (-v);
}
void operator*=(int v) {
if (v < 0)
sign = -sign, v = -v;
for (int i = 0, carry = 0; i < (int) a.size() || carry; ++i) {
if (i == (int) a.size())
a.push_back(0);
long long cur = a[i] * (long long) v + carry;
carry = (int) (cur / base);
a[i] = (int) (cur % base);
//asm("divl %%ecx" : "=a"(carry), "=d"(a[i]) : "A"(cur),
"c"(base));
}
trim();
}

bigint operator*(int v) const {


bigint res = *this;
res *= v;
return res;
}

friend pair<bigint, bigint> divmod(const bigint &a1, const bigint


&b1) {
int norm = base / (b1.a.back() + 1);
bigint a = a1.abs() * norm;
bigint b = b1.abs() * norm;
bigint q, r;
q.a.resize(a.a.size());

for (int i = a.a.size() - 1; i >= 0; i--) {


r *= base;
r += a.a[i];
int s1 = r.a.size() <= b.a.size() ? 0 : r.a[b.a.size()];
int s2 = r.a.size() <= b.a.size() - 1 ? 0 : r.a[b.a.size()
- 1];
int d = ((long long) base * s1 + s2) / b.a.back();
r -= b * d;
while (r < 0)
r += b, --d;
q.a[i] = d;
}

q.sign = a1.sign * b1.sign;


r.sign = a1.sign;
q.trim();
r.trim();
return make_pair(q, r / norm);
}

bigint operator/(const bigint &v) const {


return divmod(*this, v).first;
}

bigint operator%(const bigint &v) const {


return divmod(*this, v).second;
}

void operator/=(int v) {
if (v < 0)
sign = -sign, v = -v;
for (int i = (int) a.size() - 1, rem = 0; i >= 0; --i) {
long long cur = a[i] + rem * (long long) base;
a[i] = (int) (cur / v);
rem = (int) (cur % v);
}
trim();
}

bigint operator/(int v) const {


bigint res = *this;
res /= v;
return res;
}

int operator%(int v) const {


if (v < 0)
v = -v;
int m = 0;
for (int i = a.size() - 1; i >= 0; --i)
m = (a[i] + m * (long long) base) % v;
return m * sign;
}

void operator+=(const bigint &v) {


*this = *this + v;
}
void operator-=(const bigint &v) {
*this = *this - v;
}
void operator*=(const bigint &v) {
*this = *this * v;
}
void operator/=(const bigint &v) {
*this = *this / v;
}

bool operator<(const bigint &v) const {


if (sign != v.sign)
return sign < v.sign;
if (a.size() != v.a.size())
return a.size() * sign < v.a.size() * v.sign;
for (int i = a.size() - 1; i >= 0; i--)
if (a[i] != v.a[i])
return a[i] * sign < v.a[i] * sign;
return false;
}

bool operator>(const bigint &v) const {


return v < *this;
}
bool operator<=(const bigint &v) const {
return !(v < *this);
}
bool operator>=(const bigint &v) const {
return !(*this < v);
}
bool operator==(const bigint &v) const {
return !(*this < v) && !(v < *this);
}
bool operator!=(const bigint &v) const {
return *this < v || v < *this;
}

void trim() {
while (!a.empty() && !a.back())
a.pop_back();
if (a.empty())
sign = 1;
}

bool isZero() const {


return a.empty() || (a.size() == 1 && !a[0]);
}

bigint operator-() const {


bigint res = *this;
res.sign = -sign;
return res;
}

bigint abs() const {


bigint res = *this;
res.sign *= res.sign;
return res;
}

long long longValue() const {


long long res = 0;
for (int i = a.size() - 1; i >= 0; i--)
res = res * base + a[i];
return res * sign;
}
friend bigint gcd(const bigint &a, const bigint &b) {
return b.isZero() ? a : gcd(b, a % b);
}
friend bigint lcm(const bigint &a, const bigint &b) {
return a / gcd(a, b) * b;
}

void read(const string &s) {


sign = 1;
a.clear();
int pos = 0;
while (pos < (int) s.size() && (s[pos] == '-' || s[pos] ==
'+')) {
if (s[pos] == '-')
sign = -sign;
++pos;
}
for (int i = s.size() - 1; i >= pos; i -= base_digits) {
int x = 0;
for (int j = max(pos, i - base_digits + 1); j <= i; j++)
x = x * 10 + s[j] - '0';
a.push_back(x);
}
trim();
}

friend istream& operator>>(istream &stream, bigint &v) {


string s;
stream >> s;
v.read(s);
return stream;
}

friend ostream& operator<<(ostream &stream, const bigint &v) {


if (v.sign == -1)
stream << '-';
stream << (v.a.empty() ? 0 : v.a.back());
for (int i = (int) v.a.size() - 2; i >= 0; --i)
stream << setw(base_digits) << setfill('0') << v.a[i];
return stream;
}

static vector<int> convert_base(const vector<int> &a, int


old_digits, int new_digits) {
vector<long long> p(max(old_digits, new_digits) + 1);
p[0] = 1;
for (int i = 1; i < (int) p.size(); i++)
p[i] = p[i - 1] * 10;
vector<int> res;
long long cur = 0;
int cur_digits = 0;
for (int i = 0; i < (int) a.size(); i++) {
cur += a[i] * p[cur_digits];
cur_digits += old_digits;
while (cur_digits >= new_digits) {
res.push_back(int(cur % p[new_digits]));
cur /= p[new_digits];
cur_digits -= new_digits;
}
}
res.push_back((int) cur);
while (!res.empty() && !res.back())
res.pop_back();
return res;
}

typedef vector<long long> vll;

static vll karatsubaMultiply(const vll &a, const vll &b) {


int n = a.size();
vll res(n + n);
if (n <= 32) {
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
res[i + j] += a[i] * b[j];
return res;
}

int k = n >> 1;
vll a1(a.begin(), a.begin() + k);
vll a2(a.begin() + k, a.end());
vll b1(b.begin(), b.begin() + k);
vll b2(b.begin() + k, b.end());

vll a1b1 = karatsubaMultiply(a1, b1);


vll a2b2 = karatsubaMultiply(a2, b2);

for (int i = 0; i < k; i++)


a2[i] += a1[i];
for (int i = 0; i < k; i++)
b2[i] += b1[i];

vll r = karatsubaMultiply(a2, b2);


for (int i = 0; i < (int) a1b1.size(); i++)
r[i] -= a1b1[i];
for (int i = 0; i < (int) a2b2.size(); i++)
r[i] -= a2b2[i];

for (int i = 0; i < (int) r.size(); i++)


res[i + k] += r[i];
for (int i = 0; i < (int) a1b1.size(); i++)
res[i] += a1b1[i];
for (int i = 0; i < (int) a2b2.size(); i++)
res[i + n] += a2b2[i];
return res;
}

bigint operator*(const bigint &v) const {


vector<int> a6 = convert_base(this->a, base_digits, 6);
vector<int> b6 = convert_base(v.a, base_digits, 6);
vll a(a6.begin(), a6.end());
vll b(b6.begin(), b6.end());
while (a.size() < b.size())
a.push_back(0);
while (b.size() < a.size())
b.push_back(0);
while (a.size() & (a.size() - 1))
a.push_back(0), b.push_back(0);
vll c = karatsubaMultiply(a, b);
bigint res;
res.sign = sign * v.sign;
for (int i = 0, carry = 0; i < (int) c.size(); i++) {
long long cur = c[i] + carry;
res.a.push_back((int) (cur % 1000000));
carry = (int) (cur / 1000000);
}
res.a = convert_base(res.a, 6, base_digits);
res.trim();
return res;
}
};
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int t;
cin >> t;
while(t--){
bigint a, b;
cin >> a >> b;
cout<<a/b<<" "<<a%b<<'\n';
}
return 0;
}
 IP
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;
string s;
bool check(int x, int y){
if(y-x+1>3) return 0;
if(s[x]=='0'){
if(y-x+1==1) return 1;
return 0;
}
if(y-x+1==1 || y-x+1==2) return 1;
int sum=100*(s[x]-'0')+10*(s[x+1]-'0')+s[y]-'0';
if(0<=sum && sum<=255) return 1;
return 0;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int t;
cin >> t;
while(t--){
cin >> s;
int n=s.length();
if(n<4 || n>12) {cout<<0<<'\n'; continue;}
int ans=0;
for(int i=0; i<n; ++i)
for(int j=i+1; j<n; ++j)
for(int p=j+1; p<n; ++p){
if(check(0,i) && check(i+1,j) && check(j+1,p) && check(p+1,n-
1)) ans++;
}
cout<<ans<<'\n';
}
return 0;
}
IDCOVUA002
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;

int t, n, a[15][15], ans, check[15][15];


void mark(int x, int y, int key){
f(j,1,n) check[x][j]+=key, check[j][y]+=key;
int z=1;
while(x+z<=n && y+z<=n) check[x+z][y+z]+=key, z++;
z=1;
while(x+z<=n && y-z>=1) check[x+z][y-z]+=key, z++;
z=1;
while(x-z>=1 && y+z<=n) check[x-z][y+z]+=key, z++;
z=1;
while(x-z>=1 && y-z>=1) check[x-z][y-z]+=key, z++;
}
void Try(int k){
if(k==n+1) ans++;
else{
for(int i=1; i<=n; ++i){
if(check[k][i]==0){
mark(k,i,1);
Try(k+1);
mark(k,i,-1);
}
}
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> t;
while(t--){
cin >> n;
f(i,1,n)f(j,1,n) a[i][j]=0;
ans=0;
Try(1);
cout<<ans<<'\n';
}
return 0;
}
IDDAYSO009
#include <bits/stdc++.h>
#define MAX 100
using namespace std;

// kiem tra xem chuoi co toan bit 1 khong


bool tat_ca_bit_1(string s, int n)
{

int co = 0;
for (int i = 0; i < s.size(); i++)
co += (s[i] == '1');

return (co == n);


}

int tim_do_dai(int arr[], string s, int n, int ind, int st, int dp[]
[3])
{
if (ind >= n)
return 0;

if (dp[ind][st] != -1)
return dp[ind][st];
if (st == 0)
return dp[ind][st] = max(arr[ind] +
tim_do_dai(arr, s, n, ind + 1, 1, dp),
tim_do_dai(arr, s, n, ind + 1, 0, dp));

else
return dp[ind][st] = max(arr[ind] +
tim_do_dai(arr, s, n, ind + 1, 1, dp), 0);
}

int maxLen(string s, int n)


{

if (tat_ca_bit_1(s, n))
return -1;

int arr[MAX] = { 0 };
for (int i = 0; i < n; i++)
arr[i] = (s[i] == '0' ? 1 : -1);

int dp[MAX][3];
memset(dp, -1, sizeof dp);
return tim_do_dai(arr, s, n, 0, 0, dp);
}

int main()
{
int t;
cin >> t;
while(t--)
{
string s;
cin >> s;
int n = s.size();
cout << maxLen(s, n) << endl;
}

return 0;
}
IDNGUYENTO004
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;

ll k;
ll solve(ll n){
ll ans = -1;
while(n%2==0) ans = 2, n/=2;
while(n%3==0) ans = 3, n/=3;
for(int i = 5; i <= sqrt(n); i+=6){
while(n%i == 0) ans = i, n/=i;
while(n%(i+2) == 0) ans = i + 2, n=n/(i+2);
}
if(n > 4) ans = n;
return ans;
}
int main () {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int t;
cin >>t;
while(t--){
cin >>k;
cout <<solve(k) <<'\n';
}
return 0;
}
IDNGUYENTO005
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;

ll t, n, check[1000005], c, snt[100005], uoc[1000005], m=5e5,


ans[1000005];
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
check[1]=1;
for(int i=2; i*i<=m; ++i){
if(check[i]==0) for(int j=i*i; j<=m; j+=i) check[j]=1;
}
for(int i=m; i>=2; --i) if(check[i]==0) snt[++c]=i;
f(i,1,c){
int tmp=snt[i];
while(tmp<=m){
if(uoc[tmp]==0){
uoc[tmp]=snt[i];
}
tmp+=snt[i];
}
}
//f(i,2,20)cout<<uoc[i]<<'\n';
f(i,1,m) ans[i]=ans[i-1]+uoc[i];
cin >> t;
while(t--){
cin >> n;
cout << ans[n] <<'\n';
}
return 0;
}
NGTO003
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;

int t;
ll n;
bool check(ll n){
if(n==0 || n==1) return 0;
if(n==2 || n==3) return 1;
if(n%2==0 || n%3==0) return 0;
for(int i=3; i<=sqrt(n); i+=2) if(n%i==0) return 0;
return 1;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> t;
while(t--){
cin >> n;
for(ll i=n; i<=10*n; ++i) if(i%2!=0) if(check(i-2)==1 &&
check(i)==1) {cout<<i<<'\n'; break;}
}

return 0;
}
TONG004
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;

int t, m, n, x[15][105], sl[15], dp[15][35][1005];


int mu(int a, int b){
int ans=1;
f(i,1,b) ans*=a;
return ans;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> t;
f(i,2,10){
f(j,1,100){
if(mu(j,i)>1000) {sl[i]=j-1; break;}
else x[i][j]=mu(j,i);
}
}
f(u,2,10) dp[u][1][x[u][1]]=1;
f(u,2,10){
f(i,1,sl[u]) dp[u][i][0]=1;
}
f(u,2,10){
f(i,2,sl[u]){
f(j,1,1000){
if(j>=x[u][i]) dp[u][i][j]=dp[u][i-1][j]+dp[u][i-1][j-x[u][i]];
else dp[u][i][j]=dp[u][i-1][j];
}
}
}
while(t--){
cin >> m >> n;
cout<<dp[n][sl[n]][m]<<'\n';
}
return 0;
}
IDKNAPSACK001
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;

int t, n, w, a[30], c[30], dp[30][30005];

int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> t;
while(t--){
cin >> n >> w;
f(i,1,n) cin >> a[i];
f(i,1,n) cin >> c[i];
f(i,1,n){
f(j,1,w){
if(j>=a[i]) dp[i][j]=max(dp[i-1][j],dp[i-1][j-a[i]]+c[i]);
else dp[i][j]=dp[i-1][j];
}
}
cout<<dp[n][w]<<'\n';
}
return 0;
}
IDFIBO005
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;

int n, a[30], s, dp[105][100005], ans[100005], t;

int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
a[1]=1, a[2]=2;
f(i,3,25) a[i]=a[i-1]+a[i-2];
//f(i,1,25) cout<<a[i]<<'\n';
dp[1][a[1]]=1;
f(i,1,25) dp[i][0]=1;
f(i,2,25){
f(j,1,100000){
if(j>=a[i]) dp[i][j]=dp[i-1][j-a[i]]+dp[i-1][j];
else dp[i][j]=dp[i-1][j];
}
}
f(i,1,100000) ans[i]=dp[25][i];
//f(i,1,n) {f(j,1,s) cout<<dp[i][j]<<" "; cout<<'\n';}
cin >> t;
while(t--){
cin >> n;
cout<<ans[n]<<'\n';
}
return 0;
}
IDFIBO004
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;
int t, f[10005], n;

int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
f[1]=0, f[2]=1;
f(i,3,32) f[i]=f[i-1]+f[i-2];
cin >>t;
while(t--){
cin >> n;
int z=0;
f(i,1,32){
f(j,i+1,32){
if(f[i]+f[j]==n){cout<<f[i]<<" "<<f[j]<<'\n'; z=1; break;}
}
if(z==1) break;
}
if(z==0) cout<<-1<<'\n';
}
return 0;
}
 IDCOBAN018
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;

int t;
ll n, ans, tmp;

int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >>t;
while(t--){
cin >> n;
tmp=2; ans=0;
while(n/tmp!=0){
ans+=n/tmp;
tmp*=2;
}
cout<<ans<<'\n';
}
return 0;
}
HOANVI001
#include <bits/stdc++.h>
using namTONGespace std;

int n, x[105], k, used[105]={};


void Output(){
for(int i = 1; i <= n; ++i)
cout << x[i] <<(i<n?" ":"\n");
}
void Try(int i){
for(int j = 1; j <= n; ++j){
if(used[j] == 0){
used[j] = 1;
x[i] = j;
}
else continue;
if(i == n) Output();
if(i < n) Try(i + 1);
used[j] = 0;

}
}

int main()
{

ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
int t; cin >>t;
while(t--){
cin >>n;
Try(1);
}
return 0;
}
IDDAYSO006
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;

int t, n, a[100005];
bool compare(int b, int c){
stack<int>x,y;
while(b!=0) x.push(b%10), b/=10;
while(c!=0) y.push(c%10), c/=10;
while(!x.empty() && !y.empty()){
if(x.top()>y.top()) return 1;
if(x.top()<y.top()) return 0;
x.pop(), y.pop();
}
if(!x.empty()) return 1;
if(!y.empty()) return 0;
return 1;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >>t;
while(t--){
cin >> n;
f(i,1,n) cin >> a[i];
sort(a+1,a+n+1,compare);
f(i,1,n) cout<<a[i];
cout<<'\n';
}
return 0;
}
HOANVI001
#include <bits/stdc++.h>
using namespace std;
short int n;
short int a[15];
bool check[15] = { 0 };

void Try(int i)
{
for (int j = 1; j <= n; j++){
if (check[j] == 0){
a[i] = j;
check[j] = 1;
}
else continue;
if (i < n) Try(i + 1);
if (i == n) {
for (short int h = 1; h <= n; h++)
cout << a[h] <<(h<n?" ":"\n");
}
check[j] = 0;
}
}
int main(){
int test;
cin >> test;
while(test--){
cin >> n;
Try(1);
}

return 0;
}
IDDAYSO001
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;

int n;
ll a[1000005];
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int t;
cin >>t;
while(t--){
cin >> n;
f(i,1,n) cin >> a[i];
ll x=a[2]-a[1];
int c=0;
f(i,2,n-1){
if((a[i+1]-a[i])!=x){c=1; break;}
}
if(c==0) cout<<"YES";
else cout<<"NO";
cout <<'\n';
}
return 0;
}
IDCOBAN016
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;

int t, n;
int main()
{
ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin >>t;
while(t--){
cin >> n;
f(i,1,(n-1)/9) cout<<"123456790";
int k=(n-1)%9;
f(i,1,k) cout<<char(48+i);
fn(i,k+1,2) cout<<char(48+i);
f(i,1,(n-1)/9) cout<<"098765432";
cout<<1<<'\n';
}
return 0;
}
COBAN012
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;

int t, a[20], b[20], d[20], c;


ll n, m, x, y, z;
ll change(int f[]){
ll ans=0;
f(j,1,c) ans=ans*10+f[j];
return ans;
}
int main()
{
ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin >>t;
while(t--){
cin >> n;
if(n==1) {cout<<1<<'\n'; continue;}
c=0;
m=n;
stack<int>st;
while(m!=0) st.push(m%10), m/=10;
while(!st.empty()) a[++c]=st.top(), st.pop();
f(i,1,c) b[i]=a[i], d[i]=a[i];
m=1;
f(i,1,c-1) m*=10;
if(n==m) {cout<<n-1<<" "<<n+1<<'\n'; continue;}
f(i,c/2+1,c) d[i]=d[c-i+1];
x=change(d);
int nho=0;
if(a[(c+1)/2]==9) nho=1, a[(c+1)/2]=0;
else a[(c+1)/2]++;
for(int i=(c-1)/2; i>=1; --i){
a[i]+=nho;
if(a[i]==10) a[i]=0;
else nho=0;
}
f(i,c/2+1,c) a[i]=a[c-i+1];
y=change(a);
if(b[(c+1)/2]==0) nho=1, b[(c+1)/2]=9;
else b[(c+1)/2]--, nho=0;
for(int i=(c-1)/2; i>=1; --i){
b[i]-=nho;
if(b[i]==-1) b[i]=9;
else nho=0;
}
f(i,c/2+1,c) b[i]=b[c-i+1];
z=change(b);
if(z%10==0) z=0;
ll ax=abs(n-x), ay=abs(n-y), az=abs(n-z);
if(ax<ay && ax<az) cout<<x<<'\n';
if(ax<az && ax==ay) cout<<x<<" "<<y<<'\n';
if(ax<ay && ax==az) cout<<z<<" "<<x<<'\n';
if(ay<ax && ay<az) cout<<y<<'\n';
if(ay<ax && ay==az) cout<<z<<" "<<y<<'\n';
if(az<ax && az<ay) cout<<z<<'\n';
}
return 0;
}
GIAITHUA001
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;

string s[100005];
void giaithua(int n){
int a[500005], m=1;
ll r=0,q;
a[0]=1;
for(int i=2;i<=n;i++){
for(int j=0;j<m;j++){
q=r;
r=(a[j]*i+r)/10;
a[j]=(a[j]*i+q)%10;
}
while(r>0){
a[m]=r%10;
m++;
r=r/10;
}
for(int p=m-1;p>=0;p--) s[i]+=char('0'+a[p]);
}
}
int t, n;
int main () {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
giaithua(10000);
cin >> t;
while(t--){
cin >> n;
if(n==0 || n==1) cout<<1<<'\n';
else cout<<s[n]<<'\n';
}
return 0;
}
GTBT003
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;

int t;
float ans, n, tmp;
int main () {
//ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> t;
while(t--){
cin >> n;
ans=0, tmp=1;
f(i,1,n){
tmp*=i;
float mu=i+1;
mu=1/mu;
ans=pow(ans+tmp,mu);
}
cout<<fixed<<setprecision(3)<<ans<<'\n';
}
return 0;
}
TEST003
#include<bits/stdc++.h>
#define ll long long
const int MOD=1e9+7;
using namespace std;

int t, ans;
ll n;
ll uoc[10005], sl[10005];
int main(){
cin >> t;
while(t--){
cin >> n;
ll m=n;
ans=0;
ll mx = -1;
if(m%2==0){
ans++;
while(m%2==0){
m/=2;
uoc[ans]=2;
sl[ans]++;
mx = max(mx,sl[ans]);
}
}

for(int i=3; i<=sqrt(m); i+=2){


if(m%i==0){
ans++;
ll dem=0;
while(m%i==0) m/=i, dem++;
uoc[ans]=i, sl[ans]=dem;
mx = max(dem,mx);
}
}
if(m!=1) uoc[++ans]=m, sl[ans]=1, mx = max(mx,sl[ans]);
//cout<<ans<<'\n';
for(int i=1; i<=ans; ++i)
if(sl[i] == mx) {cout<<uoc[i]<<" "<<sl[i]<<'\n'; break;}
for(int i=1; i<=ans; ++i) uoc[i]=0, sl[i]=0;
}

return 0;
}
IDGIAITHUA003
#include <bits/stdc++.h>
#define ll long long

using namespace std;

ll n;
int main()
{

ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
int t; cin >>t;
while(t--){
cin >>n;
ll ans = 0;
int k = 1;
while(pow(5,k) <= n){
ans += n/(pow(5,k));
k++;
}
cout <<ans<<'\n';
}
return 0;
}
COVUA001
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;

int t, n, k, x, y, visited[20][20];
int l[]={-2,-2,-1,-1,1,1,2,2};
int r[]={-1,1,-2,2,-2,2,-1,1};
bool check(int x, int y, int n){
if(1<=x && x<=n && 1<=y && y<=n) return true;
return false;
}
void Try(int a, int b, int step){
if(step==k+1) return;
visited[a][b]=1;
f(i,0,7){
if(check(a+l[i],b+r[i],n)==true){
Try(a+l[i],b+r[i],step+1);
}
}
}
int main () {
//ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> t;
while(t--){
cin >> n >> k >> x >> y;
f(i,1,n) f(j,1,n) visited[i][j]=0;
Try(x,y,0);
int ans=0;
f(i,1,n) f(j,1,n) if(visited[i][j]==1) ans++;
cout<<ans<<'\n';
}
return 0;
}
HOANVI002
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;

int t, n, k, a[200005], check[200005];


int main () {
//ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> t;
while(t--){
cin >> n >> k;
if(k==0){
f(i,1,n) cout<<i<<" ";
cout<<'\n';
continue;
}
if(n%2!=0 || k>n/2){cout<<-1<<'\n'; continue;}
f(i,1,n){
if(i+k>n) break;
if(check[i]==0) a[i]=i+k, a[i+k]=i, check[i+k]++, check[i]++;
}
int z=0;
f(i,1,n) if(check[i]!=1){z=1; break;}
if(z==0) f(i,1,n) cout<<a[i]<<" ";
else cout<<-1;
cout<<'\n';
f(i,1,n) check[i]=0;
}
return 0;
}
IDFIBO001
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;

ll t, n, f[100005];
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> t;
f[1]=1, f[2]=2;
for(int i=3; i<=10005; ++i){
f[i]=f[i-1]+f[i-2];
if(f[i]>1000000005) break;
}
while(t--){
cin >> n;
int key=0;
if(n==1) {cout<<4<<'\n'; continue;}
for(int i=1; i<=10005; ++i){
if(f[i]-i==n){key=f[i]-n; break;}
if(f[i]-i>n){
key=i; break;
}
}
cout<<n+key-1<<'\n';
}
return 0;
}
IDFIBO002
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;

ll t, n, f[100005];
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> t;
f[1]=1, f[2]=1;
for(int i=3; i<=10005; ++i){
f[i]=f[i-1]+f[i-2];
if(f[i]>1000000005) break;
}
while(t--){
cin >> n;
ll ans=0;
for(int i=1; i<=10005; ++i){
if(f[i]>n) break;
if(f[i]%2==0) ans+=f[i];
}
cout<<ans<<'\n';
}
return 0;
}
IDCOBAN025
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;
ll c[32], n, t;
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> t;
c[0]=1, c[1]=1;
for(int i=1; i<=30; ++i){
for(int j=0; j<=i; ++j){
c[i+1]+=c[j]*c[i-j];
}
}
while(t--){
cin >> n;
cout<<c[n]<<'\n';
}
return 0;
}
IDCOBAN022
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;

ll t, x, y, m;
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> t;
while(t--){
cin >> x >> y >> m;
ll ans=m/x;
ll vo=ans;
while(vo>=y){
ll doi=vo/y;
ans+=doi;
vo-=doi*y;
vo+=doi;
}
cout<<ans<<'\n';
}
return 0;
}
IDFIBO003
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;

ll n, fib[100005];
map<int,int>cnt;
int t;
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
fib[1]=1, fib[2]=1;
cnt[1]=1;
for(int i=3; i<=1005; ++i){
fib[i]=fib[i-1]+fib[i-2];
cnt[fib[i]]=1;
if(fib[i]>1000000005) break;
}
cin >> t;
while(t--){
cin >> n;
int x=n+1;
while(cnt[x]==1) x++;
cout<<x<<'\n';
}
return 0;
}
IDUOCSO006
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;

ll a, b, t, sa, sb;
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> t;
while(t--){
cin >> a >> b;
sa=0, sb=0;
f(i,2,sqrt(a)){
if(a%i==0){
if(i*i!=a){
sa+=i+a/i;
}
else sa+=i;
}}
sa++;
f(i,2,sqrt(b)){
if(b%i==0){
if(i*i!=b){
sb+=i+b/i;
}
else sb+=i;
}}
sb++;
if(a==sb && b==sa) cout<<"YES"<<'\n';
else cout<<"NO"<<'\n';
}
return 0;
}
IDUOCSO007
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;

ll n, m, t;
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> t;
while(t--){
cin >> n >> m;
ll ans=0, x=sqrt(n), y;
if(x*x==n){
if(m%x!=0) ans+=x;
x--;
}
f(i,1,x){
if(n%i==0){
if(m%i!=0) ans+=i;
y=n/i;
if(m%y!=0) ans+=y;
}
}
cout<<ans<<'\n';
}
return 0;
}
IDDEC2HEX
#include <bits/stdc++.h>
#define ll unsigned long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;

ll n;
int t;
stack<ll>st;
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> t;
while(t--){
cin >> n;
if(n==0) {cout<<0<<'\n'; continue;}
while(n!=0){
ll x=n%16;
st.push(x);
n/=16;
}
while(!st.empty()){
if(st.top()<10) cout<<st.top();
else cout<<char(st.top()+'A'-10);
st.pop();
}
cout<<'\n';
}
return 0;
}
IDBISEARCH002
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;

int n, m, b[100005];
pair<int,int>a[100005];
int binarySearch(int x){
int l=1, r=n;
while(l<=r){
int mid=(r+l)/2;
if(a[mid].fi==x && a[mid-1].fi<x) return mid;
if(a[mid].fi>=x) r=mid-1;
else l=mid+1;
}
return 0;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> m;
f(i,1,n) cin >> a[i].fi, a[i].se=i;
sort(a+1,a+n+1);
f(i,1,m) cin >> b[i];
f(i,1,m){
int x=binarySearch(b[i]);
cout<<a[x].se<<" ";
}
return 0;
}
IDCOBAN017
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;

ll n, a[1000005], ans, sum;


int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int t;
cin >> t;
while(t--){
cin >> n;
f(i,1,n) cin >> a[i];
ans=-INT_MAX, sum=0;
f(i,1,n){
sum+=a[i];
if(sum>ans) ans=sum;
if(sum<0) sum=0;
}
cout<<ans<<'\n';
}
return 0;
}
IDBISEARCH001
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;

int n, m, a[100005], b[100005];


int binarySearch(int x){
int l=1, r=n;
while(l<=r){
int mid=(r+l)/2;
if(a[mid]==x && a[mid-1]<x) return mid;
if(a[mid]>=x) r=mid-1;
else l=mid+1;
}
return 0;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> m;
f(i,1,n) cin >> a[i];
f(i,1,m) cin >> b[i];
f(i,1,m){
cout << binarySearch(b[i])<<" ";
}
return 0;
}
IDLUYTHUA002
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
const int MOD=1e9+7;
using namespace std;

int t;
ll a, n;
ll tinh(ll a, ll n){
if(n==0) return 1;
if(n%2==0){
ll tmp=tinh(a,n/2);
return (tmp*tmp)%MOD;
}
else{
ll tmp=tinh(a,n/2);
return ((tmp*tmp)%MOD*a)%MOD;
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> t;
while(t--){
cin >> a >> n;
cout<<tinh(a,n)<<'\n';
}
return 0;
}
IDBIN2DC
#include <bits/stdc++.h>
#define ll unsigned long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
using namespace std;

int t;
string s;
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> t;
while(t--){
cin >> s;
ll ans=0, tmp=1;
for(int i=s.length()-1; i>=0; --i){
if(s[i]=='1'){
ans+=tmp;
}
tmp=tmp<<1;
}
cout<<ans<<'\n';
}
return 0;
}
COBAN013
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
using namespace std;

int t, n, a[100005];
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

cin >> t;
while(t--){
cin >> n;
int s0=0, sa=0, sd=0;
f(i,1,n) {
cin >> a[i];
if(a[i]==0) s0++;
if(a[i]>0) sd++;
if(a[i]<0) sa++;
}
if(s0!=0) {cout<<-1<<'\n'; continue;}
cout<<min(sa,sd)<<'\n';
}
return 0;
}
TONG003
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
using namespace std;

int t, m, n, dp[105][105], a[105][105];


int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

cin >> t;
while(t--){
cin >> m >> n;
f(i,1,m) f(j,1,n) cin >> a[i][j];
f(i,1,n) dp[0][i]=-INT_MAX, dp[m+1][i]=-INT_MAX;
f(i,1,m) dp[i][1]=a[i][1];
f(j,2,n){
f(i,1,m){
dp[i][j]=max(max(dp[i-1][j-1],dp[i][j-1]),dp[i+1][j-1])+a[i]
[j];
}
}
int ans=-INT_MAX;
f(i,1,m) ans=max(ans,dp[i][n]);
cout<<ans<<'\n';
}
return 0;
}
NGTO002
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
using namespace std;

int t, l, r, check[1000005], dem[1000005];


int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
check[1]=1;
for(int i=2; i<=1000000; ++i){
if(check[i]==0)
for(int j=i*2; j<=1000000; j+=i){
check[j]=1;
}
}
for(int i=1; i<=1000000; ++i){
if(check[i]==0) dem[i]=dem[i-1]+1;
else dem[i]=dem[i-1];
}
cin >> t;
while(t--){
cin >> l >> r;
cout << dem[r]-dem[l-1] <<'\n';
}
return 0;
}
COBAN002
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
using namespace std;

int t;
ll l, r;
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> t;
while(t--){
cin >> l >> r;
ll ssh=r-l+1;
cout<<(l+r)*ssh/2<<'\n';
}
return 0;
}
COBAN003
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define f(i,a,b) for(int i=a; i<=b; ++i)
#define fn(i,a,b) for(int i=a; i>=b; --i)
using namespace std;

int t;
ll l, r;
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> t;
while(t--){
cin >> l >> r;
ll x=sqrt(l), y=sqrt(r);
if(x*x!=l) x++;
cout<<y-x+1<<'\n';
}
return 0;
}
GTBT005
#include<bits/stdc++.h>
#define pi 3.14159265358979323846
#define epsilon 0.00001
using namespace std;

int main() {
int t;
cin >> t;
while (t--) {
float x;
cin >> x;
while (x > (2 * pi)){
x -= 2 * pi;
}
float mau = 1.0;
float tu = x;
float sin = x;
int dau = -1;
for (int i = 1; (tu / mau) > epsilon; i++) {
tu *= x * x;
mau *= (2 * i) * (2 * i + 1);
sin += dau * (tu / mau);
dau = -dau;
}
cout << setprecision(6) << fixed << sin << endl;
}
}
COBAN010
#include<bits/stdc++.h>
#define ll unsigned long long
const int MOD=1e9+7;
using namespace std;

int t;
ll a, b, c;
ll kq(ll a, ll n){
if(n==0) return 1;
if(n==1) return a;
if(n%2 == 0){
ll tmp=kq(a,n/2);
return (tmp*tmp)%MOD;
}
else{
ll tmp = kq(a,n/2);
return ((a*tmp)%MOD*tmp)%MOD;
}
}
int main(){
cin >> t;
while(t--){
cin >>a >>b;
cout <<kq(a,b) <<'\n';

}
return 0;
}
 UOCSO005
#include<bits/stdc++.h>
#define ll long long
const int MOD=1e9+7;
using namespace std;

int t, ans;
ll n;
ll uoc[10005], sl[10005];
int main(){
cin >> t;
while(t--){
cin >> n;
ll m=n;
ans=0;
if(m%2==0){
ans++;
while(m%2==0){
m/=2;
uoc[ans]=2;
sl[ans]++;
}
}
for(int i=3; i<=sqrt(m); i+=2){
if(m%i==0){
ans++;
int dem=0;
while(m%i==0) m/=i, dem++;
uoc[ans]=i, sl[ans]=dem;
}
}
if(m!=1) uoc[++ans]=m, sl[ans]=1;
cout<<ans<<'\n';
for(int i=1; i<=ans; ++i) cout<<uoc[i]<<" "<<sl[i]<<'\n';
for(int i=1; i<=ans; ++i) uoc[i]=0, sl[i]=0;
}

return 0;
}
TONG001
/* Make it count */
#include <bits/stdc++.h>
#define f(i,x,y) for(int i=x; i<=y; ++i)
#define fn(i,x,y) for(int i=x; i>=y; --i)
#define ll long long
using namespace std;

int main(){
ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
//freopen("output.txt","w",stdout);
//freopen("input.txt","r",stdin);

int t; cin >>t;


while(t--){
ll a, b;
cin >>a >>b;
ll sum = 0;
if(a % 2 == 0 && b % 2 == 0){
int k = (b - a)/2;
sum = k*(b + a)/2;
}
if(a % 2 == 0 && b % 2 != 0){
int k = (b - a + 1)/2;
sum = k*(b + a + 1)/2;
}
if(a % 2 != 0 && b % 2 != 0){
int k = (b - a + 2)/2;
sum = k*(b + a )/2;
}
if(a % 2 != 0 && b % 2 == 0){
int k = (b - a + 1)/2;
sum = k*(b + a - 1)/2;
}
cout <<sum <<endl;
}

return 0;
}
COBAN009
#include<bits/stdc++.h>
#define ll unsigned long long
const int MOD=1e9+7;
using namespace std;

int main(){

int t; cin >>t;


while(t--){
string s;
cin >>s;
int l = s.size();
if(s[0] == '-') l--;
cout <<l <<'\n';
}

return 0;
}
 UOCSO004
#include <bits/stdc++.h>
#define ll long long
using namespace std;

int t;
ll x;
int main() {
//ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> t;
while(t--){
cin >> x;
ll n=x, uoc=2;
bool check=true;
while(n!=1){
int dem=0;
while(n%uoc==0){
dem++;
n/=uoc;
}
if(dem>1) {check=false; break;}
uoc++;
if(uoc>sqrt(n)) break;
}
if(check==false) cout<<"YES"<<'\n';
else cout<<"NO"<<'\n';
}

return 0;
}
NGTO001
#include <bits/stdc++.h>

using namespace std;


bool check1(int n){
while(n!=0){
int k = n%10;
if(k!=2 && k!=3 && k!=5 && k!=7) return false;
n/=10;
}
return true;
}
int main(){
int N = 1e6;
bool check[N + 1];
for (int i = 2; i <= N; i++) {
check[i] = true;
}
check[0] = false;
check[1] = false;
for (int i = 2; i*i <= N; i++) {
if (check[i] == true) {
for (int j = i * i; j <= N; j += i) {
check[j] = false;
}
}
}
int t; cin >>t;
while(t--){
int n, cnt=0;
cin >>n;
for(int i = 1; i <= n; ++i){
if(check[i]== true && check1(i)==true) cnt++;
}
cout <<cnt <<endl;

}
}
UOCSO001
#include<bits/stdc++.h>
#define ll unsigned long long
const int MOD=1e9+7;
using namespace std;

int main(){

int t; cin >>t;


while(t--){
int n;
cin >>n;
ll sum=0;
for(int i = 1; i <= sqrt(n); ++i){
if(n%i==0) sum += i,sum+=n/i;
}
int x =sqrt(n);
if(x*x == n) sum -= x;
cout <<sum - n <<'\n';
}

return 0;
}
COBAN004
#include <bits/stdc++.h>
#define ll long long
using namespace std;

int t, k;
int main() {
//ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> t;
while(t--){
cin >> k;
ll ans=9;
int mu=(k-1)/2;
if(k==1 || k==2) ans=9;
else{
while(mu--) ans*=10;
}
cout<<ans<<'\n';
}

return 0;
}
DEC2BIN
#include <bits/stdc++.h>
#define ll long long
using namespace std;

int t;
ll n;
stack<int>st;
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> t;
while(t--){
cin >> n;
while(n!=0) st.push(n%2), n/=2;
while(!st.empty()) {cout<<st.top(); st.pop();}
cout<<'\n';
}

return 0;
}
COBAN008
#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
string cong(string a,string b){
string s="";
if(a.size()>b.size()) b.insert(0,a.size()-b.size(),'0');
else if(a.size()<b.size()) a.insert(0,b.size()-a.size(),'0');
int tmp=0;
for(int i=a.size()-1;i>=0;i--){
tmp=a[i]+b[i]-96+tmp;
s.insert(0,1,tmp%10+48);
tmp=tmp/10;
}
if(tmp>0) s.insert(0,1,tmp+48);
return s;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin>>t;
while(t--){
string a,b;
cin>>a>>b;
cout<<cong(a,b)<<endl;
}
}
COBAN001
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int Count(int n){
int cnt=0;
while(n!=0){
cnt++;
n/=10;
}
return cnt;
}
ll SUM(ll n){
ll sum = 0;
ll m = n;
while(n!=0){
sum += pow(n%10,Count(m));
n/=10;
}
return sum;
}
int main(){
int t;
cin >> t;
while(t--){
ll n;
cin >>n;
if(SUM(n) == n) cout <<1;
else cout <<0;
cout<<'\n';
}
return 0;
}
GTBT004
#include<bits/stdc++.h>
using namespace std;

float x;
int n;
float Bp(float n, float k){
float ans = 1;
for(int i = 1; i <= k; ++i) ans*=n;
return ans;
}
int main(){
int t;
cin >> t;
while(t--){
cin >> n >> x;
float tmp=1, s=0;
for(float i=1; i <= n; ++i){
s = sqrt(Bp(x,i)+s);
}
cout<<fixed<<setprecision(3)<<s<<'\n';
}
return 0;
}
GTBT002
#include<bits/stdc++.h>
using namespace std;

float x;
int n;
int main(){
int t;
cin >> t;
while(t--){
cin >> n >> x;
float tmp=1, hiep=1, s=0;
for(int i=1; i<=n; ++i){
tmp*=x;
hiep*=i;
s+=tmp/hiep;
}
cout<<fixed<<setprecision(3)<<s<<'\n';
}
return 0;
}
IDSOLON
#include <bits/stdc++.h>
#define llt unsigned long long int

using namespace std;

// convert letter to number


int num(char s) {
return (int)s - 48;
}
string sumBignumber(string a, string b) {

string result;
// once number is empty
if (a == "")
return b;
if (b == "")
return a;
// setup by default, length a >= length b
if (a.size() < b.size()) {
swap(a, b);
}
int alen = a.size(), blen = b.size();
b.insert(0, alen - blen, '0');

// calculation
llt carry = 0;
int st = alen - 1;
while (st > 10) {
llt num1 = 0, num2 = 0;
for (int i = 0;i < 10;i++) {
llt k = pow(10, i);
num1 += num(a[st]) * k;
num2 += num(b[st]) * k;
st--;
}
llt fres = num1 + num2 + carry;
carry = fres / 10000000000;
num1 = fres % 10000000000;
string sres = to_string(num1);
sres.insert(0, (10 - sres.size()), '0');
result.insert(0,sres);
}

llt num1 = 0, num2 = 0;


for (int i = st;i >= 0 ;i--) {
llt k = pow(10, st - i);
num1 += num(a[i]) * k;
num2 += num(b[i]) * k;
}
llt fres = num1 + num2 + carry;
result.insert(0, to_string(fres));

return result;
}

// littleAnd is a less of and from 0a to 9a


vector <string> littleAnd;

// load 0a-9a into littleAnd


void Setdefine(string a) {
//clear little and. LA[0] = 0a = 0
// LA[1] = 1a = a;
littleAnd.clear();
littleAnd.push_back("0");
littleAnd.push_back(a);
string littlesum = a;

// 2a -> 9a. pushback to littleAnd


for (int i = 2;i <= 9;i++) {
littlesum = sumBignumber(littlesum, a);
littleAnd.push_back(littlesum);
}
}

string andBignumber(string a, string b) {


//create result
string result;
Setdefine(a); // load 0a-> 9a vao` little AND

int st = 0;
for (int i = b.size() - 1;i >= 0;i--) {
int temp = num(b[st++]);
if (temp != 0) {
// doc gia tri tai b[st] va thuc hien phep tinh
a*b[st]
// add i times '0' after a*b[st]
// result += a*b[st]
string acall = littleAnd[temp];
acall.insert(acall.size(), i, '0');
result = sumBignumber(result, acall);
}
}
if (result == "")
result = "0";
return result;
}
int main()
{

ifstream fin ("bignumber.in");


ofstream fout ("bignumber.out");

// Number test
int test;
fin >> test;

while (test--) {

// create num1,num2 operator


string number1, number2, operation;
fin >> number1 >> operation >> number2;
if (operation == "+" ) {
fout << sumBignumber(number1, number2) << endl;
}
if (operation == "*") {
fout << andBignumber(number1, number2) << endl;
}
}

return 0;
}
 IDSTRING002
#include<bits/stdc++.h>
using namespace std;

bool f(char x){


return x=='?';
}
int main(){
int t;
cin >> t;
while(t--){
string a, b, c;
cin >>a >> b >> c;
char q=c[1], w=c[2], e=c[4], r=c[5];
if(!f(q)&& !f(w) && !f(e) && !f(r)) {cout <<c <<'\n';
continue;}
if(f(r)) c[5] ='9';
if(f(e)) c[4] ='5';
if(f(q)==true && f(w)==true) c[1]='2', c[2] = '3';
else if(f(w)){
if(q == '2') c[2] = '3';
else c[2] = '9';
}
else if(f(q)) {
if(w > '3') c[1] = '1';
else c[1] = '2';
}
cout <<c <<'\n';
}
return 0;
}
GTBT001
#include<bits/stdc++.h>
using namespace std;

int n;
double x;
int main(){
int t;
cin >> t;
while(t--){
cin >> n >> x;
float sum=0, tmp=1, mau=0;
for(int i=1; i<=n; ++i){
tmp*=x; mau+=i;
sum+=tmp/mau;
}
cout<<fixed<<setprecision(3)<<sum<<'\n';
}
return 0;
}
POINT
#include <bits/stdc++.h>
using namespace std;
double distance(pair<double, double>A, pair<double, double>B) {
double result = sqrt((A.first - B.first)* (A.first - B.first) +
(A.second - B.second)* (A.second - B.second));
return result;
}

int main()
{
ifstream fin("points.inp");
ofstream fout("points.out");

int n;
fin >> n;
vector<pair<double, double>> xy;

for (int i = 0;i < n;i++) {


double st, nd;
fin >> st >> nd;
xy.push_back(make_pair(st, nd));
}

for (int i = 0;i < xy.size() ;i++) {


for (int j = 0;j < xy.size() ;j++) {
if(j == xy.size() - 1)
fout << setprecision(2) << fixed << distance(xy[i],
xy[j]);
else
fout << setprecision(2) << fixed << distance(xy[i],
xy[j]) << " ";
}
fout << endl;
}
}
COBAN002
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll a, b ,n;
bool check(ll n){
if(n == 0) return true;
ll k = sqrt(n);
if(k*k == n) return true;
return false;
}
bool check1(ll n){
while(n!=0){
int k = n % 10;
if(k!=0 && k!=1 && k!=4 && k!=9) return false;
n/=10;
}
return true;
}
int main(){
int t; cin >>t;
while(t--){
cin >>n;
ll l = pow(10,n-1);
ll h = pow(10,n)-1;
int k = 0, x = 0;
if(n == 1) k = 1, x = 1;
if(k == 1) cout <<0;
else{
for(ll i = l; i <= h; ++i){
if(check(i) && check1(i)){
cout <<i;
x = 1;
break;
}
}
}
if(x == 0 && k!=1) cout <<-1;
cout <<endl;
}
}
TONG001
/* Make it count */
#include <bits/stdc++.h>
#define f(i,x,y) for(int i=x; i<=y; ++i)
#define fn(i,x,y) for(int i=x; i>=y; --i)
#define ll long long
using namespace std;

int main(){
ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
//freopen("output.txt","w",stdout);
//freopen("input.txt","r",stdin);

int t; cin >>t;


while(t--){
ll a, b;
cin >>a >>b;
ll sum = 0;
if(a % 2 == 0 && b % 2 == 0){
int k = (b - a)/2;
sum = k*(b + a)/2;
}
if(a % 2 == 0 && b % 2 != 0){
int k = (b - a + 1)/2;
sum = k*(b + a + 1)/2;
}
if(a % 2 != 0 && b % 2 != 0){
int k = (b - a + 2)/2;
sum = k*(b + a )/2;
}
if(a % 2 != 0 && b % 2 == 0){
int k = (b - a + 1)/2;
sum = k*(b + a - 1)/2;
}
cout <<sum <<endl;
}

return 0;
}

You might also like