zoukankan      html  css  js  c++  java
  • 【水】洛谷U291 Subarray GCD

    地址http://www.luogu.org/problem/show?pid=U291

    其实codechef也有:Subarray GCD

    学长代码:http://hzwer.com/4510.html

    题目背景 Background

    数学题怎能没有Gcd???

    题目描述 Description

    给出N个正整数a(a<=10^9),求gcd=1(所有数的最大公约数)的连续子序列的最大长度(1<=i<j<=N)。

    输入格式:


    第一行一个正整数N(N<=10^5),第二行N个正整数。

    输出格式:


    有这样的连续子序列就输出其长度,否则 输出“No solution”

     入样例:Sample IN

    3
    1 1 1

    输出样例:Sample OUT

    3

    思路 thinkings

    尼玛数据只有10^5啊。。不是10^9。。囧

    如果有两个数gcd==1那么所有数的gcd一定为1。。也就是说如果有两个数gcd==1就输出n如果没有就输出No solution

    推下去,三个数也可以…….

    所以:发现如果一段子序列的gcd=1的话那么整段的gcd也等于1

    代码codes

    #include<iostream>  
    #include<cstdlib>  
    #include<cstring>  
    #include<cstdio>  
      
    using namespace std;  
    int n,k,ans;  
      
    inline int read()  
    {  
        int x=0,f=1;char ch=getchar();  
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}  
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}  
        return x*f;  
    }  
      
    int gcd(int x,int y)  
    {  
        return y==0 ? x : gcd(y,x%y);  
    }  
      
    int main()  
    {  
        cin>>n;   cin>>ans;  
        for (int i=2;i<=n;i++)  
        {  
            cin>>k;  
            ans=gcd(ans,k);  
        }  
        if (ans==1) cout<<n<<endl; else cout<<"No solution"<<endl;  
        return 0;     
    }  

     

  • 相关阅读:
    codeforces1191B Tokitsukaze and Mahjong 哈希+思维
    洛谷P1608 路径统计 最短路变种 dijkstra算法
    自考新教材-p90_5(4)
    自考新教材-p90_5(3)
    自考新教材-p90_5(2)
    自考新教材-p90_5(1)
    自考新教材-p89_3
    自考新教材-p88_4(2)
    自考新教材-p88_4(1)
    自考新教材-p87_3
  • 原文地址:https://www.cnblogs.com/seekdreamer/p/4056593.html
Copyright © 2011-2022 走看看