Blue Bridge 31 days | 4 questions today Day9 | C++

1. Maximum Product

insert image description here

#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
const int N=12;
bool st[N];
int s[N];
vector<int>num;
long long maxans=0;
int divide(int l,int r)
{
    
    
    int t=0;
    for(int i=l; i<r; i++)
    {
    
    
        t*=10;
        t+=num[i];
    }
    return t;
}
bool judge(int x,int y)
{
    
    
    memset(s,0,sizeof s);
    long long k=(long long)x*y;
    long long sum=k;
    int w=0;
    while(sum)
    {
    
    
        s[sum%10]++;
        sum/=10;
        w++;
    }
    if(w!=9)
        return false;
    for(int i=1; i<=9; i++)
    {
    
    
        if(s[i]!=1)
            return false;
    }

    maxans=max(maxans,k);

    return true;
}
void dfs(int u)
{
    
    
    if(u==9)
    {
    
    

        for(int i=1; i<9; i++)
        {
    
    
            int x=divide(0,i);
            int y=divide(i,9);
            judge(x,y);
        }
        return;
    }

    for(int i=1; i<=9; i++)
    {
    
    
        if(!st[i])
        {
    
    
            st[i]=true;
            num.push_back(i);
            dfs(u+1);
            num.pop_back();
            st[i]=false;
        }
    }
}
int main()
{
    
    
    dfs(0);
    printf("%lld",maxans);
    return 0;
}

2. Factorial Divisor

insert image description here

Any positive integer X can be expressed in the form of the product of several prime numbers, that is, X = p1α1 ∗ p2α2  … ∗ pkαk

Divisors = (a1 + 1)(a2 + 1)...(ak + 1)

#include <iostream>
#include <unordered_map>
using namespace std;
unordered_map<int,int>h;
int main()
{
    
    
  for(int i=2;i<=100;i++){
    
    
      int t=i;
      for(int j=2;j<=t/j;j++){
    
    
        while(t%j==0){
    
    
          h[j]++;
          t/=j;
        }
      } 
      //注意可能没有除尽
      if(t>1)h[t]++;
  }
  long long ans=1;
  for(int i=2;i<99;i++){
    
    
     if(h[i]>0)ans*=(h[i]+1);
  }
  
  printf("%lld",ans);
  return 0;
}

3. Include 2 days

insert image description here

#include <iostream>
using namespace std;
int month[13]={
    
    0,31,28,31,30,31,30,31,31,30,31,30,31};
bool isdate(int date){
    
    
  int yyyy=date/10000;
  int mm=date/100%100;
  int dd=date%100;
  if(mm>12||mm<=0)return false;
  if(yyyy%400==0||(yyyy%4==0&&yyyy%100!=0)){
    
    
    month[2]=29;
  }else{
    
    
    month[2]=28;
  }
  if(dd<=month[mm]&&dd>=1)return true;
  return false;
}
bool check(int date){
    
    
  while(date){
    
    
    if(date%10==2)return true;
    date/=10;
  }
  return false;
}
int main()
{
    
    
  int ans=0;
  for(int i=19000101;i<=99991231;i++){
    
    
    if(isdate(i)){
    
    
      if(check(i)){
    
    
        ans++;
      }
    }
  }
  printf("%d",ans);
  return 0;
}

4.k times interval

insert image description here

Method 1: Prefix and
time complexity O(n 2 )
score 28 The rest of the samples time out

#include <iostream>
using namespace std;
const int N=1e5+100;
int num[N];
int s[N];
int main(){
    
    
    int n,k;
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++){
    
    
        scanf("%d",&num[i]);
        s[i]=s[i-1]+num[i];
    }
    long long ans=0;
    for(int i=1;i<=n;i++){
    
    
        for(int j=i;j<=n;j++){
    
    
            if((s[j]-s[i-1])%k==0){
    
    
                ans++;
            }
        }
    }
    printf("%lld",ans);
    return 0;
}

Method 2: Optimize
s[j]-s[i-1])%k==0 , that is, the remainder of s[j] and s[i-1] are the same.
Use cnt[N] to count the number of numbers with the same remainder, That is, the number of numbers with the same remainder in the interval (l,r]

Another point to note is that if a certain segment number A1...Ai does not need to be subtracted from the interval, it itself is a K-fold interval, but cnt[0] stores the number of prefix sums with a remainder of 0 before i. This interval It is not counted in itself, so in the initial state, cnt[0] should be assigned a value of 1.
Time complexity: O(n3 )-> prefix sum O(n2 )-> storage remainder O(n)

#include <iostream>
using namespace std;
const int N=1e5+100;
long long s[N];
long long cnt[N];
int main(){
    
    
    int n,k;
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++){
    
    
        scanf("%d",&s[i]);
        s[i]+=s[i-1];
    }
    long long ans=0;
    cnt[0]=1;
    for(int i=1;i<=n;i++){
    
    
         ans+=cnt[s[i]%k];
         cnt[s[i]%k]++;
    }
    printf("%lld",ans);
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324144005&siteId=291194637