Not Adding

You have an array a1,a2,…,ana1,a2,…,an consisting of nn distinct integers. You are allowed to perform the following operation on it:

  • Choose two elements from the array aiai and ajaj (i≠ji≠j) such that gcd(ai,aj)gcd(ai,aj) is not present in the array, and add gcd(ai,aj)gcd(ai,aj) to the end of the array. Here gcd(x,y)gcd(x,y) denotes greatest common divisor (GCD) of integers xx and yy.

Note that the array changes after each operation, and the subsequent operations are performed on the new array.

What is the maximum number of times you can perform the operation on the array?

Input

The first line consists of a single integer nn (2≤n≤1062≤n≤106).

The second line consists of nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1061≤ai≤106). All aiai are distinct.

Output

Output a single line containing one integer — the maximum number of times the operation can be performed on the given array.

Examples

input

Copy

5
4 20 1 25 30

output

Copy

3

input

Copy

3
6 10 15

output

Copy

4

Note

In the first example, one of the ways to perform maximum number of operations on the array is:

  • Pick i=1,j=5i=1,j=5 and add gcd(a1,a5)=gcd(4,30)=2gcd(a1,a5)=gcd(4,30)=2 to the array.
  • Pick i=2,j=4i=2,j=4 and add gcd(a2,a4)=gcd(20,25)=5gcd(a2,a4)=gcd(20,25)=5 to the array.
  • Pick i=2,j=5i=2,j=5 and add gcd(a2,a5)=gcd(20,30)=10gcd(a2,a5)=gcd(20,30)=10 to the array.

It can be proved that there is no way to perform more than 33 operations on the original array.

In the second example one can add 33, then 11, then 55, and 22.

思路:有无数次更新,在更新中更新,以上一次更新为基础更新这次:可以从1-n范围中选出符合的答案

gcd:要想算i是不是里面的最大因数,就让i的倍数除以i,看最后gcd是不是1,如果不是1,说明不符合gcd的规则,不是最大的(因数),就不能插入。gcd=1才能插入。只要判断这个数是否存在,便能知道是否要插入这个数。(总结:判断这个数是不是gcd,就看除去这个数后,它的倍数的gcd是不是1(相当于判断它是不是最大的))

gcd模板:

int gcd(int a,int b)
{
    if(!b) return a;
    return gcd(b,a%b);
}

完整代码:

#include <iostream>
#include <algorithm>
#include <cstring>
//#include <cmath>
using namespace std;

const int N=1e6+10;
int a[N];

int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        int x;
        cin>>x;
        a[x]=1;
    }
    int ans=0;
    for(int i=1;i<=1000000;i++)
    {
        int g=0;
        for(int j=i;j<=1000000;j+=i)
        {
            if(a[j])
            g=__gcd(g,j/i);
        }
        if(g==1&&!a[i])
        {
            ans++;
        }
    }
    cout<<ans<<endl;
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
解释一下这段代码 private ImmutableList<LauncherApp> mergeWithDefaultApps( int maxLimit, ImmutableList<LauncherApp> recentApps) { int numToAdd = maxLimit - recentApps.size(); LogUtil.logD(TAG, "mergeWithDefaultApps numToAdd = " + numToAdd); if (numToAdd <= 0) { LogUtil.logDOrNotUser(TAG, "No need to merge with defaultApps"); return recentApps; } List<DefaultLauncherApp> defaultApps = loadDefaultApps(); LogUtil.logDOrNotUser(TAG, "Have to merge with " + defaultApps.size() + " default apps"); // verify that our defaults are not adding duplicates boolean enableRemovalOfDefaultRecents = enableRemovalOfDefaultRecentsProvider.get(); List<LauncherApp> matchedDefaultsList = new ArrayList<>(); for (LauncherApp app : recentApps) { Predicate<DefaultLauncherApp> matchesRecentApp = defaultApp -> matches(defaultApp, app); if (enableRemovalOfDefaultRecents) { Iterables.addAll(matchedDefaultsList, Iterables.filter(defaultApps, matchesRecentApp)); } Iterables.removeIf(defaultApps, matchesRecentApp); } if (enableRemovalOfDefaultRecents && !matchedDefaultsList.isEmpty()) { // If the user has used an app which we are populating as a default, then we can stop // promoting it in the future. hiddenDefaultRecentsHelper.hideDefaultRecentApp(matchedDefaultsList); } List<LauncherApp> mergedRecentApps = new ArrayList<>(recentApps); numToAdd = min(numToAdd, defaultApps.size()); LogUtil.logD(TAG, "numToAdd = " + numToAdd); if (numToAdd > 0) { mergedRecentApps.addAll(defaultApps.subList(0, numToAdd)); } // Try to find duplicated app and log if there is any. See b/217928263 if (LogUtil.isDorNotUser(TAG)) { Map<String, LauncherApp> appsMap = new HashMap<>(); for (LauncherApp app : mergedRecentApps) { String label = app.getLabel().toString(); LauncherApp duplicatedApp = appsMap.get(label); if (duplicatedApp != null) { LogUtil.logDOrNotUser( TAG, "Found duplicated Launcher App label:" + label + ", \nduplicated app:" + duplicatedApp + " \nand " + app); } appsMap.put(label, app); } } return ImmutableList.copyOf(mergedRecentApps); }
06-02

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值