javacoder1's blog

By javacoder1, history, 8 years ago, In English

I was trying this question "http://mirror.codeforces.com/contest/27/problem/E"

Code is giving the correct answer on my machine for values say 1000 but in custom invocation it gives 0

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll dp[12][1005];
int arr[]={0,2,3,5,7,11,13,17,19,23,29};
int base(int num)
{
    ll ans=num;
    for(int i=2;i<=1000;i++)
    {
        if( (ans*num)/num == ans )
            ans=ans*num;
        else
            return i;
    }
}
ll temp[100];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;cin>>n;
    for(int i=1;i<=10;i++)
        for(int j=1;j<=n;j++)
        dp[i][j]=2e18;
    dp[1][1]=1;
    for(int i=2;i<=1000;i++)
    {
        if( (dp[1][i-1]*2)/2== dp[1][i-1] )
            dp[1][i]=2*dp[1][i-1];
        else
            break;
    }

    temp[0]=1;
    for(int i=2;i<=10;i++)
    {
        int till=base(arr[i]);
        for(int j=1;j<till;j++)
            temp[j]=temp[j-1]*1ll*arr[i];

        for(int j=1;j<=n;j++)
        {
            for(int k=0;k<till;k++)
            {

                if((dp[i-1][j] * temp[k])/temp[k] == dp[i-1][j] && j*(k+1)<=n)
                  {dp[i][j*(k+1)]=min(dp[i][j*(k+1)],dp[i-1][j]*temp[k]);}
                else
                   break;

            }
        }
        for(int j=1;j<=n;j++)
            dp[i][j]=min(dp[i][j],dp[i-1][j]);
    }
    cout<<dp[10][n]<<"\n";
}
  • Vote: I like it
  • -41
  • Vote: I do not like it

»
8 years ago, # |
  Vote: I like it +13 Vote: I do not like it

You are invoking undefined behavior. This article will enlighten you http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html#optimizations