COBAN008

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

COBAN008:

#include<bits/stdc++.h>
using namespace std;

string findSum(string str1, string str2)


{

if (str1.length() > str2.length())


swap(str1, str2);

string str = "";

int n1 = str1.length(), n2 = str2.length();

reverse(str1.begin(), str1.end());
reverse(str2.begin(), str2.end());

int carry = 0;
for (int i=0; i<n1; i++)
{

int sum = ((str1[i]-'0')+(str2[i]-'0')+carry);


str.push_back(sum%10 + '0');

carry = sum/10;
}

for (int i=n1; i<n2; i++)


{
int sum = ((str2[i]-'0')+carry);
str.push_back(sum%10 + '0');
carry = sum/10;
}

if (carry)
str.push_back(carry+'0');

reverse(str.begin(), str.end());
return str;
}

int main()
{
int t;
cin >> t;
while (t--){
string str1;
string str2;
cin >> str1 >> str2;
cout << findSum(str1, str2) << endl;
}
return 0;
}
Coban012: #include <bits/stdc++.h>

using namespace std;

// ham kiem tra doi xung


bool Ktdoixung(string n)
{
for (int i = 0; i < n.size() / 2; i++)
if (n[i] != n[n.size() - 1 - i])
return false;
return true;
}

// chuyen tu number sang string


string NUM_to_String(long long num)
{

if (num == 0)
return "0";

string Snum = "";


while (num > 0) {
Snum += (num % 10 - '0');
num /= 10;
}
return Snum;
}

void Sodoixunggannhat(long long num)


{
// case1 : so doi xung lon nhat nho hon so da cho
long long Num1 = num - 1;

while (!Ktdoixung(NUM_to_String(abs(Num1))))
Num1--;

// Case 2 : so doi xung nho nhat lon hon so da cho

long long Num2 = num + 1;

while (!Ktdoixung(NUM_to_String(Num2)))
Num2++;

// kiem tra tri tuyet doi de in ra


if (abs(num - Num1) > abs(num - Num2))
cout << Num2;
else if(abs(num - Num1) < abs(num - Num2))
cout << Num1;
else
cout << Num1 << " " << Num2;
}

int main()
{
int t;
cin >> t;
while(t--)
{
long long num;
cin >> num;
if(Ktdoixung(NUM_to_String(num))==true)
{
cout << num;
}
else if(num<9)
{
cout << num;
}
else
Sodoixunggannhat(num);
cout << endl;
}
return 0;
}
COBAN103:

#include<bits/stdc++.h>

#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)


#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--)
#define REP(i,a) for(int i=0,_a=(a); i<_a; i++)
#define ll long long
#define F first
#define S second
#define PB push_back
#define MP make_pair
using namespace std;

const double PI = acos(-1.0);


const double e = 2.71828183;

ll nDig(int x) {
if (x <= 3) return 1;
return (ll) (1. + floor(log10(x*PI*2)/2 + log10(x/e) * x));
}

int main() {
int t ;
cin >> t ;
while(t--){
ll k; cin >> k;

int Left, Right, l, r;

// Left
l = 0, r = 1000111000; Left = r;
while (l <= r) {
int mid = (l + r) >> 1;
if (nDig(mid) >= k) {
Left = mid;
r = mid - 1;
}
else l = mid + 1;
}

// Right
l = 0, r = 1000111000; Right = l;
while (l <= r) {
int mid = (l + r) >> 1;
if (nDig(mid) <= k) {
Right = mid;
l = mid + 1;
}
else r = mid - 1;
}

if (nDig(Left) != k) {
puts("NO");
}
else {
cout << Right - Left + 1 << " ";
FOR(i,Left,Right) cout << i <<" ";
cout << endl ;
}
}
return 0 ;
}
COBAN107:

#include <bits/stdc++.h>
using namespace std;
int N;
struct mt
{
long long c[11][11];
};
int m = 1e9 + 7;
mt operator*(mt a, mt b)
{
mt 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]) % m;
}
}
}
return res;
}
mt POW(mt a, long long k)
{
if (k == 1)
return a;
if (k % 2 != 0)
return POW(a, k - 1) * a;
mt x = POW(a, k / 2);
return x * x;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
long long k;
while (t--)
{
cin >> N >> k;
mt a;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
cin >> a.c[i][j];
}
}
mt b;
b = POW(a, k);
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
cout << b.c[i][j] << " ";
}
cout << endl;
}
}
}
COVUA001:

#include <bits/stdc++.h>
#define ll long long
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;
for(int i=0; i<=7; ++i){
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;
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
visited[i][j]=0;
}
}
Try(x,y,0);
int ans=0;
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
if(visited[i][j]==1) ans++;
}
}
cout<<ans<<endl;
}
return 0;
}
SCTDL-053:

#include<bits/stdc++.h>

using namespace std;


vector<long long >a;
void Poscenter(long long n){
int t =0;
long long mu =1;
while(n/2>0){
t++;
if(t==1){
a.push_back(2);
mu=2;

}else {
mu *=2;
a.push_back(mu);
}
n/=2;
}

}
long long val(long long k,long long n,long long pos){
if(k%2==1)
return 1;
if(k<a[pos])
val(k,n/2,pos-1);
else if (k==a[pos]) return n%2;
else if(k>a[pos] )val(2*a[pos]-k,n/2,pos-1);
}
int main (){
ios_base ::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int t;
cin>>t;
while(t--){
long long n,l,r,res=0;
a.clear();
cin>>n>>l>>r;
Poscenter(n);
for(int i=l;i<=r;i++){
res+= val(i,n,a.size()-1);
}
cout<<res<<endl;
}
return 0;
}
SCTDL056;

#include <iostream>
#include <string>
using namespace std;
long long binaryStringToDecimal(string s) {
long long result = 0;
for (char c : s) {
result = result * 2 + (c - '0');
}
return result;
}

int main() {
int T;
cin >> T;

while (T--) {
string S1, S2;
cin >> S1 >> S2;

long long num1 = binaryStringToDecimal(S1);


long long num2 = binaryStringToDecimal(S2);

long long product = num1 * num2;


cout << product << endl;
}

return 0;
}

SCTDL055;

#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7, F[2][2], M[2][2];
void Mul(long long f[2][2], long long m[2][2])
{
long long x = (f[0][0] * m[0][0] % mod + f[0][1] * m[1][0] % mod) % mod;
long long y = (f[0][0] * m[0][1] % mod + f[0][1] * m[1][1] % mod) % mod;
long long z = (f[1][0] * m[0][0] % mod + f[1][1] * m[1][0] % mod) % mod;
long long t = (f[1][0] * m[0][1] % mod + f[1][1] * m[1][1] % mod) % mod;
F[0][0] = x;
F[0][1] = y;
F[1][0] = z;
F[1][1] = t;
}
void Pow(long long f[2][2], long long n)
{
if (n <= 1)
return;
Pow(f, n / 2);
Mul(f, f);
if (n & 1)
Mul(f, M);
}
long long fibo(long long n)
{
F[0][0] = F[0][1] = F[1][0] = 1;
F[1][1] = 0;
M[0][0] = M[0][1] = M[1][0] = 1;
M[1][1] = 0;
Pow(F, n - 1);
return F[0][0];
}
int main()
{
int t;
cin >> t;
while (t--)
{
long long n;
cin >> n;
cout << fibo(n) << endl;
}
}
SCTDL031:

#include <iostream>
#include <cmath>
using namespace std;

int findGrayCode(int n) {
return n ^ (n >> 1);
}

int main() {
int T;
cin >> T;

while (T--) {
int N;
cin >> N;
int grayCode = findGrayCode(N);
cout << grayCode << endl;
}

return 0;
}

SCTDL047: #include<bits/stdc++.h>

using namespace std;

int main(){

ios_base::sync_with_stdio(false);

cin.tie(0);

cin.tie(0);

int t;

cin>>t;

int n;

while (t--){

cin>>n;

int check = 0;

int so4,so7;

for(int i= n/7;i>=0;i--){

if((n-i*7)%4==0){

so7=i;

so4= (n-i*7)/4;

check =1;

break;

if(check ==1){

for(int i=1;i<=so4;i++) cout<<"4";


for(int i =1;i<=so7;i++)cout<<"7";

else

cout<<"-1\n";

cout<<endl;

return 0;

9.DAYSO007

#include <bits/stdc++.h>
#define MAX 500
using namespace std;

int D[MAX][10 * MAX];

int demNhom (int curr_pos, int curr_sum, int N, char * num){


if (curr_pos == N)
return 1;
if (D[curr_pos][curr_sum] != -1)
return D[curr_pos][curr_sum];

D[curr_pos][curr_sum] = 0;
int sum = 0;
int temp = 0;

for (int i = curr_pos; i < N; i++){


sum += (num[i] - 48);
if (sum >= curr_sum)
temp += demNhom(i+1, sum, N, num);
}

D[curr_pos][curr_sum] = temp;

return temp;
}

int main (){


int t;
cin >> t;
while (t--){
int N;
cin >> N;
char num[MAX + 1];
for (int i = 0; i < N; i++){
cin >> num[i];
}
num[N] = '\0'; //ki tu ket thuc xau
memset(D, -1, sizeof(D));
cout << demNhom(0, 0, N, num) << endl;
}
return 0;
}

10,DAYSO009

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int maxBitDifference(string s) {
int n = s.size();
int maxDiff = -1; // Khởi tạo maxDiff là -1 nếu không có bit 0 trong chuỗi

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


int count0 = 0; // Đếm số bit 0 từ vị trí i
int count1 = 0; // Đếm số bit 1 từ vị trí i

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


if (s[j] == '0') {
count0++;
} else {
count1++;
}

int diff = count0 - count1;


maxDiff = max(maxDiff, diff);
}
}

return maxDiff;
}
int main() {
int t;
cin >> t;

while (t--) {
string s;
cin >> s;

int result = maxBitDifference(s);

cout << result << endl;


}

return 0;
}

11,EXAM002

#include <bits/stdc++.h>
using namespace std;
int cmp(string a, string b)
{
a.insert(0, max(0, (int)(b.length() - a.length())), '0');
b.insert(0, max(0, (int)(a.length() - b.length())), '0');
if (a > b)
return 1;
if (a == b)
return 2;
return 3;
}
string csl(string a, string b)
{
int du = 0;
int mid = 0;
string res = "";
a.insert(0, max(0, (int)(b.length() - a.length())), '0');
b.insert(0, max(0, (int)(a.length() - b.length())), '0');
for (int i = a.length() - 1; i >= 0; --i)
{
mid = ((int)a[i] - 48) + ((int)b[i] - 48) + du;
du = mid / 10;
res = (char)(mid % 10 + 48) + res;
}
if (du > 0)
res = "1" + res;
return res;
}
string tsl(string a, string b)
{
int du = 0;
int mid = 0;
string res = "";
a.insert(0, max(0, (int)(b.length() - a.length())), '0');
b.insert(0, max(0, (int)(a.length() - b.length())), '0');
for (int i = a.length() - 1; i >= 0; --i)
{
mid = ((int)a[i] - 38) - ((int)b[i] - 48) - du;
du = (mid < 10) ? 1 : 0;
res = (char)(mid % 10 + 48) + res;
}
while (res[0] == '0' && res.length() > 1)
res.erase(0, 1);
return res;
}
int main ()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while(t--)
{
string n;
cin>>n;
cout<<csl(n,"2022")<<"\n";

}
return 0;
}

12,EXAM004

#include <bits/stdc++.h>

using namespace std;

bool isSpecialChar(char c)
{
return (ispunct(c));
}

int main()
{
string s;

while (cin >> s)


{
bool up_char = false;
bool low_char = false;
bool num_char = false;
bool special_char = false;
bool near_char = true;

if (s.size() < 8)
{
cout << "INVALID"
<< "\n";
}
else
{
for (long long i = 0; i < s.size(); ++i)
{
if (isupper(s[i]))
{
up_char = true;
}
else if (islower(s[i]))
{
low_char = true;
}
else if (isdigit(s[i]))
{
num_char = true;
}
else if (isSpecialChar(s[i]))
{
special_char = true;
}
}

for (long long i = 0; i < s.size() - 1; ++i)


{
if (s[i] == s[i + 1])
{
near_char = false;
break;
}
}

if (up_char && low_char && num_char && special_char && near_char)


{
cout << "VALID"
<< "\n";
}
else
{
cout << "INVALID"
<< "\n";
}
}
}

return 0;
}
13,BDS008

#include<iostream>
using namespace std;

const int MOD = 1e9 + 7;


int kiemtra(int x, unsigned long y, int p)
{
int res = 1;

x = x % p;

while (y > 0)
{

if (y & 1)
res = (1LL * res * x) % p;

y = y>>1;
x = (1LL * x * x) % p;
}
return res;
}
long giatri(long n)
{
return kiemtra(2, n/2 - 1, MOD);
}
int main()
{
int t;
cin>>t;
while(t--)
{
long n;
cin>>n;
if(n%2!=0) cout<<-1<<endl;
else
cout<<giatri(n)<<endl;
}
}

14,BDS010

#include<iostream>
using namespace std;
int m = 1e9 + 7;
void multiply(long long F[2][2], long long M[2][2])
{
long long x = F[0][0]%m * M[0][0]%m + F[0][1] * M[1][0]%m;
long long y = F[0][0]%m * M[0][1]%m + F[0][1] * M[1][1]%m;
long long z = F[1][0]%m * M[0][0]%m + F[1][1] * M[1][0]%m;
long long w = F[1][0]%m * M[0][1]%m + F[1][1] * M[1][1]%m;

F[0][0] = x%m;
F[0][1] = y%m;
F[1][0] = z%m;
F[1][1] = w%m;
}

// Power function in log n


void power(long long F[2][2], long long n)
{
if (n == 0 || n == 1)
return;
long long M[2][2] = { {1,1},{1,0} };

power(F, n / 2);
multiply(F, F);
if (n % 2 != 0)
multiply(F, M);
}

/* function that returns (n+1)th Fibonacci number


Or number of ways to represent n as sum of 1's
2's */
long long countWays(long long n)
{
long long F[2][2] = { {1,1},{1,0} };
if (n == 0)
return 0;
power(F, n);
return F[0][0]%m;
}

// Driver program
int main()
{
int t;
cin >> t;
while (t--) {
long long n;
cin >> n;
cout << countWays(n) << endl;
}
return 0;
}

15,DBS0009

#include <bits/stdc++.h>
#define Mod 1000000007
#define ll long long
using namespace std;
map<ll, ll> m;

ll fibo(ll n)
{
if (m.count(n))
return m[n];
ll tmp = n / 2;
if (!(n & 1))
{
return m[n] = (fibo(tmp) * fibo(tmp) + fibo(tmp - 1) * fibo(tmp - 1)) %
Mod;
}
return m[n] = (fibo(tmp) * fibo(tmp + 1) + fibo(tmp - 1) * fibo(tmp)) % Mod;
}

void solve()
{
ll n;
cin >> n;
cout << (n == 0 ? 0 : fibo(n - 1)) << endl;
}

int main()
{

m[0] = m[1] = 1;
int test = 1;
cin >> test;
// clean();
while (test--)
{
solve();
}
// pause();
return 0;
}
16,BIN2DC

#include <bits/stdc++.h>
using namespace std;

long long he_co_so_10 (string s){


string num = s;
long long value = 0;
long long base = 1;
for (int i = num.length() - 1; i >= 0; i--){
if (num[i] == '1')
value += base;
base *= 2;
}
return value;
}

int main(){
int t;
cin >> t;
while (t--){
string s;
cin >> s;
cout << he_co_so_10(s) << endl;
}
return 0;
}
17,UOCSO006

#include <bits/stdc++.h>
using namespace std;

long long sum (long long n){


long long sum = 0;
for (int i = 2; i <= sqrt(n); i++)
if (n % i == 0) {
if (i * i == n)
sum += i;
else
sum += i + n / i;
}
return sum + 1;
}

bool soBanBe (long long a, long long b){


return (sum(a) == b && sum(b) == a);
}

int main (){


int t;
cin >> t;
while (t--){
long long a,b;
cin >> a >> b;
if (soBanBe(a,b)) cout << "YES";
else cout << "NO";
cout << endl;
}
return 0;
}
18,NGTO001

#include<iostream>
#include<math.h>
using namespace std;
// Kiem tra so nguyen to
int snt(int a)
{
if(a == 0 || a == 1)
{
return 0;
}
for(int i = 2; i <= sqrt(a); i ++)
{
if(a % i == 0)
{
return 0;
}
}
return 1;
}
// Tat ca phan tu deu la so nguyen to
int allsnt(int a)
{
int count;
int dem1 = 0, dem2 = 0; //dem1 dem so luong so, dem2 dem so luong so la snt
while (a > 0)
{
count = a % 10;
if(snt(count) == 1)
{
dem2 ++;
}
a /= 10;
dem1 ++;
}
if(dem1 == dem2)
{
return 1;
}
return 0;
}
//So luong snt nho hon n
void sl(int n)
{
int count = 0;
for(int i = 2; i <= n; i ++)
{
if(allsnt(i) == 1 && snt(i) == 1)
{
count ++;
}
}
cout << count << endl;
}
int main()
{
int test,n;
cin >> test;
while(test --)
{
cin >> n;
sl(n);
}
return 0;
}
19.KTLDT010

#include <iostream>

using namespace std;

int main() {
int T;
cin >> T;

while (T--) {
long long N;
cin >> N;

// Số quân tượng lớn nhất có thể đặt trên bàn cờ nxn


// là 2 * (N - 1)
long long result = 2 * (N - 1);

cout << result << endl;


}

return 0;
}

20,DEC2HEC

#include <bits/stdc++.h>
using namespace std;
void chuyen_sang_Hexa(long long n){
char hexaDecinum[500];

long long i = 0;
if (n == 0) cout << "0" << endl;
else{
while (n != 0){
long long temp = 0;
temp = n%16;
if (temp < 10){
hexaDecinum[i] = temp +48;
i++;
}
else{
hexaDecinum[i] = temp + 55;
i++;
}
n = n/16;
}
for (long long j = i-1; j >= 0; j--){
cout << hexaDecinum[j];
}
cout << endl;
}
}

int main(){
int t;
cin >> t;
while (t--){
long long n;
cin >> n;
chuyen_sang_Hexa(n);
}
return 0;
}
21,DAYSO010

#include<bits/stdc++.h>
using namespace std;

typedef unsigned long long ull;

int main (){


int t;
cin >> t;
while (t--){
int n , l;
cin >> n >> l;
ull Sum, Sum_try = 0;
Sum = (ull)n * (2 * l + n - 1) / 2;
for (int i = 0; i < n-1; i++){
int a;
cin >> a;
Sum_try += a;
}

cout << Sum - Sum_try << endl;


}

return 0;
}

22,KLDT001

#include <iostream>
using namespace std;

long long countWays(long long n) {


long long count = 0;
long long i = 1;

while (i * (i + 1) / 2 < n) {
long long remainder = n - i * (i + 1) / 2;
if (remainder % (i + 1) == 0) {
count++;
}
i++;
}

return count;
}

int main() {
int T;
cin >> T;

while (T--) {
long long N;
cin >> N;
long long ways = countWays(N);
cout << ways << endl;
}

return 0;
}

You might also like