We have given the number of iterations upto which the number of ranges should be taken as input. Like if the number of iterations(n)=2, we have to input 2 ranges. And then for each range we have to count and print the total number of integers which have exactly two divisors. Sample input/output format is given below the code.
This is my code
#include<iostream>
using namespace std;
int main() {
long long int n, a, b, count=0, j, k, flag=1, ans=0;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a>>b;
for(j=a;j<=b;j++){
if(j==1){
continue;
}
else if(j==2){
count++;
}
else{
for(k=1;k<=j;k++){
if(j%k==0){
flag=0;
}
}
if(flag==1){
ans++;
}
}
if(ans==2){
count++;
}
}
cout<<count;
}
return 0;
}
Input Format
The first line contains N , no of test cases. The next N lines contain two integers a and b denoting the range of country numbers.
Output Format
Output contains N lines each containing an answer for the test case.
Sample Input
2
1 10
11 20
Sample Output
4
4
My output
1
1
Related
Given an array count the total number of pairs of (i,j) such that A[i]*A[j] is not a perfect square.
input
n=3
input array={2,3,12}
output:2
explanation 2 * 3 i.e A[0]*A[1] & 2 * 12 i.e A[0]*A[2] does form perfect square. pair 2 * 3 & 3 * 2 are counted as a single pair.
my approach & code:
i simply run 2 for loop which gives result in quardratic time. How can i optimise it.
#include <iostream>
#include<vector>
#include<cmath>
using namespace std;
bool isPerfectSquare(long long x)
{
if (x >= 0) {
long long sr = sqrt(x);
return (sr * sr == x);
}
return false;
}
int main() {
// your code goes here
long long t;cin>>t;
while(t--){
long long n;cin>>n;
vector<long long>v(n);
for(int i=0;i<n;i++){
cin>>v[i];
}
long long count=0;
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if(!isPerfectSquare(v[i]*v[j])){
count++;
}
}
}
cout<<count<<endl;
}
return 0;
}
The trick is to take out all the square factors first, which you can trivially do by testing divisibility by all numbers up to sqrt(A[i])). Then only matching pairs produce perfect square products:
[2,3,12] -> [2,3,3]
You can determine the number of square pairs by counting the frequency of each value, and then subtract that from the total number of pairs.
Why this code is showing TLE in the time limit of 1 sec ?
taking string inputs to be of size 10^6 and 10 in number
total number of operation touch approax 10^8 ,which is inclusive in the range of 1 sec but this code shows TLE;
Question : To find a substring of particular length and particular character in a main string
ex:
Example Input
3
5 2
ab*
5 2
*a**b
5 1
abcde
Example Output
NO
YES
NO
#include <iostream>
#include<string>
using namespace std;
int main() {
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int k;
cin>>k;
string s ;
cin>>s;
string a="";
string r(k,'*');
// cout<<r<<endl;
string ans = "NO";
for(int i = 0 ; i <= n-k;i++)
{
a = s.substr(i,k);
if(a==r)
{
ans ="YES";
break;
}
}
cout<<ans<<"\n";
}
return 0;
}
That's because time complexity of substr is O(N), N being the length of the substring. So in the worst case, your code will run in O(N^2) which is not accepted if N >= 10^4
Additional:
O(N) will be accepted if N <= 10^7 while some online platforms allow it up to 10^8
I am trying to solve a problem where every letter has a respective number such as a-1,b-2....z-26.
Now given a number, in how many ways can the number be decoded is the question. consider an example where 25114 can be decoded as 'BEAN',‘BEAAD’, ‘YAAD’, ‘YAN’, ‘YKD’ and ‘BEKD’. this could be decoded in 6 ways.
I have written code in c++ but I am getting the wrong answer. Please correct my code.
#include<bits/stdc++.h>
using namespace std;
int total = 0;
int arr[100001];
void func(int start,int end,int factor){
if(start==end)
return;
int j =start;
if(factor==2&&j==end-1)//if j is the last element and factor is 2,accessing j+1 element is illegual
return;
if(factor==2){
if((arr[j]*10+arr[j+1])>26)
return;
else{
total++;
func(start+2,end,1);
func(start+2,end,2);
}
}
else{//factor is 1
total++;
func(start+1,end,1);
func(start+1,end,2);
}
}
int main(){
int n;
cin>>n;
for(int i=0;i<n;i++){
int p;
cin>>p;
arr[i]=p;
}
func(0,n,1);
func(0,n,2);
cout<<total<<endl;
return 0;
}
essentially what my code is doing is that it fixes one number from the given array(using one digit or two digits from the the given array) and recurses until all the combinations are covered. for example considering the above case, I first choose '2' as my first digit and decode it as 'B'(factor = 1) and then choose '25' and decode it as 'E'(factor = 2).
**following are the input and output from the following code
input : 25114
expected output : 6
my output : 15
input : 3333333333(10 digits)
expected output : 1
my output : 10
Based on the original program from the question I suggest to count the encodings when you reach the end only (if(start==end)).
As func will always be called twice with factor=1 and factor=2, I can freely choose either condition for counting.
Here is the modified code:
#include<bits/stdc++.h>
using namespace std;
int total = 0;
int arr[100001];
void func(int start,int end,int factor){
if(start==end) {
if(factor == 1) total++; // count once when reaching the end
return;
}
int j =start;
if((factor==2) && (j==end-1))//if j is the last element and factor is 2,accessing j+1 element is illegal
return;
if(factor==2){
if((arr[j]*10+arr[j+1])>26)
return;
else{
//total++;
func(start+2,end,1);
func(start+2,end,2);
}
}
else{//factor is 1
//total++;
func(start+1,end,1);
func(start+1,end,2);
}
return;
}
int main(){
int n;
cin>>n;
for(int i=0;i<n;i++){
int p;
cin>>p;
arr[i]=p;
}
func(0,n,1);
func(0,n,2);
cout<<total<<endl;
return 0;
}
This calculates the expected results from the example input in the question.
$ echo 5 2 5 1 1 4|./program
6
$ echo 10 3 3 3 3 3 3 3 3 3 3|./program
1
There is room for improvement.
Instead of modifying a global variable I would return the number of combinations from func and add the values in the higher level.
I would also handle the distinction between 2-digit and 1-digit numbers in the called func instead of in the caller.
Something like this pseudo code:
int func(int start, int end)
{
if(remaining length is <2) {
// we reached the end, so this is one combination
return 1;
}
if(two-digit number is >26) {
// only a 1-digit number is possible, count remaining combinations
return func(start+1, end);
}
// both a 1-digit or 2-digit number is possible, add the remaining combinations for both cases
return func(start+1) + func(start+2);
}
Your question is tagged as "dynamic-programming", but it is anything but.
Instead, think about the state space and its boundary conditions:
The empty string has zero encodings;
A single digit has a single encoding;
An n-digit string has as many encodings as an (n-1)-digit substring plus as many encodings as an (n-2)-digit substring if the first two digits are <= 26.
Thus, we can walk the string from back to front and store the intermediate results for reuse:
uint64_t solve(std::vector<int>& digits) {
const int n = digits.size();
std::vector<int> encodings(n+1);
encodings[n] = 1;
for (int i = n-1; i >= 0; i--) {
bool two_digits_fit = (i < n - 1) && (digits[i] * 10 + digits[i+1]) <= 26; // What if digits[i] == 0?
encodings[i] = encodings[i+1] + (two_digits_fit ? encodings[i+2] : 0);
}
return encodings[0];
}
Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate all prime numbers between two given numbers!
Input
The input begins with the number t of test cases in a single line (t<=10). In each of the next t lines there are two numbers m and n (1 <= m <= n <= 1000000000, n-m<=100000) separated by a space.
Output
For every test case print all prime numbers p such that m <= p <= n, one number per line, test cases separated by an empty line.
Can anyone help me optimize my code as it is showing Time limit exceeded even after i am using sieve. Here is the link to the problem
https://www.spoj.com/problems/PRIME1/
Here is my code:
#include <iostream>
#include <math.h>
using namespace std;
int is_prime(int m)
{
int i,c=0;
for(i=2;i<=sqrt(m);i++)
{
if(m%i==0)
c++;
}
if(c==0)
return 1;
else
return 0;
}
int main()
{
int n,m,i,j,k,num;
cin>>num;
for(i=1;i<=num;i++)
{
cin>>m>>n;
int a[n];
for(j=0;j<=n;j++)
a[j]=1;
for(j=m;j<sqrt(n);j++)
{
if(is_prime(j)==1)
{
for(k=j*j;k<=n;k=k+j)
{
a[k]=0;
}
}
}
for(j=m;j<=n;j++)
{
if(a[j]==1)
cout<<j<<endl;
}
cout<<endl;
}
enter code here
return 0;
}
Your code has few issues:
You cannot create 10^9 (int a[n] ) array in given time constraint!
The nested for loops are taking too long almost O(sqrt(n-m)^2)
To optimise use https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes and https://www.geeksforgeeks.org/segmented-sieve/
I want to find total factors of any number.
In number theory, factorization is the breaking down of a composite number into smaller non-trivial divisors, which when multiplied together equal the original integer. Your job is to calculate number of unique factorization(containing at least two positive integers greater than one) of a number.
For example: 12 has 3 unique factorizations: 2*2*3, 2*6, 3*4 . Note:
3*4 and 4*3 are not considered different.
I have attempted to find that but not getting exact for all.
Here is my code :
#include<iostream>
using namespace std;
int count=0;
void factor(int n,int c,int n1)
{
for(int i=n1; i<n ; i++)
{
if(c*i==n)
{count++;
return;}
else
if(c*i>n)
return;
else
factor(n,c*i,i+1);
}
return;
}
int main()
{
int num,n;
cin>>num;
for(int i=0 ; i<num ; i++)
{
cin>>n;
count=0;
factor(n,1,1);
cout<<count<<endl;
}
return 0;
}
Input is number of test cases followed by test-cases(Numbers).
Example : Input: 3 12 36 3150
Output: 3 8 91
I think you are looking for number of factorizations of a number which are unique.
For this I think you need to find the count of number of prime factor of that number. Say for
12 = 2, 2, 3
Total count = 3;
For 2, 2, 3 we need
(2*2)*3 ~ 4*3
2*(2*3) ~ 2*6
2*2*3 ~ 2*2*3
To solve this we have idea found in Grimaldi, discrete and combinatorial mathematics.
To find number of ways of adding to a number(n) is 2^(n-1) -1. For 3 we have...
3 =
1+1+1
2+1
1+2
Total count = 2^(3-1) -1 = 4-1 = 3
We can use analogy to see that
1+1+1 is equivalent to 2*2*3
1+2 is equivalent to 2*(2*3)
2+1 is equivalent to (2*2)*3
Say number of prime factors = n
So we have number of factorizations = 2^(n-1)-1
The code:
#include <stdio.h>
int power(int x, int y)
{
int prod =1, i ;
for(i=1; i<=y;i++) prod *= x;
return prod;
}
int main()
{
int number,div;
int count = 0, ti, t;
printf("Input: ");
scanf("%d",&t);
for(ti=1; ti<=t;ti++)
{
scanf("%d", &number);
div = 2;count = 0;
while(number != 0)
{
if(number%div!=0) div = div + 1;
else
{
number = number / div;
//printf("%d ",div);
count++;
if(number==1) break;
}
}
printf("%d ", power(2,count-1)-1);
}
return 0;
}
Using mod is really useful in attempting to factor:
for(int i = 1; i <= fnum; ++i){ //where fnum is the number you wish to factor
if(!(fnum % i)) ++count;
}
return count;
Of cross this is the number of factors, not unique factors, if you want the number of unique factors, you have to do some additional work.
The solution is to realize that of all permutations, precisely one is sorted. 2 * 4 * 7 * 3 gives the same result as 2 * 3 * 4 * 7. That means that when you've found one factor, you should not check the remainder for lower factors. However, you should check if the same factor appears again: 12 = 2 * 2 * 3. The sequence 2 2 3 is also sorted.
BTW, you should give your variables clearer names, or at least add some comments describing them.