রবিবার, ৩১ মার্চ, ২০১৯

The maximum possible product of digits among all integers from 1 to n.

In the first example the maximum product is achieved for 389 (the product of digits is 389=216).
In the second example the maximum product is achieved for 7 (the product of digits is 7).
In the third example the maximum product is achieved for 999999999 (the product of digits is 99=387420489).



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

#define fi(i,a,b)    for(long long i=a;i<=b;i++)
#define fr(i,a)      for(long long i=0;i<a;i++)
#define fd(i,a,b)    for(long long i=b;i>=a;i--)
#define clr(x)       memset(x,0,sizeof(x))
#define cln(x)       memset(x,-1,sizeof(x))
#define __           printf(" ")
#define _            printf("\n")
#define _o           printf("1\n")
#define stree        long long lft=node<<1,rht=(node<<1)|1,mid=(s+e)>>1
#define snode        long long s,long long e,long long node
#define slft         s,mid,lft
#define srht         mid+1,e,rht
#define sin()        if(S<=s&&e<=E)
#define sout()       if(S>e||s>E)
#define mod          1000000007
#define read()       freopen("in.txt","r",stdin)
#define write()      freopen("out.txt","w",stdout)
#define sfl(x)       scanf("%I64d",&x)
#define sfll(x,y)    scanf("%I64d %I64d",&x,&y)
#define sflll(x,y,z) scanf("%I64d %I64d %I64d",&x,&y,&z)
#define pfl(x)       printf("%I64d",x)
#define pfll(x,y)    printf("%I64d %I64d",x,y)
#define pflll(x,y,z) printf("%I64d %I64d %I64d",x,y,z)
#define xx           1000000

typedef long long ll;
typedef pair<long long,long long> pll;



main(){
    long long n;
    cin>>n;
    long long cur=1,ans=0,pre=1,v;
    string s;
    while(n){
        s+=(char(n%10)+'0');
        n/=10;
        cur*=9;
    }

    long long sz=s.size();sz--;

    //cout<<s<<" "<<cur<<endl;

    ans=cur/9;

    while(sz>=0){
        v=s[sz]-'0';
        cur/=9;
        ans=max(ans,max(pre*(v-1)*cur,pre*v));
        pre*=v;
        sz--;
    }
    cout<<ans<<endl;




    return 0;
}






Again


//#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>
#include<cstdio>
#include<vector>
#include<stdio.h>
#include<cstring>
#include<algorithm>
#include<string>
#include<iostream>
#include<map>
#include<set>
#include<queue>
#include<set>
#include<cmath>
#include<cstdlib>
#define pi                  acos(-1)
#define READ                freopen("in.txt", "r", stdin)
#define WRITE               freopen("out.txt", "w", stdout)
#define INF9                 1000000010
#define INF18               1000000000000000010
#define dist(ax,ay,bx,by)   sqrt((ax-bx)*(ax-bx)+(ay-by)*(ay-by))
#define mod                  1000000007
#define gcd(a,b)            __gcd(a,b)
#define lcm(a,b)            (a*b)/__gcd(a,b)
#define m_p(a,b)            make_pair(a,b)
#define pb                  push_back
#define bpll(a)             __builtin_popcountll(a)
#define MX                  100005

typedef long long lli;
typedef unsigned long long llu;
using namespace std;

vector<lli>ara;



lli func(lli idx, bool small)
{
//    cout<<idx<<endl;
    if(idx==ara.size()) return 1;

    lli ret=0;
    if(small)
    {
        ret= 9*func(idx+1, small);
    }
    else
    {
        ret=0;
        for(lli i=1; i<ara[idx]; i++)
        {
            ret= max(ret, i*func(idx+1, true));
        }
        ret= max(ret, ara[idx]*func(idx+1, small));
    }
    return ret;
}


int main()
{
    lli n;

    cin>>n;

    while(n)
    {
        lli rem= n%10;
        ara.push_back(rem);
        n/=10;
    }

    reverse(ara.begin(), ara.end());

    lli ans= func(0, false);
    ans= max(ans, func(1, true));
    cout<<ans<<endl;


    return 0;
}


কোন মন্তব্য নেই:

একটি মন্তব্য পোস্ট করুন

Factorization with prime Sieve

vector <int> prime; char sieve[1000009]; int N=1000009; void primeSieve ( ) { sieve[0] = sieve[1] = 1; prime.push_back(2); ...