9.4训练日志

[日志9.4训练题]

Odds and Ends

Where do odds begin, and where do they end? Where does hope emerge, and will they ever break?

Given an integer sequence a1, a2, …, an of length n. Decide whether it is possible to divide it into an odd number of non-empty subsegments, the each of which has an odd length and begins and ends with odd numbers.

A subsegment is a contiguous slice of the whole sequence. For example, {3, 4, 5} and {1} are subsegments of sequence {1, 2, 3, 4, 5, 6}, while {1, 2, 4} and {7} are not.

输入n,后n个数,判断奇数子集个数是否为奇数个,奇数子集为长度奇数并且首尾奇数;
代码写的难了。

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int m,a[101],i,x,n,ans=0;
    cin>>m;
    for(i=1; i<=m; i++)
        cin>>a[i];
    for(i=1; i<=m; i++)
    {
        if(a[i]&1)
        {
            ans++;
            n=i;
            for(x=i+2; x<=m; x=x+2)
            {
                if(x>m)break;
                if(a[x]%2==1)n=x;
            }
            i=n;
        }
        else
        {
            cout<<"NO";
            return 0;
        }
    }

    if(ans&1)
        cout<<"YES";
    else cout<<"NO";
}

Tell Your World

Connect the countless points with lines, till we reach the faraway yonder.

There are n points on a coordinate plane, the i-th of which being (i, yi).

Determine whether it’s possible to draw two parallel and non-overlapping lines, such that every point in the set lies on exactly one of them, and each of them passes through at least one point in the set.

判断n个数是否在两条不重叠且平行的一次函数图像上,写法:分三类,第一类1,2共线看可不可以,第二类1,2不共线1,3共线看可不可以,第三类看1,2不共线2,3共线看可不可以。如果有一种可以即可输出YES。
代码(第二第三类大部分可复制第一类的)

#include<bits/stdc++.h>
using namespace std;
int main()
 {int m,i;long long int a[1001];double k,b[3]={0};bool n=0,flag=0,flag1=1,flag2=1,flag3=1;
 cin>>m;
 for(i=1;i<=m;i++)
  {cin>>a[i];
  }
 for(i=2;i<=m;i++)//12gongxian
     { if(i==2)
      {k=(a[2]-a[1]);b[1]=a[1]-k;
       }
    else 
       {
        if(i*k+b[1]!=a[i]&&n==0)
         {b[2]=a[i]-i*k;n=1;
            }
        else if(i*k+b[1]!=a[i]&&i*k+b[2]!=a[i]&&n==1)
         {flag1=0;break;
         }
       }
     }
  if(n==0)flag1=0;  
 for(i=3;i<=m;i++)//12bugongxian,13gong
  {if(i==3)
    {k=(double)(a[3]-a[1])/2;b[1]=a[1]-k;b[2]=a[2]-2*k;
    if(b[1]==b[2])
     {flag2=0;break;
     }
    }
    else 
       {
        if(i*k+b[1]!=a[i]&&i*k+b[2]!=a[i]&&n==1)
         {flag2=0;break;
         }
       }
     }
 for(i=3;i<=m;i++)//12bugongxian,23gong
  {if(i==3)
    {k=(a[3]-a[2]);b[1]=a[2]-2*k;b[2]=a[1]-k;
    if(b[1]==b[2])
     {flag3=0;break;
     }
    }
    else 
       {
        if(i*k+b[1]!=a[i]&&i*k+b[2]!=a[i]&&n==1)
         {flag3=0;break;
         }
       }
     }
  if(flag1==1||flag2==1||flag3==1)
   cout<<"YES";
  else cout<<"NO";   
  } 

From Y to Y

From beginning till end, this message has been waiting to be conveyed.

For a given unordered multiset of n lowercase English letters (“multi” means that a letter may appear more than once), we treat all letters as strings of length 1, and repeat the following operation n - 1 times:

Remove any two elements s and t from the set, and add their concatenation s + t to the set.
The cost of such operation is defined to be , where f(s, c) denotes the number of times character c appears in string s.

Given a non-negative integer k, construct any valid non-empty set of no more than 100 000 letters, such that the minimum accumulative cost of the whole process is exactly k. It can be shown that a solution always exists.

一个字符串操作它的长度减一次,每次合并2个子集,cost=在两个子集中都出现的元素的次数乘积和。给定一个cost输出字符串。
代码

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int m,i,n=0;
    char c='a';
    cin>>m;
    if(m==0)cout<<"a";
    while(m)
    {
        for(i=1;; i++)
        {
            if((i*i-i)/2==m)
            {
                cout<<(char)(c+n);
                return 0;
            }
            else if((i*i-i)/2>m)
            {
                i--;
                break;
            }
            else cout<<(char)(c+n);
        }
        n++;
        m=m-((i*i-i)/2);
    }
}

Bear and Prime Numbers

Recently, the bear started studying data structures and faced the following problem.

You are given a sequence of integers x1, x2, …, xn of length n and m queries, each of them is characterized by two integers li, ri. Let’s introduce f(p) to represent the number of such indexes k, that xk is divisible by p. The answer to the query li, ri is the sum: , where S(li, ri) is a set of prime numbers from segment [li, ri] (both borders are included in the segment).

Help the bear cope with the problem.
先输入一串数,接着输入几个区间,输出一串数中为区间中质数倍数的个数。
代码

#include<bits/stdc++.h>
using namespace std;
const int N=10000001;
int k[N]= {0},a,n,v[N]= {0},s[N]= {0};


void f()
{
    int i,x,ans;
    for(i=2; i<=N; i++)
    {
        if(v[i]==1)continue;
        else
        {
            for(x=i; x<N; x=x+i)
            {
                if(k[x]!=0)s[i]=s[i]+k[x];
                v[x]=1;
            }
        }
    }
    for(i=1; i<N; i++)
        s[i]=s[i]+s[i-1];
}

int main()
{
    int m,i;
    int x,y;
    scanf("%d", &n);
    for(i=1; i<=n; i++)
    {
        scanf("%d", &a);
        k[a]++;
    }
    f();
    scanf("%d", &m);
    for(i=1; i<=m; i++)
    {
        scanf("%d%d", &x,&y);
        if(y>=N)
            y=N-1;
        if(x>=N)
        {
            x=N;
        }
        printf("%d\n", s[y]-s[x-1]);
    }
    return 0;
}

Harmony Analysis

The semester is already ending, so Danil made an effort and decided to visit a lesson on harmony analysis to know how does the professor look like, at least. Danil was very bored on this lesson until the teacher gave the group a simple task: find 4 vectors in 4-dimensional space, such that every coordinate of every vector is 1 or  - 1 and any two vectors are orthogonal. Just as a reminder, two vectors in n-dimensional space are considered to be orthogonal if and only if their scalar product is equal to zero, that is:
.
Danil quickly managed to come up with the solution for this problem and the teacher noticed that the problem can be solved in a more general case for 2k vectors in 2k-dimensinoal space. When Danil came home, he quickly came up with the solution for this problem. Can you cope with it?
类似于分形。。输入一个数字n,输出pow(2,n)行,每一行有pow(2,n)个元素,元素为“+”或”*“。输出满足每一行与其他任意一行,每个相同位置上的元素如果相同,则为1,若不同为-1,每一行的和要为0,输出一种情况即可。
代码(看成递推)

#include<bits/stdc++.h>
using namespace std;int a[1025][1025];
int main()
{
    int m,i,n=1,x;
    a[1][1]=1;
    cin>>m;
    m=1<<m;
    for(i=1; i<=m; i++)
    {
        for(i=1; i<=n; i++)
            for(x=1; x<=n; x++)
            {
                a[i][x+n]=a[i][x];
                a[i+n][x]=a[i][x];
                a[i+n][x+n]=-a[i][x];
            }
        n=n<<1;
    }
    for(i=1; i<=m; i++)
    {
        for(x=1; x<=m; x++)
        {
            if(a[i][x]==1)cout<<"+";
            else cout<<"*";
        }
        cout<<endl;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值