One part of the new WAP portal is also a calculator computing expressions with very long numbers. To make the output look better, the result is formated the same way as is it usually used with manual calculations.
Your task is to write the core part of this calculator. Given two numbers and the requested operation, you are to compute the result and print it in the form specified below. With addition and subtraction, the numbers are written below each other. Multiplication is a little bit more complex: first of all, we make a partial result for every digit of one of the numbers, and then sum the results together.

Input

There is a single positive integer T on the first line of input (equal to about 1000). It stands for the number of expressions to follow. Each expression consists of a single line containing a positive integer number, an operator (one of +- and *) and the second positive integer number. Every number has at most 500 digits. There are no spaces on the line. If the operation is subtraction, the second number is always lower than the first one. No number will begin with zero.

Output

For each expression, print two lines with two given numbers, the second number below the first one, last digits (representing unities) must be aligned in the same column. Put the operator right in front of the first digit of the second number. After the second number, there must be a horizontal line made of dashes (-).
For each addition or subtraction, put the result right below the horizontal line, with last digit aligned to the last digit of both operands.
For each multiplication, multiply the first number by each digit of the second number. Put the partial results one below the other, starting with the product of the last digit of the second number. Each partial result should be aligned with the corresponding digit. That means the last digit of the partial product must be in the same column as the digit of the second number. No product may begin with any additional zeros. If a particular digit is zero, the product has exactly one digit -- zero. If the second number has more than one digit, print another horizontal line under the partial results, and then print the sum of them.
There must be minimal number of spaces on the beginning of lines, with respect to other constraints. The horizontal line is always as long as necessary to reach the left and right end of both numbers (and operators) directly below and above it. That means it begins in the same column where the leftmost digit or operator of that two lines (one below and one above) is. It ends in the column where is the rightmost digit of that two numbers. The line can be neither longer nor shorter than specified.
Print one blank line after each test case, including the last one.

Example

Sample Input:
4
12345+67890
324-111
325*4405
1234*4

Sample Output:
 12345
+67890
------
80235

324
-111
----
213

325
*4405
-----
1625
0
1300
1300
-------
1431625

1234
*4
---- 
4936========================================================================
solution:
#include
#include
#include
#include
#include

using namespace std;

void printhipen(int x)
{
     for(int i=0;i<x;i++)
             cout<<"-";
           
     cout<<endl;
   
}

void add(string x,int y)
{
   
     string str1=x.substr(0,y);
     string str2=x.substr(y+1);
   
     if(str1[0]=='0' || str2[0]=='0')
                     exit(0);
   
     if(str1.length()>500 || str2.length()>500)
            exit(0);
   
     char* cstr1;char* cstr2;
   
     cstr1=new char[str1.size()+1];
     cstr2=new char[str2.size()+1];
   
     strcpy(cstr1,str1.c_str());
     strcpy(cstr2,str2.c_str());
   
     int p=atoi(cstr1);
     int q=atoi(cstr2);
   
     int z=p+q;
   
     int anslen=0,j;
   
     j=z;
   
     while(j!=0)
     {
                j=j/10;
               
                anslen++;
               
     }        
               
   
     str2="+"+str2;
   
     if(str2.length()>=str1.length() && str2.length()>=anslen)
     {
                                    int k=str2.length();
                                    cout<<setw(k)<<str1<<endl;
                                    cout<<str2<<endl;
                                    printhipen(str2.length());
                                    cout<<setw(k)<<z<<endl;
                                   
                                    cout<<endl;
     }
   
     else if(str2.length()=anslen)
     {
                                    int k=str1.length();
                                    cout<<str1<<endl;
                                    cout<<setw(k)<<str2<<endl;
                                    printhipen(str1.length());
                                    cout<<setw(k)<<anslen<<endl;
                                   
                                    cout<<endl;
                                   
     }
   
     else if(str2.length()>=str1.length() && str2.length()<anslen)
     {
                                    int k=anslen;
                                    cout<<setw(k)<<str1<<endl;
                                    cout<<setw(k)<<str2<<endl;
                                    printhipen(anslen);
                                    cout<<z<<endl;
                                   
                                    cout<<endl;
     }  
   
}

void sub(string x,int y)
{
     string str1=x.substr(0,y);
     string str2=x.substr(y+1);
   
     if(str1[0]=='0' || str2[0]=='0')
                     exit(0);
   
     char* cstr1;char* cstr2;
   
     cstr1=new char[str1.size()+1];
     cstr2=new char[str2.size()+1];
   
     strcpy(cstr1,str1.c_str());
     strcpy(cstr2,str2.c_str());
   
     int p=atoi(cstr1);
     int q=atoi(cstr2);
   
     if(p500 || str2.length()>500)
            exit(0);
   
     int z=p-q;
   
     int anslen=0,j;
   
     j=z;
   
     while(j!=0)
     {
                j=j/10;
               
                anslen++;
               
     }        
               
   
     str2="-"+str2;
   
     if(str2.length()>=str1.length() && str2.length()>=anslen)
     {
                                    int k=str2.length();
                                    cout<<setw(k)<<str1<<endl;
                                    cout<<str2<<endl;
                                    printhipen(str2.length());
                                    cout<<setw(k)<<z<<endl;
                                   
                                    cout<<endl;
     }
   
     else if(str2.length()=anslen)
     {
                                    int k=str1.length();
                                    cout<<str1<<endl;
                                    cout<<setw(k)<<str2<<endl;
                                    printhipen(str1.length());
                                    cout<<setw(k)<<anslen<<endl;
                                   
                                    cout<<endl;
                                   
     }
   
     else if(str2.length()>=str1.length() && str2.length()<anslen)
     {
                                    int k=anslen;
                                    cout<<setw(k)<<str1<<endl;
                                    cout<<setw(k)<<str2<<endl;
                                    printhipen(anslen);
                                    cout<<z<<endl;
                                   
                                    cout<<endl;
     }  
}

void mult(string x,int y)
{
     string str1=x.substr(0,y);
     string str2=x.substr(y+1);
   
     if(str1[0]=='0' || str2[0]=='0')
                     exit(0);
   
     char* cstr1;char* cstr2;
   
     cstr1=new char[str1.size()+1];
     cstr2=new char[str2.size()+1];
   
     strcpy(cstr1,str1.c_str());
     strcpy(cstr2,str2.c_str());
   
     int p=atoi(cstr1);
     int q=atoi(cstr2);
   
     if(str1.length()>500 || str2.length()>500)
            exit(0);
   
     int z=p*q;
   
     int anslen=0,j;
   
     j=z;
   
     while(j!=0)
     {
                j=j/10;
               
                anslen++;
               
     }        
               
   
     str2="*"+str2;
   
     if(str2.length()>=str1.length() && str2.length()>=anslen)
     {
                                    int k=str2.length();
                                    cout<<setw(k)<<str1<<endl;
                                    cout<<str2<<endl;
                                    printhipen(str2.length());
                                    int count=0;
                                    int cal;
                                    while(q!=0)
                                    {
                                               cal=p*(q%10);
                                                     
                                                cout<<setw(k-count)<<cal<<endl;              
                                   
                                                    q=q/10;
                                                    count++;
                                    }
                                   
                                    if(count!=1)
                                    {
                                    printhipen(str2.length());
                                    cout<<setw(k)<<z<<endl;
                                    }
                                   
                                    cout<<endl;
     }
   
     else if(str2.length()=anslen)
     {
                                    int k=str1.length();
                                    cout<<str1<<endl;
                                    cout<<setw(k)<<str2<<endl;
                                    printhipen(str1.length());
                                    int count=0;
                                    int cal;
                                    while(q!=0)
                                    {
                                               cal=p*(q%10);
                                                     
                                                cout<<setw(k-count)<<cal<<endl;              
                                   
                                                    q=q/10;
                                                    count++;
                                    }
                                   
                                    if(count!=1)
                                    {
                                    printhipen(str1.length());
                                    cout<<setw(k)<<anslen<<endl;
                                    }
                                   
                                    cout<<endl;
                                   
     }
   
     else if(str2.length()>=str1.length() && str2.length()<anslen)
     {
                                    int k=anslen;
                                    cout<<setw(k)<<str1<<endl;
                                    cout<<setw(k)<<str2<<endl;
                                    printhipen(anslen);
                                    int count=0;
                                    int cal;
                                    while(q!=0)
                                    {
                                               cal=p*(q%10);
                                                     
                                                cout<<setw(k-count)<<cal<<endl;              
                                   
                                                    q=q/10;
                                                    count++;
                                    }
                                   
                                    if(count!=1)
                                    {
                                    printhipen(anslen);
                                    cout<<z<<endl;
                                    }
                                   
                                    cout<<endl;
     }
   
   
}

int main()
{
    int t;
   
    scanf("%d",&t);
   
    if(t>1000)
              exit(0);
   
    string x[t];
   
               for(int i=0;i<t;i++)
               {
                       cin>>x[i];
               }
             
               cout<<endl;
             
             
               for(int j=0;j<t;j++)
               {
               for(int i=0;i<x[j].length();i++)
               {
                       if(x[j][i]=='+')
                       {
                                    add(x[j],i);
                                    break;
                       }
                     
                       else if(x[j][i]=='-')
                       {
                            sub(x[j],i);
                            break;
                       }
                     
                       else if(x[j][i]=='*')
                       {
                            mult(x[j],i);
                            break;
                       }
                     
               }
               }
             
             
   
    system ("pause");
   
    return 0;
}

A convenient algorithm for determining the date of Easter in a given year was devised
in 1876 and rst appeared in Butcher's Ecclesiastical Handbook. The algorithm is valid
for all years in the Gregorian calendar. Subject to minor adaptations, the algorithm is as
follows:
1. Let y be the year
2. Set a to y%19
3. Set b to y/100 and c to y%100
4. Set d to b/4 and e to b%4
5. Set f to (b+8)/25
6. Set g to (b-f+1)/3
7. Set h to (19*a+b-d-g+15)%30
8. Set i to c/4 and k to c%4
9. Set l to (32+2*e+2*i-h-k)%7
10. Set m to (a+11*h+22*l)/451
11. Set n to (h+l-7*m+114)/31 and p to (h+l-7*m+114)%31
12. Determine 10*(p+1)+n
Note that all identi ers represent integers.
The value of n gives the month (3 for March and 4 for April) and the value of p+1 gives
the day of the month. These two values can be combined as 10*(p+1)+n when 23 April
would be given as 234.
Write a method private static int easter(int y) which, when presented with a
year y, returns the date of Easter in the form shown at step 12.
Incorporate this method into a complete test program. Verify that the method gives the
correct date of Easter for the current year.

solution

import java.util.Scanner;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Shobhit
 */
public class q12 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter the year y");
     
        int y = scanner.nextInt();
        int a = y% 19;
        int b = y/100;
        int c= y%100;
        int d= b/4;
        int e=b%4;
        int f = (b+8)/25;
        int g=(b-f+1)/3;
        int h=(19*a+b-d-g+15)%30;
        int i=c/4;
        int k= c%4;
        int l=(32+2*e+2*i-h-k)%7;
        int m=(a+11*h+22*l)/451;
        int n=(h+l-7*m+114)/31;
        int p= (h+l-7*m+114)%31;
        int z=10*(p+1)+n;
        System.out.println(z);
     
        System.out.println(z+n);
         

       
       
    }
   
}

Question:
Write a program to print a table of all prime numbers less than 600. Use the sieve method;
take the rst 600 integers and cross out all those that are multiples of 2, 3, 5, etc. until only
primes remain, then print out the numbers.
solution
class primesnumber
{
public static void main(String[] args)
{
int n,p;

for(int i=2;i<600;i++)
{
p=0;
for(int j=2;j<i;j++)
{
if(i%j==0)
p=1;
}
if(p==0)
System.out.println(i);
}
}
}
/*Square Inc. processes thousands of transactions daily amounting to millions of dollars. They also have a daily target that they must achieve. Given a list of transactions done by Square Inc. and a daily target your task is to determine at which transaction does Square achieves the same.

Input:
First line contains T, number of transactions done by Square in a day.
The following line contains T integers, the worth of each transactions.
Next line contains Q, the no of queries.
Next Q lines contain an integer each representing the daily target.

Output:
For each query, print the transaction number where the daily limit is achieved or -1 if the target can't be achieved.

Constraints:
1<= T <=100000
1<= Ai <=1000
1<= Target <= 109
1<=Q<=1000000

Problem statement in native language : http://hck.re/7Hb1a3

Sample Input(Plaintext Link)
 5
1 2 1 3 4
3
4
2
10

Sample Output(Plaintext Link)
 3
2
5

*/
#include
int a[100000];
long long int t;
unsigned long long int s[100000],sum[100000];
long long int binary(long long int n)
{
long long int low=0,high=t,mid=0;
while(low<=high)
{
mid=(low+high)/2;
if(sum[mid]>=n && sum[mid-1]<n)
return mid;
if(sum[mid]<n)
low=mid+1;
else if(sum[mid]>n)
high=mid-1;
}
return -2;
}
int main()
{
long long int ans;
long long int i,q,j;
scanf("%lld",&t);

 for (j=0;j<t;j++)
{
scanf("%d",&a[j]);
if(j==0)
{
sum[j]=a[j];
continue;
}
sum[j]=a[j]+sum[j-1];
}


scanf("%lld",&q);
for (i=0;i<q;i++)
scanf("%lld",&s[i]);
for(i=0;i<q;i++)
{
if(s[i]>sum[t-1])
{
printf("-1\n");
continue;
}
ans=binary(s[i]);
printf("%lld\n",ans+1);
}


return 0;
}:
Foo was not amongst the most brilliant students of his class. So, he has some pending exams to clear. As the exams are approaching, this time he vowed to pass in all of them. This will only happen if he is not under stress. Foo's stress can be calculated using a simple function called Foo_function which depends upon the time for which Foo studies continuously .
Foo_funtion is defined as follows:
F(t)=A(t^3)+B(t^2)+C*(t)+D, F(t)<=10^18
where A,B,C,D belong to the set of prime numbers. t is the time in minutes for which foo studies continuously.
As foo is not very good at solving cubic equations, he seeks your help to find out the maximum number of minutes for which he can study continuously without taking stress. Help him find t such that F(t+1) > K, and F(t) <= K, where K is the maximum stress Foo can bear.
Input:
The first line of the input contains a single integer T denoting the number of test cases. each test case consists of a single line containing 5 space seperated positive numbers abcdK.
Output:
for each test case, output a single integer t denoting the maximum time for which foo can study continuously without taking stress.
Constraints:
1 <= T <= 10^4
A, B, C, D belong to a set of prime numbers such that F(t) would never exceed 10^18
t >= 0
1 <= K <= 10^18

Sample Input
(Plaintext Link)
2
2 2 2 2 10
2 3 5 7 1000
Sample Output
(Plaintext Link)
1
7
Explanation
In the 1st test case for t = 2 foo will be under stress because F(2)=30 > K, therefore he can study for a maximum time of 1 minute without having stress.
In the 2nd test case for t = 8 foo will be under stess because F(8)=1263 > K, therefore he can study for a maximum time of 7 minutes continuously without having stress.


code:
#include
#include
using namespace std;
 
double value(int a, int b, int c, int d, unsigned long long int k, double x){
return (a*x*x*x + b*x*x + c*x + d - k);
}
 
double diff(int a,int b, int c, int d, double x){
return (a*3*x*x + b*2*x + c);
}
 
int root(int a, int b, int c, int d, unsigned long long int k){
double x = 0;double val = 10,ki;
while(val > 1 || val < -1){
val = value(a,b,c,d,k,x);
ki = diff(a,b,c,d,x);
// cout << val << " " << ki << '\n';
if(ki == 0){x = x+1; continue;};
x -= val/ki ;
}
if(value(a,b,c,d,k,floor(x)) > 0)return floor(x)-1;
return floor(x);
}
 
int main()
{
    int t,a,b,c,d;
    unsigned long long int k;
    cin >> t;
    while(t--){
     cin >> a >> b >> c >> d >> k;
     cout << root(a,b,c,d,k) << '\n';
    }
    
    return 0;
}

TEST - Life, the Universe, and Everything


Your program is to use the brute-force approach in order to find the Answer to Life, the Universe, and Everything. More precisely... rewrite small numbers from input to output. Stop processing input after reading in the number 42. All numbers at input are integers of one or two digits.

Example

Input:
1
2
88
42
99

Output:
1
2
88
--------------------------------------------------------------------------------------------------------#include 
using namespace std;

int main() {
int n;
while(1) {
cin >> n;
if (n == 42) break;
cout << n << endl;
}
return 0;
}
--------------------------------------------------------------------------------------------
output

/*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. */
--------------------------------------------------------------------------------------------------------------------------
#include
#include
#include
#include
using namespace std;

int main() {
    vector primes;
    primes.push_back(2);

    for (int i = 3; i <= 32000; i+=2) {
        bool isprime = true;
        int cap = sqrt(i) + 1;

        vector::iterator p;
        for (p = primes.begin(); p != primes.end(); p++) {
            if (*p >= cap) break;
            if (i % *p == 0) {
                isprime = false;
                break;
            }
        }
        if (isprime) primes.push_back(i);
    }

    int T,N,M;

    cin >> T;

    for (int t = 0; t < T; t++) {
        if (t) cout << endl;

        cin >> M >> N;
        if (M < 2) M = 2;

        int cap = sqrt(N) + 1;

        set notprime;
        notprime.clear();

        vector::iterator p;
        for (p = primes.begin(); p != primes.end(); p++) {

            if (*p >= cap) break;
            int start;

            if (*p >= M) start = (*p)*2;
            else start = M + ((*p - M % *p) % *p);

            for (int j = start; j <= N; j += *p) {
                notprime.insert(j);
            }
        }

        for (int i = M; i <= N; i++) {
            if (notprime.count(i) == 0) {
                cout << i << endl;
            }
        }

    }
    return 0;
}
--------------------------------------------------------------------------------------------------------------------------
output



Hello World!


<?php
// single-line comments can be like this
# or even like this
/* multi-line comments can
be like this */ ?>

Examples




<?php
// The semicolon at the end of the statement is important!
?>
<?php
// print works like echo
print "Hello World!";
?>


<?php
// concatenation
echo "Hello" . " World!
";

// simple math
echo 2 + 3;

?>



// join my fb group :_
https://www.facebook.com/groups/1068660546488118/