zoukankan      html  css  js  c++  java
  • 2017百度之星初赛

    1001 小C的倍数问题

    Accepts: 1990
    Submissions: 4931
    Time Limit: 2000/1000 MS (Java/Others)
    Memory Limit: 32768/32768 K (Java/Others)
    Problem Description

    根据小学数学的知识,我们知道一个正整数x是3的倍数的条件是x每一位加起来的和是3的倍数。反之,如果一个数每一位加起来是3的倍数,则这个数肯定是3的倍数。

    现在给定进制P,求有多少个B满足P进制下,一个正整数是B的倍数的充分必要条件是每一位加起来的和是B的倍数。

    Input

    第一行一个正整数T表示数据组数(1<=T<=20)。

    接下来T行,每行一个正整数P(2 < P < 1e9),表示一组询问。

    Output

    对于每组数据输出一行,每一行一个数表示答案。

    Sample Input
    1
    10
    
    Sample Output
    3
    P-1的因子个数
    #include <iostream> 
    #include <algorithm> 
    #include <cstring> 
    #include <cstdio>
    #include <vector> 
    #include <queue> 
    #include <cstdlib> 
    #include <iomanip>
    #include <cmath>
    #include <cassert>
    #include <ctime> 
    #include <map> 
    #include <set> 
    using namespace std; 
    #define lowbit(x) (x&(-x)) 
    #define max(x,y) (x>y?x:y) 
    #define min(x,y) (x<y?x:y) 
    #define MAX 100000000000000000 
    #define MOD 1000000007
    #define pi acos(-1.0) 
    #define ei exp(1) 
    #define PI 3.141592653589793238462
    #define ios() ios::sync_with_stdio(false)
    #define INF 1044266558
    #define mem(a) (memset(a,0,sizeof(a)))
    typedef long long ll;
    ll kk(ll n)
    {
        int ans=0;
        int o=sqrt(n);
        for(int i=1;i<=o;i++)
        {
            if(n%i==0) ans+=2;
        }
        if(o*o==n) ans--;
        return ans;
    }
    int main()
    {
        ll t,k;
        scanf("%lld",&t);
        while(t--)
        {
            scanf("%lld",&k);
            printf("%lld
    ",kk(k-1));
        }
        return 0;
    }

    1002 数据分割

    Accepts: 102
    Submissions: 1332
    Time Limit: 2000/1000 MS (Java/Others)
    Memory Limit: 32768/32768 K (Java/Others)
    Problem Description

    小w来到百度之星的赛场上,准备开始实现一个程序自动分析系统。

    这个程序接受一些形如xi=xjx_i = x_jxi​​=xj​​ 或 xi≠xjx_i eq x_jxi​​xj​​ 的相等/不等约束条件作为输入,判定是否可以通过给每个 w 赋适当的值,来满足这些条件。

    输入包含多组数据。 然而粗心的小w不幸地把每组数据之间的分隔符删掉了。 他只知道每组数据都是不可满足的,且若把每组数据的最后一个约束条件去掉,则该组数据是可满足的。

    请帮助他恢复这些分隔符。

    Input

    111行:一个数字LLL,表示后面输入的总行数。

    之后LLL行,每行包含三个整数,i,j,ei,j,ei,j,e,描述一个相等/不等的约束条件,若e=1e=1e=1,则该约束条件为xi=xjx_i = x_jxi​​=xj​​ ,若e=0e=0e=0,则该约束条件为 xi≠xjx_i eq x_jxi​​xj​​ 。

    i,j,L≤100000 i,j,L leq 100000i,j,L100000

    xi,xj≤Lx_i , x_j leq L xi​​,xj​​L

    Output

    输出共T+1T+1T+1行。

    第一行一个整数TTT,表示数据组数。

    接下来TTT行的第iii行,一个整数,表示第i组数据中的约束条件个数。

    Sample Input
    6
    2 2 1
    2 2 1
    1 1 1
    3 1 1
    1 3 1
    1 3 0
    
    Sample Output
    1
    6
    并查集,按条件加入集合中。满足条件时,加入集合,不满足时说明是该组最后一组数据,清空即可。
    #include <iostream> 
    #include <algorithm> 
    #include <cstring> 
    #include <cstdio>
    #include <vector> 
    #include <queue> 
    #include <cstdlib> 
    #include <iomanip>
    #include <cmath>
    #include <cassert>
    #include <ctime> 
    #include <map> 
    #include <set> 
    using namespace std; 
    #define lowbit(x) (x&(-x)) 
    #define max(x,y) (x>y?x:y) 
    #define min(x,y) (x<y?x:y) 
    #define MAX 100000000000000000 
    #define MOD 1000000007
    #define pi acos(-1.0) 
    #define ei exp(1) 
    #define PI 3.141592653589793238462
    #define ios() ios::sync_with_stdio(false)
    #define INF 1044266558
    #define mem(a) (memset(a,0,sizeof(a)))
    typedef long long ll;
    set<int>s[100005],v;
    set<int>::iterator it;
    int ans,f[100005],pos[100005];
    int L,x,y,fx,fy,cnt,w;
    int find(int x)
    {
        return f[x]==x?x:find(f[x]);
    }
    void uion(int x,int y)
    {
        if(x==y) return ;
        if(s[x].size()>s[y].size()) swap(x,y);
        for(it=s[x].begin();it!=s[x].end();it++)//因为有这种情况:x和y相等,x和z不相等,那么y和z肯定也不相等
            //也就是说只要z和x不相等,那么和x在同一个并查集合里的所有数都和z不相等
        {
            s[*it].erase(x);
            s[*it].insert(y);
            s[y].insert(*it);
        }
        f[x]=y;
    }
    int main()
    {
        for(int i=0;i<=100005;i++) f[i]=i;
        scanf("%d",&L);
        cnt=0;ans=0;//Recoding the number of date per case//Recoding the numper of case;
        while(L--)
        {
            cnt++;
            scanf("%d%d%d",&x,&y,&w);
            v.insert(x);v.insert(y);
            fx=find(x);fy=find(y);
            if(w==1)
            {
                if(fx==fy || s[fx].count(fy)==0) uion(fx,fy);//can satisfy
                else
                {//not satisfy,delete;
                    pos[++ans]=cnt;
                    cnt=0;//Recoding the number of date per case;
                    for(it=v.begin();it!=v.end();it++)//for the new Case,so clear completely
                    {
                        s[*it].clear();
                        f[*it]=*it;
                    }
                    v.clear();
                }
            }
            else
            {
                if(fx==fy)//not satisfy,delete
                {
                    pos[++ans]=cnt;
                    cnt=0;
                    for(it=v.begin();it!=v.end();it++)//for the new Case,so clear completely
                    {
                        s[*it].clear();
                        f[*it]=*it;
                    }
                    v.clear();
                }
                else s[fx].insert(f[y]),s[fy].insert(fx);//can satisfy
            }
        }
        printf("%d
    ",ans);
        for(int i=1;i<=ans;i++)
            printf("%d
    ",pos[i]);
        return 0;
    }

    1005 今夕何夕

    Accepts: 1345
    Submissions: 5533
    Time Limit: 2000/1000 MS (Java/Others)
    Memory Limit: 32768/32768 K (Java/Others)
    Problem Description

    今天是2017年8月6日,农历闰六月十五。

    小度独自凭栏,望着一轮圆月,发出了“今夕何夕,见此良人”的寂寞感慨。

    为了排遣郁结,它决定思考一个数学问题:接下来最近的哪一年里的同一个日子,和今天的星期数一样?比如今天是8月6日,星期日。下一个也是星期日的8月6日发生在2023年。

    小贴士:在公历中,能被4整除但不能被100整除,或能被400整除的年份即为闰年。

    Input

    第一行为T,表示输入数据组数。

    每组数据包含一个日期,格式为YYYY-MM-DD。

    1 ≤ T ≤ 10000

    YYYY ≥ 2017

    日期一定是个合法的日期

    Output

    对每组数据输出答案年份,题目保证答案不会超过四位数。

    Sample Input
    3
    2017-08-06
    2017-08-07
    2018-01-01
    
    Sample Output
    2023
    2023
    2024
    分情况讨论月份小于2、月份大于2、月份等于2,天数等于29.
    #include <iostream> 
    #include <algorithm> 
    #include <cstring> 
    #include <cstdio>
    #include <vector> 
    #include <queue> 
    #include <cstdlib> 
    #include <iomanip>
    #include <cmath>
    #include <cassert>
    #include <ctime> 
    #include <map> 
    #include <set> 
    using namespace std; 
    #define lowbit(x) (x&(-x)) 
    #define max(x,y) (x>y?x:y) 
    #define min(x,y) (x<y?x:y) 
    #define MAX 100000000000000000 
    #define MOD 1000000007
    #define pi acos(-1.0) 
    #define ei exp(1) 
    #define PI 3.141592653589793238462
    #define ios() ios::sync_with_stdio(false)
    #define INF 1044266558
    #define mem(a) (memset(a,0,sizeof(a)))
    typedef long long ll;
    int t,n,y,d;
    char ch;
    int leap(int x)
    {
        if(x%400==0) return 1;
        if(x%4==0 && x%100) return 1;
        return 0;
    }
    int main()
    {
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d%c%d%c%d",&n,&ch,&y,&ch,&d);
            int pos=0;
            if(y==2 && d==29)
            {
                for(int i=1;;i++)
                {
                    if(leap(n+i)) pos+=2;
                    else pos+=1;
                    if(pos%7==0 && leap(n+i))
                    {
                        printf("%d
    ",n+i);
                        break;
                    }
                }
            }
            else if(y<=2)
            {
                for(int i=0;;i++)
                {
                    if(leap(n+i)) pos+=2;
                    else pos+=1;
                    if(pos%7==0)
                    {
                        printf("%d
    ",n+i+1);
                        break;
                    }
                }
            }
            else
            {
                for(int i=1;;i++)
                {
                    if(leap(n+i)) pos+=2;
                    else pos+=1;
                    if(pos%7==0)
                    {
                        printf("%d
    ",n+i);
                        break;
                    }
                }
            }
        }
        return 0;
    }

    1006 度度熊的01世界

    Accepts: 967
    Submissions: 3064
    Time Limit: 2000/1000 MS (Java/Others)
    Memory Limit: 32768/32768 K (Java/Others)
    Problem Description

    度度熊是一个喜欢计算机的孩子,在计算机的世界中,所有事物实际上都只由0和1组成。

    现在给你一个n*m的图像,你需要分辨他究竟是0,还是1,或者两者均不是。

    图像0的定义:存在1字符且1字符只能是由一个连通块组成,存在且仅存在一个由0字符组成的连通块完全被1所包围。

    图像1的定义:存在1字符且1字符只能是由一个连通块组成,不存在任何0字符组成的连通块被1所完全包围。

    连通的含义是,只要连续两个方块有公共边,就看做是连通。

    完全包围的意思是,该连通块不与边界相接触。

    Input

    本题包含若干组测试数据。 每组测试数据包含: 第一行两个整数n,m表示图像的长与宽。 接下来n行m列将会是只有01组成的字符画。

    满足1<=n,m<=100

    Output

    如果这个图是1的话,输出1;如果是0的话,输出0,都不是输出-1。

    Sample Input
    32 32
    00000000000000000000000000000000
    00000000000111111110000000000000
    00000000001111111111100000000000
    00000000001111111111110000000000
    00000000011111111111111000000000
    00000000011111100011111000000000
    00000000111110000001111000000000
    00000000111110000001111100000000
    00000000111110000000111110000000
    00000001111110000000111110000000
    00000001111110000000011111000000
    00000001111110000000001111000000
    00000001111110000000001111100000
    00000001111100000000001111000000
    00000001111000000000001111000000
    00000001111000000000001111000000
    00000001111000000000000111000000
    00000000111100000000000111000000
    00000000111100000000000111000000
    00000000111100000000000111000000
    00000001111000000000011110000000
    00000001111000000000011110000000
    00000000111000000000011110000000
    00000000111110000011111110000000
    00000000111110001111111100000000
    00000000111111111111111000000000
    00000000011111111111111000000000
    00000000111111111111100000000000
    00000000011111111111000000000000
    00000000001111111000000000000000
    00000000001111100000000000000000
    00000000000000000000000000000000
    32 32
    00000000000000000000000000000000
    00000000000000001111110000000000
    00000000000000001111111000000000
    00000000000000011111111000000000
    00000000000000111111111000000000
    00000000000000011111111000000000
    00000000000000011111111000000000
    00000000000000111111110000000000
    00000000000000111111100000000000
    00000000000001111111100000000000
    00000000000001111111110000000000
    00000000000001111111110000000000
    00000000000001111111100000000000
    00000000000011111110000000000000
    00000000011111111110000000000000
    00000001111111111111000000000000
    00000011111111111111000000000000
    00000011111111111111000000000000
    00000011111111111110000000000000
    00000000001111111111000000000000
    00000000000000111111000000000000
    00000000000001111111000000000000
    00000000000111111110000000000000
    00000000000011111111000000000000
    00000000000011111111000000000000
    00000000000011111111100000000000
    00000000000011111111100000000000
    00000000000000111111110000000000
    00000000000000001111111111000000
    00000000000000001111111111000000
    00000000000000000111111111000000
    00000000000000000000000000000000
    3 3
    101
    101
    011
    
    Sample Output
    0
    1
    -1
    BFS求连通块个数,注意0 1的定义
    #include <iostream> 
    #include <algorithm> 
    #include <cstring> 
    #include <cstdio>
    #include <vector> 
    #include <queue> 
    #include <cstdlib> 
    #include <iomanip>
    #include <cmath>
    #include <cassert>
    #include <ctime> 
    #include <map> 
    #include <set> 
    using namespace std; 
    #define lowbit(x) (x&(-x)) 
    #define max(x,y) (x>y?x:y) 
    #define min(x,y) (x<y?x:y) 
    #define MAX 100000000000000000 
    #define MOD 1000000007
    #define pi acos(-1.0) 
    #define ei exp(1) 
    #define PI 3.141592653589793238462
    #define ios() ios::sync_with_stdio(false)
    #define INF 1044266558
    #define mem(a) (memset(a,0,sizeof(a)))
    typedef long long ll;
    char a[101][101];
    int vis[101][101],n,m;
    char ch;
    int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
    struct node
    {
        int x,y;
    }pos,q;
    queue<node>ans;
    int bfs(int x,int y,char c)
    {
        int xx,yy;
        pos.x=x;pos.y=y;
        vis[x][y]=1;
        ans.push(pos);
        int ok=1;
        while(!ans.empty())
        {
            pos=ans.front();
            ans.pop();
            for(int i=0;i<4;i++)
            {
                xx=dir[i][0]+pos.x;
                yy=dir[i][1]+pos.y;
                if((xx>=0 && xx<n) && (yy>=0 && yy<m))
                {
                    if(a[xx][yy]==c && !vis[xx][yy])
                    {
                        q.x=xx;
                        q.y=yy;
                        vis[xx][yy]=1;
                        ans.push(q);
                    }
    
                }
                else ok=0;
            }
        }
        return ok;
    }
    int main()
    {
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            for(int i=0;i<n;i++)
            {
                scanf("%s",a[i]);
            }
            int pos=0,ans=0,cnt=0;
            int la=0,lb=0;
            memset(vis,0,sizeof(vis));
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<m;j++)
                {
                    if(!vis[i][j])
                    {
                        if(a[i][j]=='0')
                        {
                            la++;
                            pos+=bfs(i,j,'0');
                        }
                        else
                        {
                            lb++;
                            ans+=bfs(i,j,'1');
                        }
                    }
                }
            }
            if(lb==1 && pos==0) printf("1
    ");
            else if(pos==1 && lb==1) printf("0
    ");
            else printf("-1
    ");
        }
        return 0;
    }
    /*
    16 32
    00000000111110000001111100000000
    00000000111110000000111110000000
    00000001111110000000111110000000
    00000001111110000000011111000000
    00000001111110000000001111000000
    00000001111110000000001111100000
    00000001111100000000001111000000
    00000001111000000000001111000000
    00000001111000000000001111000000
    00000001111000000000000111000000
    00000000111100000000000111000000
    00000000111100000000000111000000
    00000000111100000000000111000000
    00000001111000000000011110000000
    00000001111000000000011110000000
    00000000111000000000011110000000
    -1
    */
    /*
    3 4
    1111
    1101
    1011
    0
    */


  • 相关阅读:
    rocketMQ配置事故
    微信网页授权问题记录
    记一次Spring配置事故
    文件转换
    Java对象空间分配流程
    mysql(六)索引的数据结构
    mysql(五)查询缓存
    mysql(四)log
    从项目中加载文件
    cookie
  • 原文地址:https://www.cnblogs.com/shinianhuanniyijuhaojiubujian/p/7350851.html
Copyright © 2011-2022 走看看