2020 winter vacation the first week of training

2020 cattle off winter training camp 1 rin basic algorithm and fast iterative

Topic link: https: //ac.nowcoder.com/acm/contest/3002/E

Meaning of the questions: is to give a number, and then seek its number of factors, the number of factors and then seek its number of factors, and so on, until the first factor to find the number of how many times it is a factor of the number 2.

Solution: Yes, and his teammates out of! The number of his teammates watched told me that might be violent! I spent half an hour to prove ah indeed violence is actually no need ah Hey then staged a violent results but later changed wa the right is the direct recursion, each recursive traversal √n inside the number, if it is divisible + 2 = direct answer, and finally a judgment √n alone can not be the whole, it can be re-+ = 1.

Code:

#include<cstdio>
#include<cmath>
#define sqs 0.00000000001
using namespace std;
    int cnt = 0;
    long long temp = 1;
    while(temp < sqrt(now))
    {
        if(now % temp == 0)
        {
            cnt += 2;
        }
        temp ++;
    }
    if(temp - sqrt(now) <= sqs) cnt ++;
    return cnt;
}
int main(void)
{
    long long n;
    scanf("%lld",&n);
    int cnt = 0;
    long long temp = get(n);
//  printf("temp = %lld\n",temp);
    while(temp != 2)
    {
        temp = get(temp);
//      printf("temp = %lld\n",temp);
        cnt ++;
    }
    printf("%d\n",cnt + 1);
    return 0;
}

2020 cattle off winter training camp 1 eli algorithm basis and strings

Topic link: https: //ac.nowcoder.com/acm/contest/3002/G

The meaning of problems: Q a string which contains the shortest string length k of identical letters.

Solution: First, it is for the shortest, it is surely axxxxa this is the beginning and the end are sure to meet the requirements of that letter, so I will direct the location of all the letters appear inside of string are written down, if all the letters appear less than the required number of k directly outputs 0, otherwise twenty-two subtraction, if the update condition is satisfied directly ans = min (ans, pos [i + 1] - pos [i] + 1) so.

Code:

#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
const int MAX_SIZE = 200005;
vector<int>b[26];
char a[MAX_SIZE];
int cnt[26];
int main(void)
{
 int n,k;
 scanf("%d%d",&n,&k);
 scanf("%s",a);
 for(int i = 0; i < n; i ++ )
 {
  cnt[a[i]-'a']++;
  b[a[i]-'a'].push_back(i);
 }
 int flag = 1;
 for(int i = 0; i < 26; i ++ )
 {
  if(cnt[i] >= k)
  {
   flag = 0;
   break;
  }
 }
 if(flag)
 {
  printf("-1\n");
  return 0;
 }
 int ans = INF;
 for(int i = 0 ; i < 26 ; i ++ )
 {
  if(cnt[i] < k) continue;
  else{
   int temp = 0;
   for(int j = 0 ; j < cnt[i] - k + 1 ; j ++ )
   {
//    printf("%d %d\n",b[i][j],b[i][j+k-1]);
    temp = b[i][j + k - 1] - b[i][j] + 1;
    ans = min(temp,ans);
//    printf("%d\n",ans);
   }
  }
 }
 if(ans == INF) printf("-1\n");
 else printf("%d\n",ans);
 return 0;
}

2020 cattle off winter training camp base algorithm 2 do count

Topic link: https: //ac.nowcoder.com/acm/contest/3003/E

The meaning of problems: a given n, in the range of 1 ~ 4e7, and then ask i * j <= n, and satisfies √i + √j = √k, (i, j, k) triples consisting of a positive integer, i , in a different order j can be considered different triples, asked a total of how many different triplets.

Solution: The √i + √j = √k be simultaneously on both sides of the squares of J + 2 + I √ (I J) = k, first be sure i, j are integers, k and the requirements of the subject is a positive integer, can be certainly √ (i J) is also a positive integer, so long as i * j <= n && √ (i * j) - (int) √ (i * j) <= sqs on the line. ~ ~ Do question here, when in fact the card for a long time because I always get the judge that i, j is the number to be squared, but this condition is actually unnecessary, because as long as i J is a square number, then i, j, respectively the possibility of involving the square had been included into it, you silly [beep -] ~ ~

Code:

#include<cstdio>
#include<cmath>
#include<vector>
#define sqs 0.0000000001
using namespace std;
int main(void){
    int n;
    scanf("%d",&n);
    int node = sqrt(n);
    int cnt = 0;    
    int ans = 0;    
    for(int i = 1; i * i <= n; i ++ )    
    {
        cnt = 0;
        int now = i*i;
        int temp = 1;
        while(temp < i)
        {
                    if(now % temp == 0)
                    {
                          cnt ++;
                    }            
                    temp ++;
        }        
        ans += (cnt * 2 + 1);
    }
    printf("%d\n",ans);    
    return 0;
}

2020 cattle off winter training camp algorithm basis sentenced to 2 or false

Topic link: https: //ac.nowcoder.com/acm/contest/3003/G

The meaning of problems: determining a ^ d + b ^ e + c ^ f == g, abcdefg is a 1e9.

Solution: a naked power of fast, but the modulus relatively attractive. See the official solution to a problem is to let several modulus try several times, but it is still a little bit confused I must be too dishes hum hum .

Code:

#include<cstdio>
#include<cstring>
#include<algorithm>
#define MOD 1000000009
using namespace std;
const int MAX_SIZE = 105;
char num[MAX_SIZE];
long long binaryPow(long long a, long long b, long long m) {
 long long ans = 1;
    a = a % m;
    b = b % m;
 while(b > 0) {
  if(b & 1) {
   ans = ans * a % m;
  }
  a = a * a % m;
  b >>= 1;
 }
 return ans;
}

2020 cattle off winter training camp algorithm base 3 Taurus array offside

Topic link: https: //ac.nowcoder.com/acm/contest/3004/C

Meaning of the questions: is to give an array but is not standardized wording, then if the input method to directly access an array out of bounds on the output Runtime error, if the input is not standardized, but can put the success of the assignment after assignment array output and output Undefined Behaviour, the input will be successful if the normal array-valued output and output Accept.

Solution: According to the position that is input to its X m + Y This can first be obtained in the range of 0 ~ n input within the range of m, it is up to the judgment of this step would be directly re If not within this range, otherwise the give it the whole into a one-dimensional array of rows then output on the line.

Code:

#include<cstdio>
#include<cstring>
using namespace std;
const int MAX_SIZE = 1000;
int num[3000005];
int n,m,p;
int main(void)
{
 int T;
 scanf("%d",&T);
 while(T--)
 {
  memset(num,0,sizeof(num));
  int flag = 0;
  scanf("%d%d%d",&n,&m,&p);
//  printf("%d\n",0%5);
  int hugest = n*m;
  while(p--)
  {
   int x,y,z;
   scanf("%d%d%d",&x,&y,&z);
   long long temp;
   temp = x*m + y;
   if(temp < hugest && temp >= 0)//正常输入 
   {
    if(x >= 0 && x <= n && y >= 0 && y <= m)
    {
     num[(int)temp] = z;
     //printf("temp = %lld num[temp] = %d\n",temp,num[(int)temp]);
    }
    else{
     if(flag != 1) flag = 2;
//     if(temp == 0)
//     {
//      num[0][0] = z;
//      continue;
//     }
//     if(temp % m == 0 && (temp / m) % 2 == 0)
//     {
//      y = m - 1;
//     }else if(temp % m == 0 && (temp / m) % 2 != 0) y = 0;
//     else y = temp % m ;
//     if(temp / m == 0)
//     {
//      x = 0;
//     }else x = temp / m; 
     num[(int)temp] = z;
//     printf("temp = %lld num[temp] = %d\n",temp,num[(int)temp]);
    }
//    printf("x = %d y = %d z = %d\n",x,y,z);
   }else
   {
    flag = 1;
   }
  }
  if(flag == 1)
  {
   printf("Runtime error\n");
  }else if(flag == 2)
  {
   for(int i = 0; i < n * m; i ++ )
   {
     if((i + 1) % m == 0) printf("%d\n",num[i]);
    else printf("%d ",num[i]);
   }
   printf("Undefined Behaviour\n");
  }else{
   for(int i = 0; i < n * m; i ++ )
   {
     if((i + 1) % m == 0) printf("%d\n",num[i]);
    else printf("%d ",num[i]);
   }
   printf("Accepted\n");
  }
 }
 return 0;
}

2020 cattle off winter training camp algorithm base 3 Taurus composite number k factor

Topic link: https: //ac.nowcoder.com/acm/contest/3004/H

Meaning of the questions: is the factor for each number 1 ~ n the total number of non-prime factors considered himself [1] but is not.

Solution: first pretreated with Eppendorf screen look, get all the prime numbers within a given range, and then directly and training camps that E title rapid iteration 1 as hard, it is a one more judge each step is not a prime number, if not a prime number on ans ++.

Code:

#include<cstdio>
#include<cstring>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
const int MAX_SIZE = 100005;
int num[MAX_SIZE];
bool isprime[MAX_SIZE];
int n,m; 
void sieve()
{
 for(int i = 0; i <= n; i ++ )
 {
  isprime[i] = true;
 }
 isprime[0] = isprime[1] = false;
 for(int i = 2; i <= n; i ++ )
 {
  if(isprime[i])
  {
   for(int j = 2 * i; j <= n; j += i)
   {
    isprime[j] = false;
   }
  }
 }
// for(int i = 1; i <= n ;i ++ )
// {
//  printf("%3d",i);
// }
// printf("\n");
// for(int i = 1; i <= n; i ++ )
// {
//  printf("%3d",isprime[i]);
// }
// printf("\n");
}
int ans[MAX_SIZE];
int find(int now)
{
 int ans = 0;
 int temp = 1;
 for(int i = 1; i <= sqrt(now); i ++ )
 {
  if(now % i == 0)
  {
//   printf("temp = %d now/temp = %d\n",temp,now/temp);
   if(now == i * i)
   {
    if(isprime[i] == false && i != 1)
     ans++;
   }else{
    if(isprime[i] == false && i != 1)
    {
     ans ++;
    }
    if(isprime[now/i] == false && now/i != 1)
    {
     ans ++;
    }
   }
  }
 }
// printf("%d %d\n",now,ans);
 return ans;
}
int cnt[MAX_SIZE];
int main(void)
{
 scanf("%d%d",&n,&m);
 sieve();
// find(8);
 for(int i = 1; i <= n; i ++ )
 {
  ans[i] = find(i);
 }
 for(int i = 1; i <= n; i ++ )
 {
  cnt[ans[i]]++;
 }
 for(int i = 1; i <= m; i ++ )
 {
  int ask;
  scanf("%d",&ask);
  printf("%d\n",cnt[ask]);
 }
 return 0;
}
Released seven original articles · won praise 0 · Views 100

Guess you like

Origin blog.csdn.net/qq_42804223/article/details/104242987