zoukankan      html  css  js  c++  java
  • dada的GCD ( jxnu acm新生选拔赛)

    1007 dada的GCD,输入格式描述有误,已修正

    dada的GCD

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

    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
    

    Author

    Luke叶

    Source

    jxnu 
    思路:每次都求验证是否最大公约数是否大于等于2,如果是,输出"Yes",否则输出“No” 
    这题数据很水,暴力可以过的。
    我当时觉得用gcd是不是会爆内存。于是选择暴力了。
    暴力代码:
     1 #include<iostream>
     2 #include<stdio.h>
     3 #include<string.h>
     4 #include<algorithm>
     5 using namespace std;
     6 int a[100050];
     7 int s[1005];
     8 bool su(int x){
     9     if(x%2==0) return false;
    10     else {
    11         for(int i=3;i*i<=x;i=i+2){
    12             if(x%i==0) return false;
    13         }
    14         return true;
    15     }
    16 }
    17 int main()
    18 {
    19     int T;
    20     s[0]=2;
    21     int t=1;
    22     for(int i=3;t<1003;i++)
    23         if(su(i)){
    24             s[t]=i;
    25             t++;
    26         }
    27     cin>>T;
    28     while(T--)
    29     {
    30         int n;
    31         scanf("%d",&n);
    32         memset(a,0,sizeof(a));
    33         for(int i=0;i<n;i++)
    34             scanf("%d",&a[i]);
    35         bool flag=false;
    36         for(int i=0;i<t;i++)
    37         {
    38             int sum=0;
    39             for(int j=0;j<n;j++){
    40                 if(a[j]%s[i]==0){
    41                     sum++;
    42                 }
    43             }
    44             if(sum==n){
    45                 flag=true;
    46                 break;
    47             }
    48         }
    49         if(flag) cout<<"Yes"<<endl;
    50         else cout<<"No"<<endl;
    51     }
    52     return 0;
    53 }

    正版AC代码:

     1 #include<iostream>
     2 #include<stdio.h>
     3 using namespace std;
     4 int a[10005];
     5 int gcd(int a,int b)
     6 {
     7     if(a==0) return b;
     8     else{
     9         return gcd(b%a,a);
    10     }
    11 }
    12 int main()
    13 {
    14     int T;
    15     cin>>T;
    16     while(T--){
    17         int n;
    18         cin>>n;
    19         for(int i=0;i<n;i++){
    20             scanf("%d",&a[i]);
    21         }
    22         if(n==1){
    23             if(a[0]>=2) cout<<"Yes"<<endl;
    24             else cout<<"No"<<endl;
    25         }else{
    26             int ans=gcd(a[0],a[1]);
    27             bool flag=true;
    28             for(int i=2;i<n;i++){
    29                 ans=gcd(ans,a[i]);
    30                 if(ans<2){
    31                     flag=false;
    32                     break;
    33                 }
    34             }
    35             if(flag) cout<<"Yes"<<endl;
    36             else cout<<"No"<<endl;
    37         }
    38     }
    39     return 0;
    40 }
  • 相关阅读:
    入门菜鸟
    FZU 1202
    XMU 1246
    Codeforces 294E Shaass the Great 树形dp
    Codeforces 773D Perishable Roads 最短路 (看题解)
    Codeforces 814E An unavoidable detour for home dp
    Codeforces 567E President and Roads 最短路 + tarjan求桥
    Codeforces 567F Mausoleum dp
    Codeforces 908G New Year and Original Order 数位dp
    Codeforces 813D Two Melodies dp
  • 原文地址:https://www.cnblogs.com/ISGuXing/p/7257373.html
Copyright © 2011-2022 走看看