11.1 下午考试

幸运数字(number)
Time Limit:1000ms Memory Limit:64MB
题目描述
LYK 最近运气很差,例如在 NOIP 初赛中仅仅考了 90 分,刚刚卡进复赛,于是它决定使
用一些方法来增加自己的运气值。
它觉得,通过收集幸运数字可以快速的增加它的 RP 值。
它给幸运数字下了一个定义:如果一个数 x 能被 3 整除或被 5 整除或被 7 整除,则这个
数为幸运数字。
于是它想让你帮帮它在 L~R 中存在多少幸运数字。
输入格式(number.in)
第一行两个数 L,R。
输出格式(number.out)
一个数表示答案。
输入样例
10 15
输出样例
4
数据范围
对于 50%的数据 1<=L<=R<=10^5。
对于 60%的数据 1<=L<=R<=10^9。
对于 80%的数据 1<=L<=R<=10^18。
对于 90%的数据 1<=L<=R<=10^100。
对于另外 10%的数据 L=1, 1<=R<=10^100。
对于 100%的数据 L, R 没有前导 0。

/*
容斥原理
设num(n,i)表示1~n中是i的倍数
则求1~n中3或5或7的倍数为
num(n,3)+num(n,5)+num(n,7)-num(n,3*5)-num(n,3*7)-num(5*7)+num(n,3*5*7)
这样求出1~r的减去1~l-1的即可
只是高精度...... 
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#define maxn 210
using namespace std;
int a[maxn],b[maxn],c[maxn],ans[maxn];
int sa[maxn],sb[maxn];
char s[maxn],t[maxn];
int ha[2]={1,1};
void jian2(int a[maxn],int b[maxn])
{
    memset(sa,0,sizeof(sa));
    memset(sb,0,sizeof(sb));
    sa[0]=a[0];
    for(int i=1;i<=a[0];i++)
      sa[i]=a[a[0]-i+1];
    for(int i=1;i<=b[0];i++)
      sb[i]=b[b[0]-i+1];
    
    int l=1;
    while(l<=sa[0])
    {
        sa[l]-=sb[l];
        if(sa[l]<0)
        {
            sa[l]+=10;
            sa[l+1]--;
        }
        l++;
    }
    while(sa[0]>1&&sa[sa[0]]==0)sa[0]--;
    a[0]=sa[0];
    for(int i=1;i<=sa[0];i++)a[i]=sa[sa[0]-i+1];
}
void jian(int a[maxn],int b[maxn])
{
    memset(sb,0,sizeof(sb));
    for(int i=1;i<=b[0];i++)
      sb[i]=b[b[0]-i+1];
    int l=1;
    while(l<=a[0])
    {
        a[l]-=sb[l];
        if(a[l]<0)
        {
            a[l]+=10;
            a[l+1]--;
        }
        l++;
    }
    while(a[0]>1&&a[a[0]]==0)a[0]--;
}
void add(int a[maxn],int b[maxn])
{
    memset(sa,0,sizeof(sa));
    memset(sb,0,sizeof(sb));
    
    int l1=a[0],l2=b[0];
    
    for(int i=1;i<=l2;i++)sb[i]=b[l2-i+1];
    
    int l3=1;
    while(l3<=l1||l3<=l2)
    {
        sa[l3]+=a[l3]+sb[l3];
        sa[l3+1]=sa[l3]/10;
        sa[l3]%=10;
        l3++;
    }
    while(l3>1&&sa[l3]==0)l3--;sa[0]=l3;
    for(int i=0;i<=sa[0];i++)a[i]=sa[i];
}
void chu(int r[maxn],int a[maxn],int x)
{
    int s=0;r[0]=a[0];
    for(int i=1;i<=a[0];i++)
    {
        s=s*10+a[i];
        r[i]=s/x;
        s=s%x;
    }
}
int main()
{
    freopen("number.in","r",stdin);
    freopen("number.out","w",stdout);
    scanf("%s%s",s,t);
    int l1=strlen(s);
    int l2=strlen(t);
    
    a[0]=l1;for(int i=1;i<=l1;i++)a[i]=s[i-1]-'0';
    b[0]=l2;for(int i=1;i<=l2;i++)b[i]=t[i-1]-'0';
    jian2(a,ha);
    
    chu(c,b,3);add(ans,c);
    chu(c,b,5);add(ans,c);
    chu(c,b,7);add(ans,c);
    chu(c,b,3*5*7);add(ans,c);
    chu(c,a,3*5);add(ans,c);
    chu(c,a,5*7);add(ans,c);
    chu(c,a,3*7);add(ans,c);
    
    chu(c,b,3*5);jian(ans,c);
    chu(c,b,5*7);jian(ans,c);
    chu(c,b,3*7);jian(ans,c);
    chu(c,a,3);jian(ans,c);
    chu(c,a,5);jian(ans,c);
    chu(c,a,7);jian(ans,c);
    chu(c,a,3*5*7);jian(ans,c);
    
    for(int i=ans[0];i>=1;i--)
      printf("%d",ans[i]);
    printf("
");
    return 0;
}

位运算(bit)
Time Limit:2000ms Memory Limit:64MB
题目描述
lyk 最近在研究位运算。它发现除了 xor,or,and 外还有很多运算。
它新定义了一种运算符“#” 。
具体地,可以由 4 个参数来表示。 令 a[i][j]表示 i#j。 其中 i,j 与 a 的值均∈[0,1]。
当然问题可以扩展为>1 的情况,具体地,可以将两个数分解为 p 位,然后对于每一位
执行上述的位运算,再将这个二进制串转化为十进制就可以了。
例如当 a[0][0]=0, a[1][1]=0, a[0][1]=1, a[1][0]=1 时,3#4 在 p=3 时等于 7,2#3 在
p=4 时等于 1(实际上就是异或运算)。
现在 lyk 想知道的是,已知一个长为 n 的数列 b。它任意选取一个序列 c,满
足 c1<c2<...<ck,其中 1≤c1 且 ck≤n,定义这个序列的价值为 b{c1}#b{c2}#...#b{ck}
的平方。
这里我们假设 k 是正整数,因此满足条件的 c 的序列个数一定是 2^n−1 。 lyk 想知道
所有满足条件的序列的价值总和是多少。
由于答案可能很大, 你只需输出答案对 1,000,000,007 取模后的结果即可。
输入格式(bit.in)
第一行两个数 n,p。
第二行 4 个数表示 a[0][0], a[0][1], a[1][0], a[1][1]。
第三行 n 个数表示 bi(0<=bi<2^p)。
输出格式(bit.out)
一个数表示答案。
输入样例
3 30
0 1 1 0
1 2 3
输出样例
28
样例解释
{1}的价值为 1, {2}的价值为 4, {3}的价值为 9, {1,2}的价值为 9, {1,3}的价值为 4, {2,3}
的价值为 1, {1,2,3}的价值为 0,因此 7 个子集的价值总和为 28。
数据范围
总共 10 组数据。
对于第 1,2 组数据 n<=5。
对于第 3,4 组数据 n<=10000, p=1。
对于第 5 组数据 a 值均为 0。
对于第 6 组数据 a 值均为 1。
对于第 7 组数据 a[0][0]=0,a[1][0]=0,a[1][1]=1,a[0][1]=1。
对于第 8,9 组数据 n<=1000, p<=10。
对于所有数据 n<=10000, 1<=p<=30。

/*
暴力 + 特判(30分 应该可以判到40分有一种情况没判)
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#define LL long long
#define maxn 10010
#define mod 1000000007
using namespace std;
LL n,p,tot,ans;
LL a[2][2];
LL b[maxn][50];
LL c[maxn],s[50];
LL init()
{
    LL x=0,f=1;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x*f;
}

void fen(LL a[50],LL x)
{
    LL l=0;
    while(x)
    {
        a[++l]=x%2;
        x=x/2;
    }
}

LL he(LL a[50])
{
    LL ret=0;
    for(LL i=1,j=1;i<=p;i++,j*=2)
      if(a[i])ret+=j,ret%=mod;
    return ret*ret%mod;
}

void dfs(LL x)
{
    if(x==n+1)
    {
        if(tot==0)return;
        LL now=c[1];
        for(LL i=1;i<=p;i++)s[i]=b[now][i];
        for(LL i=2;i<=tot;i++)
        {
            LL x=c[i];
            for(LL i=1;i<=p;i++)
            s[i]=a[s[i]][b[x][i]];
        }
        ans+=he(s);ans%=mod;
        return;
    }
    dfs(x+1);
    tot++;c[tot]=x;
    dfs(x+1);tot--;
}
int main()
{
    freopen("bit.in","r",stdin);
    freopen("bit.out","w",stdout);
    n=init();p=init();
    a[0][0]=init();a[0][1]=init();a[1][0]=init();a[1][1]=init();
    if(a[0][0]==0&&a[0][1]==0&&a[1][0]==0&&a[1][1]==0)
    {
        LL sum=0;
        for(LL i=1;i<=n;i++)
        {
            LL x;x=init();
            sum+=x*x%mod;sum%=mod;
        }
        cout<<sum<<endl;
        return 0;
    }
    for(LL i=1;i<=n;i++)
    {
        LL x;x=init();
        fen(b[i],x);
    }
    dfs(1);
    cout<<ans<<endl;
    return 0;
}
View Code

暂无正解

蚂蚁运输(ant)
Time Limit:5000ms Memory Limit:64MB
题目描述
LYK 在观察一些蚂蚁。
蚂蚁想要积攒一些货物来过冬。积攒货物的方法是这样的。
对于第 i 只蚂蚁,它要从 li出发,拿起货物,走到 ri处放下货物,需要消耗的时间为|ri-li|。
而且所有蚂蚁都是可以同时进行的,也就是说,假如有 m 只蚂蚁,那么运输完货物的时间
为 max{|ri-li|}。
LYK 决定帮蚂蚁一把,它发明了空间传输装置。具体地,当蚂蚁走到 X 处时,它可以不
耗费任意时间的情况下瞬间到达 Y,或者从 Y 到达 X。也就是说,一个蚂蚁如果使用了空间
传输装置,它耗费的时间将会是 min{|li-X|+|ri-Y|,|li-Y|+|ri-X|},当然蚂蚁也可以选择徒步走
到目标点。
由于空间传输装置非常昂贵, LYK 打算只建造这么一台机器。并且 LYK 想让蚂蚁运输完
货物的时间尽可能短,你能帮帮它吗?
输入格式(ant.in)
第一行两个数 n,m, n 表示 li,ri 的最大值。
接下来 m 行,每行两个数 li,ri。
输出格式(ant.out)
一个数表示答案
输入样例
5 2
1 3
2 4
输出样例
1
数据范围
对于 20%的数据 n,m<=100。
对于 40%的数据 n,m<=1000。
对于 60%的数据 n<=100000, m<=1000。
对于 80%的数据 n,m<=100000。
对于 100%的数据 n,m<=1000000, 1<=li,ri<=n( li=ri 时你甚至可以无视这只蚂蚁)。
样例解释
令空间传输装置的参数中 X=2, Y=3 或者 X=3, Y=2 都行。

/*
O(n^2m)暴力 + 卡时
40分 
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#define LL long long
#define inf 10000000
#define maxn 1010
using namespace std;
LL n,m,ans=inf;
LL a[maxn],b[maxn];
LL init()
{
    LL x=0,f=1;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x*f;
}
LL abs(LL x)
{
    return x>=0?x:-x;
}
int main()
{
    freopen("ant.in","r",stdin);
    freopen("ant.out","w",stdout);
    n=init();m=init();
    for(LL i=1;i<=m;i++)
    {
        a[i]=init();
        b[i]=init();
    }
    for(LL i=1;i<=n;i++)
    {
        if(clock()>4500)break; 
        for(LL j=1;j<=n;j++)
        {
            LL tot=0;
            for(LL k=1;k<=m;k++)
            {
                LL x=abs(a[k]-i)+abs(b[k]-j),y=abs(a[k]-j)+abs(b[k]-i),z=abs(a[k]-b[k]);
                tot=max(tot,min(min(x,y),z));
            }
            ans=min(ans,tot);
        }
    }
    cout<<ans<<endl;
    return 0;
}
View Code
/*
二分答案 
读入时保证li<=ri 
chaeck(mid)时 
设建的传送站为X_Y 
abs(l-X)+abs(r-Y)<=mid 
则要求
l-X+r-Y<=mid
l-X+Y-r<=mid
X-l+r-Y<=mid
X-l+Y-r<=mid
则
l+r-mid<=X+Y<=l+r+mid
l-r-mid<=X-Y<=l-r+mid
只要存在X,Y对于所有的l,r满足上面两条件即可 
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#define maxn 1000010
#define inf 1000000000
using namespace std;
int n,m,l,r,ans;
int x[maxn],y[maxn];
int init()
{
    int x=0,f=1;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x*f;
}
int check(int t)
{
    int L=-inf,R=inf;
    for(int i=1;i<=m;i++)
    {
        if(y[i]-x[i]<=t)continue;
        L=max(L,x[i]+y[i]-t);
        R=min(R,x[i]+y[i]+t);
    }
    if(L>R)return 0;
    L=-inf,R=inf;
    for(int i=1;i<=m;i++)
    {
        if(y[i]-x[i]<=t)continue;
        L=max(L,x[i]-y[i]-t);
        R=min(R,x[i]-y[i]+t);
    }
    if(L>R)return 0;
    return 1;
}
int main()
{
    freopen("ant.in","r",stdin);
    freopen("ant.out","w",stdout);
    n=init();m=init();r=n;
    for(int i=1;i<=m;i++)
    {
        x[i]=init();y[i]=init();
        if(x[i]>y[i])swap(x[i],y[i]);
    }
    while(l<=r)
    {
        int mid=(l+r)/2;
        if(check(mid))
        {
            r=mid-1;
            ans=mid;
        }
        else 
          l=mid+1;
    }
    cout<<ans<<endl;
    return 0;
}
原文地址:https://www.cnblogs.com/dingmenghao/p/6035715.html