0% found this document useful (0 votes)
24 views7 pages

So Hoc 2

The documents contain C++ code for solving problems related to number theory, prime numbers, and computational geometry. The code includes functions for checking prime numbers, prime factorization, and calculating properties of numbers and geometric shapes.

Uploaded by

Baolong Nguyen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views7 pages

So Hoc 2

The documents contain C++ code for solving problems related to number theory, prime numbers, and computational geometry. The code includes functions for checking prime numbers, prime factorization, and calculating properties of numbers and geometric shapes.

Uploaded by

Baolong Nguyen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

1.

#include <bits/stdc++.h>
using namespace std;
const int N = 1e6;
int a[N],t,m;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> t;
while(t--)
{
cin >> m;
for(int j=1; j<=m; j++)
cin >> a[j];
cout << a[1] << " ";
for(int i = 1; i<m; i++)
cout << a[i]*a[i+1]/__gcd(a[i],a[i+1]) << " ";
cout << a[m] << "\n";
}
return 0;
}

2.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
ll n;
cin >> n;
ll ans = 1,Prime = 0;
ll i = 2;
while (true)
{
if (i*i > n) break;
if (n % i == 0)
{
++Prime;
ll d = 0;
while (n % i == 0)
{
d++;
n /= i;
}
ans *= (d + 1);
}
i++;
}
if (n != 1)
{
ans *= 2;
Prime++;
}
cout << ans - Prime;
return 0;
}

3.
#include <bits/stdc++.h>
using namespace std;
const int N = 32000;
int t;
int a, b;
int p[N+3];
long long res = 1;
void phan_tich(int &n)
{

for(long long i = 2; i*i <=n; i++)


{
while(n%i == 0)
{
p[i]++;
n = n/i;
}
}
if(n>1)
if(n<=N){
p[n]++;
n = 1;
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> t;
while(t--)
{
res = 1;
memset(p,0,sizeof(p));
cin >> a >> b;
phan_tich(a);
phan_tich(b);
for(auto x: p)
if(x>0)
res*=(x+1);
if(a>1 && b>1)
{
if(a!=b)
res*=4;
else
res*=3;
}
else if(a>1 || b>1)
res*=2;
cout << res <<"\n";
}
return 0;
}
4.
#include<bits/stdc++.h>
using namespace std;
long long b,m,n;
int c;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> b >> n;
c=0;
for (m=1;m<=2*n;m++)
if (((b*(m-n)*(m-n))%(n*n)==0) && (m!=n))
c++;
cout << c;
return 0;
}

5.
#include <bits/stdc++.h>
using namespace std;
long long n;
bool Is_prime(long long soA)
{
if (soA < 2)
return false;
else
for (int i = 2; i <= sqrt(soA); i ++)
if (soA%i==0)
return false;
return true;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n;
if(n<13){
cout << -1;
return 0;
}
for(long long i = 3; i*i<=n-4; i++)
{
if(Is_prime(i)==true)
{
long long z = 4+i*i;
if(Is_prime(z)==true && z<=n)
cout << 2 << " " << i << " " << z << "\n";
}
}
return 0;
}

6.
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 3;
int t, c[N];
bool p[N];
bool soNguyenTo(int soA)
{
if (soA < 2)
return 0;
for (int i = 2; i <= sqrt(soA); i++)
{
if (soA%i==0)
{
return 0;
}
}
return 1;
}
void SangSNT()
{
for(int i = 2; i<N; i++)
p[i] = 1;
for(int i = 2; i*i <= N; i++)
{
if(p[i] == 1)
for(int j = i*i; j<=N; j+=i)
p[j] = 0;
}
}
int phan_tich(int k)
{
int res = 1;
for(int i = 2; i*i<=k; i++)
{
int lt = 0;
while(k%i == 0)
{
k = k/i;
lt++;
}
res = res * (2*lt+1);
}
if(k > 1)
res = res*3;
return res;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
SangSNT();
c[0] = 0; c[1] = 0;
for(int i = 2; i<N; i++)
{
if(p[i] == 1)
c[i] = c[i-1]+1;
else
{
int k = sqrt(i);
if(k*k!=i) c[i] = c[i-1];
else
{
int t = phan_tich(k);
if(p[t] == 1)
c[i] = c[i-1]+1;
else
c[i] = c[i-1];
}
}
}
cin >> t;
while(t--)
{
int a, b;
cin >> a >> b;
cout << c[b]-c[a-1] << "\n";
}
return 0;
}

7.
#include <bits/stdc++.h>
using namespace std;
const long long N = 1e9 + 10;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
long long g, l, k, res = N, y, a , b;
cin >> g >> l;
k = l/g;
for(long long x = 1; x*x<=k; x++)
{
if(k%x==0)
{
y = k/x;
if(__gcd(x,y)==1){
res = min(res,g*(x+y));
a = g*x;
b = g*y; }
}
}
if(a*b/__gcd(a,b) == l)
cout << res;
else
cout << -1;
return 0;
}

8.
#include <bits/stdc++.h>
using namespace std;
long long n, res = 1000000000, x;
long long S(long long x)
{
long long Sum = 0;
while(x > 0)
{
Sum+=x%10;
x/=10;
}
return Sum;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n;
for(int k = 1; k<=90; k++)
{
x = (-k+(long long)sqrt(k*k+4*n))/2;
if(x*x+S(x)*x-n==0)
res = min(res,x);
}
if(res == 1000000000)
cout << -1;
else
cout << res;
return 0;
}

9.
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int t, a, b;
int x[N];
bool check_prime(int y)
{
if(y<2)
return false;
for(int i = 2; i<=sqrt(y); i++)
if(y%i == 0)
return false;
return true;
}
void solve(int j)
{
x[j] = x[j-1];
if(check_prime(j) == true)
x[j]++;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
x[1] = 0; x[2] = 1; x[3] = 2; x[0] = 0;
for(int i = 4; i<= 1000000; i++)
solve(i);
cin >> t;
while(t--)
{
cin >> a >> b;
cout << x[b]-x[a-1] << "\n";
}
return 0;
}

10.
#include <bits/stdc++.h>
using namespace std;
#define faster ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define N 1e9+3
int t;
long long solve(long long a, long long x, long long b, long long y, long long n)
{
if((a-x)+(b-y) <= n)
return x*y;
if(a-x <= n)
{
n = n - (a-x);
a = x;
if(b-y<=n)
b = y;
else
b = b- n;
}
else
a = a-n;
return a*b;
}
int main()
{
faster;
cin >> t;
while(t--)
{
long long a, b, x, y ,n;
cin >> a >> b >> x >> y >> n;
long long res = solve(a,x,b,y,n);
res = min(res, solve(b,y,a,x,n));
cout << res << "\n";
}
return 0;
}

You might also like