天天看點

uva 1464(數學)

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <map>
using namespace std;
typedef long long ll;
map<ll,ll> mmp;
int t,n;
ll x;
ll gcd(ll a,ll b)
{
    return b==0?a:gcd(b,a%b);
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        ll ans=0;
        mmp.clear();
       scanf("%d",&n);
       for(int i=1;i<=n;i++)
       {
           scanf("%lld",&x);
           if(!mmp.count(x)) mmp[x]=i;//存自己,作為map内一項
           for(map<ll,ll>::iterator it=mmp.begin();it!=mmp.end();)
           {
               ll temp=gcd(x,it->first);
               ans=max(ans,temp*(i-it->second+1));
               if(!mmp.count(temp)) mmp[temp]=it->second;
               else mmp[temp]=min(mmp[temp],it->second);//使最大公因數的i最小,使ans盡量大
               if(temp< it->first)
                 mmp.erase(it++);//如果他的最大公因數比它小,那麼它就可以被代替
                 else it++;
           }
       }
       printf("%lld\n",ans);
    }
    return 0;
}      

轉載于:https://www.cnblogs.com/Wangwanxiang/p/8433167.html