Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 吐出数组,使gcd(val[i],val[j])>1_C++ - Fatal编程技术网

C++ 吐出数组,使gcd(val[i],val[j])>1

C++ 吐出数组,使gcd(val[i],val[j])>1,c++,C++,只有当gcdval[i],val[j]>1时才是好的数组 这里, gcda,b=两个数的最大公约数 拆分数组有一个参数 Val:n个整数的整数数组 这里有两个例子 Sample Input 0: 5 // no of value in an integer 2 3 2 3 3 Sample Output 0: 2 Sample Input 1: 5 //no of value in an integer 3 5 7 11 2 Sample Output 1: 5 示例输入0 子阵

只有当gcdval[i],val[j]>1时才是好的数组

这里,

gcda,b=两个数的最大公约数

拆分数组有一个参数

Val:n个整数的整数数组

这里有两个例子

Sample Input 0:
5 // no of value in an integer
2
3
2
3
3
Sample Output 0:
2

Sample Input 1:
5   //no of value in an integer
3
5
7
11
2

Sample Output 1:
5   
示例输入0

子阵列[1..3]={2,3,2}此处gcd2,2>1

子阵列[4..5]={3,3}gcd3,3>1

#include <bits/stdc++.h>

using namespace std;

string ltrim(const string &);
string rtrim(const string &);

现在如何实现splitTheArray功能?

您需要找到子阵列的最小数量,以便在每个子阵列中,第一个和最后一个元素的gcd>1。通过使用ˆ2复杂性,您可以轻松地完成此任务

int splitTheArray(vector<int> val) {
 // implement this function
 int sz = val.size();
 if(sz == 0) return 0;
 int ind = sz - 1;
 int subarray = 0;
 while(ind >= 0) {
     for(int i = 0; i <= ind; i++) {
         if(__gcd(val[ind], val[i]) > 1) {
             subarray++;
             ind = i-1;
             break;
         }
     }
 }
 return subarray;
}
包括 包括 包括 包括 使用名称空间std; 内特力a,内特力b { 如果b==0 返回a; 返回gcdb,a%b; } 整数最大整数a,整数b { 返回a>b?a:b; } 内部最小值a,内部最小值b { 返回a1 返回0; int con=0,flag=0,j=0,i=0,flag2=0; 对于i=j;i=vec.size 打破 int f=向量[i]; flag=0; 对于j=i+1;j1 con++; 其他的 打破 } 如果n>1 { flag=1; flag2=1; con++; } } } 如果!flag2 返回向量大小; 返回con; } int main { int n; cin>>n; 向量向量机; 对于int i=0;i>tm; vec.emplace_backtm; } 库特 时间复杂度:N*SqrtmaxA[i]
另外,可以使用筛子优化因子计算,而不是每次计算每个数字的因子。

这是一个hackerank问题吗?是的,你能帮我@sha111提供它的链接吗?要理解它,@sha111你不能访问它,我们必须拆分数组,这样子数组的gcd的最后和第一个值必须大于tHAN 1??这是问题??int子数组=0;这是不正确的。考虑测试用例[10,13,17,9,23,6,6,29,3]和[3,29,6,6,23,9,17,13,10]。
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define boost ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
void solve()
{
    int n,i,j;
    cin>>n;
    int A[n+1],DP[n+1];
    for(i=1;i<=n;i++)
        cin>>A[i];
    memset(DP,0,sizeof(DP));
    unordered_map<int,int> M;
    for(i=1;i<=n;i++)
    {
        vector<int> Fact;
        Fact.push_back(A[i]);
        for(j=2;j*j<=A[i];j++)
        {
            if(A[i]%j==0)
            {
                if(j*j==A[i])
                {
                    Fact.push_back(j);
                }
                else
                {
                    Fact.push_back(j);
                    Fact.push_back(A[i]/j);
                }
            }
        }
        int ans=DP[i-1]+1;
        for(j=0;j<Fact.size();j++)
        {
            if(M.find(Fact[j])==M.end())
            {
                M[Fact[j]]=DP[i-1];
            }
            else
            {
                ans=min(ans,M[Fact[j]]+1);
            }
        }
        DP[i]=ans;
    }
    cout<<DP[n]<<endl;
}
int32_t main()
{
    boost;
    int  t=1;
    // cin>>t;
    for(int i=1;i<=t;i++)
    {
        //cout<<"Case #"<<i<<": ";
        solve();
    }
}