• dada的GCD


    dada的GCD

    Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other)
    Total Submission(s) : 18   Accepted Submission(s) : 12

    Font: Times New Roman | Verdana | Georgia

    Font Size: ← →

    Problem Description

    C语言都学过了怎么计算两个数的最大公约数,而一段区间[L,R]的GCD即这段区间所有数的最大公约数。现在给你一串长度为n的序列,如果对于序列的任意子区间[L,R],都有这段区间的gcd>=2,那么这段序列就叫做dada的GCD序列。
    n<=10^4
    序列的每个数小于10^9

    Input

    第一行有一个整数t,代表t组数据
    每组输入有一个正整数n,
    随后一行n个正整数。

    大量输入,使用cin的同学请关闭stdio同步

    Output

    如果是dada的GCD序列,就输出Yes,反之输出No

    Sample Input

    2
    3
    2 6 4
    3
    4 6 9
    

    Sample Output

    Yes
    No

    说道这题满满的不爽,我竟然在这里超时了N次;
    我把求最大公约数的子函数用了辗转相减法(哭死);
    其实题意一开始也不怎么清楚,其实就是所有的数的最大公约数大于等于2就行了,直接遍历;

     1 #include <iostream>  //dadadeGCD  1007
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #define ll long long
     7 #define N 10005
     8 using namespace std;
     9 ll gcd(int a,int b){
    10     return b?gcd(b,a%b):a;
    11 }
    13 ll a[N];
    14 int main() {
    15     int n;
    16     scanf("%d",&n);
    17     while(n--){
    18         int m;
    19 
    20         scanf("%d",&m);
    21         for(int i=0;i<m;i++){
    22             scanf("%d",&a[i]);
    23         }
    24         if(m==1){
    25             if(a[0]>=2)
    26                 cout<<"Yes"<<endl;
    27             else
    28                 cout<<"No"<<endl;
    29         }else{
    30             ll z=gcd(a[0],a[1]);
    31             for(int i=2;i<m;i++){
    32                 z=gcd(z,a[i]);
    33             }
    34             if(z>=2)
    35                 cout<<"Yes"<<endl;
    36             else
    37                 cout<<"No"<<endl;
    38         }
    39     }
    40     return 0;
    41 }


  • 相关阅读:
    [程序员修炼之道]读书笔记
    protobufnet 简介
    提高IIS响应能力
    彻底解决性能问题,就因为一个配置
    2009年12月小记(Split,netstat,SortedList)
    如何在vmwarer中的linux安装lrzsz文件共享工具
    Liunx常用命令
    正则表达式 浅析
    java的final关键字
    JAVA正则表达式 Pattern和Matcher
  • 原文地址:https://www.cnblogs.com/zllwxm123/p/7260395.html
Copyright © 2020-2023  润新知