码迷,mamicode.com
首页 > 其他好文 > 详细

此坑待补

时间:2017-10-20 10:21:05      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:sizeof   sum   ==   dimen   output   cto   rom   determine   minimum   

 

4383: n % ( pow( p , 2) ) ===0 技术分享

Time Limit(Common/Java):10000MS/30000MS     Memory Limit:65536KByte
Total Submit: 238            Accepted:54

Description

 

There is a number n , determine whether there is a p (p>1) that p^2 is a divisor of n.

 

Input

 

The first line contains an integer T , the number of test case.

The following T lines , each contains an integer n.

( 1<= T <=10^2 , 1<= n <=10^18 )

 

Output

 

A integer p , if there exist multiple answer ,output the minimum one.

Or print “oh,no.” .

 

Sample Input

 

3
8
16
17

Sample Output

 

2
2
oh,no.

Source

数信学院第5届新生程序设计竞赛

我的代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<algorithm>
#include<cstring>
using namespace std;
typedef __int64 ll;
const int times=20;
const int N=100;
ll mult_mod(ll a,ll b,ll mod)
{
    a%=mod;
    b%=mod;
    ll res=0;
    while(b)
    {
        if(b&1)
        {
            res+=a;
            res%=mod;
        }
        a<<=1;
        if(a>=mod) a%=mod;
        b>>=1;
    }
    return res;
}
ll pow_mod(ll x,ll n,ll mod)
{
    if(n==1) return x%mod;
    x%=mod;
    ll t=x;
    ll res=1;
    while(n)
    {
        if(n&1) res=mult_mod(res,t,mod);
        t=mult_mod(t,t,mod);
        n>>=1;
    }
    return res;
}
bool test(ll a,ll n,ll x,ll t)
{
    ll res=pow_mod(a,x,n);
    ll last=res;
    for(int i=1; i<=t; i++)
    {
        res=mult_mod(res,res,n);
        if(res==1&&last!=1&&last!=n-1)
            return true;
        last=res;
    }
    if(res!=1) return true;
    return false;
}

bool miller_rabin(ll n)
{
    if(n<2) return false;
    if(n==2) return true;
    if((n&1)==0) return false;
    ll x=n-1,t=0;
    while((x&1)==0)
    {
        x>>=1;
        t++;
    }
    for(int i=0; i<times; i++)
    {
        ll a=rand()%(n-1)+1;
        if(test(a,n,x,t))
            return false;
    }
    return true;
}
ll factor[N];
int tot;
ll gcd(ll a,ll b)
{
    if(a==0) return 1;
    if(a<0) return gcd(-a,b);
    while(b)
    {
        ll c=a%b;
        a=b;
        b=c;
    }
    return a;
}

ll pollard_rho(ll x,ll c)
{
    ll i=1,k=2;
    ll x0=rand()%x;
    ll y=x0;
    while(1)
    {
        i++;
        x0=(mult_mod(x0,x0,x)+c)%x;
        ll d=gcd(y-x0,x);
        if(d!=1&&d!=x) return d;
        if(y==x0) return x;
        if(i==k)
        {
            y=x0;
            k+=k;
        }
    }
}
void find_factor(ll n)
{
    if(miller_rabin(n))
    {
        factor[tot++]=n;
        return ;
    }
    ll p=n;
    while(p>=n)
        p=pollard_rho(p,rand()%(n-1)+1);
    find_factor(p);
    find_factor(n/p);
}
int main()
{
    srand(time(0));
    int t;
    scanf("%d",&t);
    ll n;
    while(t--)
    {
        scanf("%I64d",&n);
        if(miller_rabin(n)||n<=1)
        {
            printf("oh,no.\n");
            continue;
        }
        tot=0;
        find_factor(n);
        sort(factor,factor+tot);
        int f=1;
        for(int i=1; i<tot; i++)
            if(factor[i]==factor[i-1])
            {
                printf("%I64d\n",factor[i]);
                f=0;
                break;
            }
        if(f)printf("oh,no.\n");
    }
    return 0;
}

2017: N-Credible Mazes 技术分享

Time Limit(Common/Java):1000MS/10000MS     Memory Limit:65536KByte
Total Submit: 6            Accepted:0

Description

An n-tersection is defined as a location in n-dimensional space, n being a positive integer, having all non-negative integer coordinates. For example, the location (1,2,3) represents an n-tersection in three dimensional space. Two n-tersections are said to be adjacent if they have the same number of dimensions and their coordinates differ by exactly 1 in a single dimension only. For example, (1,2,3) is adjacent to (0,2,3) and (2,2,3) and (1,2,4), but not to (2,3,3) or (3,2,3) or (1,2). An n-teresting space is defined as a collection of paths between adjacent n-tersections.

Finally, an n-credible maze is defined as an n-teresting space combined with two specific n-tersections in that space, one of which is identified as the starting n-tersection and the other as the ending n-tersection.

Input

The input file will consist of the descriptions of one or more n-credible mazes. The first line of the description will specify n, the dimension of the n-teresting space. (For this problem, n will not exceed 10, and all coordinate values will be less than 10.) The next line will contain 2n non-negative integers, the first n of which describe the starting n-tersection, least dimension first, and the next n of which describe the ending n-tersection. Next will be a nonnegative number of lines containing 2n non-negative integers each, identifying paths between adjacent n-tersections in the n-teresting space. The list is terminated by a line containing only the value ?C1. Several such maze descriptions may be present in the file. The end of the input is signalled by space dimension of zero. No further data will follow this terminating zero.

Output

For each maze output it‘s position in the input; e.g. the first maze is "Maze #1", the second is "Maze #2", etc. If it is possible to travel through the n-credible maze‘s n-teresting space from the starting n-tersection to the ending n-tersection, also output "can be travelled" on the same line. If such travel is not possible, output "cannot be travelled" instead.

Sample Input

 

2
0 0 2 2
0 0 0 1
0 1 0 2
0 2 1 2
1 2 2 2
-1
3
1 1 1 1 2 3
1 1 2 1 1 3
1 1 3 1 2 3
1 1 1 1 1 0
1 1 0 1 0 0
1 0 0 0 0 0
-1
0

Sample Output

Maze #1 can be travelled
Maze #2 cannot be travelled

爆搜的代码

#include<stdio.h>
#include<algorithm>
using namespace std;
int a[1005][10],b[1005][10],c[1005][3];
int main()
{
    int n,T=1;
    while(~scanf("%d",&n),n)
    {
        int i,j,k;
        for(i=0;;i++)
        {
            scanf("%d",&a[i][0]);
            if(a[i][0]==-1)break;
            for(j=1; j<n; j++)
                scanf("%d",&a[i][j]);
            for(j=0; j<n; j++)
                scanf("%d",&b[i][j]);
        }
        for(k=0; k<i; k++)
            for(j=0; j<n; j++)
                c[k][j]=0;
        c[0][0]=1;
        for(i=0; i<k; i++)
        {
            int f=0;
            for(j=0; j<n; j++)
                if(a[i+1][j]!=a[i][j])
                {
                    f++;
                    if(abs(a[i+1][j]-a[i][j])!=1)break;
                }
            if(f<=1&&j==n&&c[i][0]==1)c[i+1][0]=1;
            f=0;
            for(j=0; j<n; j++)
                if(b[i][j]!=a[i][j])
                {
                    f++;
                    if(abs(b[i][j]-a[i][j])!=1)break;
                }
            if(f<=1&&j==n&&c[i][0]==1)c[i][1]=1;
            f=0;
            for(j=0; j<n; j++)
                if(b[i+1][j]!=b[i][j])
                {
                    f++;
                    if(abs(b[i+1][j]-b[i][j])!=1)break;
                }
            if(f<=1&&j==n&&c[i][1]==1)c[i+1][1]=1;
            f=0;
            for(j=0; j<n; j++)
                if(b[i+1][j]!=a[i+1][j])
                {
                    f++;
                    if(abs(b[i+1][j]-a[i+1][j])!=1)break;
                }
            if(f<=1&&j==n&&c[i+1][1]==1)c[i+1][0]=1;
        }
        if(c[k-1][1]>=1) printf("Maze #%d can be travelled\n",T++);
        else printf("Maze #%d cannot be travelled\n",T++);
    }
    return 0;
}

map+set维护

#include<stdio.h>
#include<vector>
#include<set>
#include<map>
using namespace std;
map<int,vector<int> >M;
set<int>S;
int la(int &num,int n)
{
    num=0;
    for(int i=1; i<=n; i++)
    {
        int x;
        scanf("%d",&x);
        if(x==-1)return 0;
        num=num*10+x;
    }
    return 1;
}
int dfs(int st,int ed)
{
    if(st==ed)return 1;
    int l=M[st].size();
    for(int i=0; i<l; i++)
    {
        int v=M[st][i];
        if(!S.count(v))
        {
            S.insert(v);
            if(dfs(v,ed))return 1;
        }
    }
    return 0;
}
int main()
{
    int t=1,n;
    while(~scanf("%d",&n))
    {
        if(n==0)
        {
            if(scanf("%d",&n)==EOF)
                break;
        }
        M.clear(),S.clear();
        int st,ed;
        la(st,n);
        la(ed,n);
        int s,e;
        while(la(s,n))
        {
            la(e,n);
            M[s].push_back(e);
            M[e].push_back(s);
        }
        if(dfs(st,ed))
            printf("Maze #%d can be travelled\n",t++);
        else
            printf("Maze #%d cannot be travelled\n",t++);
    }
    return 0;
}

网上的HDUac代码

#include<stdio.h>
#include<string.h>
int f,sum,e,s;
int a[10010],b[10010];
int mark[10010];
void dfs(int w)
{
    int i;
    if (w==e)
    {
        f=1;
        return ;
    }
    for (i=1; i<=sum; i++)
        if (mark[i]==0 && (a[i]==w || b[i]==w))
        {
            mark[i]=1;
            if (a[i]==w) dfs(b[i]);
            else dfs(a[i]);
        }
}


int main()
{
    int Case,i,j,n,x;
    Case=0;
    while (scanf("%d",&n)!=EOF)
    {
        if (n==0) break;

        Case++;
        s=e=0;
        for (i=1; i<=n; i++)
        {
            scanf("%d",&x);
            s=s*10+x;
        }
        for (i=1; i<=n; i++)
        {
            scanf("%d",&x);
            e=e*10+x;
        }

        sum=0;
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));

        while (scanf("%d",&x)!=EOF)
        {
            if (x==-1) break;

            sum++;
            a[sum]=x;
            for (i=1; i<n; i++)
            {
                scanf("%d",&x);
                a[sum]=a[sum]*10+x;
            }
            for (i=1; i<=n; i++)
            {
                scanf("%d",&x);
                b[sum]=b[sum]*10+x;
            }

        }
        f=0;
        memset(mark,0,sizeof(mark));
        dfs(s);
        printf("Maze #%d ",Case);
        if (f==0)
            printf("cannot be travelled\n");
        else
            printf("can be travelled\n");
    }
    return 0;
}

 

此坑待补

标签:sizeof   sum   ==   dimen   output   cto   rom   determine   minimum   

原文地址:http://www.cnblogs.com/BobHuang/p/7697718.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!