Codejam Qualification Round 2016 - A,B,C题解

来源:互联网 发布:贴吧查发帖记录软件 编辑:程序博客网 时间:2024/06/16 00:36

A - Counting Sheep

题意:

给一个数N,问这个数的多少倍后(假设为i),1~iN中0~9就都至少出现一次了。输出iN。

题解:

除了0不可能外,其他的都可以有,暴力的做就好

Code:

#include <iostream>#include <cstdio>using namespace std;int had[10];int main(){int T,cases,x,y,m;freopen("A-large.in.txt","r",stdin);freopen("A-large.out","w",stdout);scanf("%d",&T);for (int cases=1;cases<=T;cases++){printf("Case #%d: ",cases);scanf("%d",&x);if (!x) puts("INSOMNIA");    else{    m=y=0;    memset(had,false,sizeof(had));    while (m!=10){    y+=x;    int t=y;    while (t){    if (!had[t%10])    had[t%10]=true,m++;    t/=10;    }    }    printf("%d\n",y);    }}return 0;}

B - Revenge of the Pancakes

题意:

有一个栈,里面的元素有-,+两个状态,可以做的操作是从栈顶取出若干个元素,将它们顺序反过来,然后状态反过来放回去,问给一个栈的状态,至少多少次操作可以让栈里的元素都为+。

题解:

观察后,得知操作是将栈中最上方连续的一块-变为+

Code:

#include <iostream>#include <cstdio>using namespace std;char s[105];int main(){int T,cases,n,ans;freopen("B-large.in.txt","r",stdin);freopen("B-large.out","w",stdout);scanf("%d",&T);for (int cases=1;cases<=T;cases++){printf("Case #%d: ",cases);  scanf("%s",s+1),n=strlen(s+1);ans=0;if (s[1]=='-') ans=1;for (int i=2;i<=n;i++){if (s[i]==s[i-1]) continue;if (s[i]=='+') continue;ans+=2;}printf("%d\n",ans);}return 0;}

C - Coin Jam

题意:

我们称一个数为jamcoin当其

1、只包含0,1

2、以1开头,以1结尾

3、将其看作2~10进制,所得到的数都不是素数

输入N,J,则输出J个N位的符合要求的jamcoin,并且为了证明输出的数是jamcoin,输出其为成2~10进制时的非1与本身的一个约数

题解:

Miller-Rabin + Pollard_rho  参考这个代码

因为大数据比较大,改了一个python版本,且为了快速求解,可以多开几个进程同时枚举,我的机器开了4个进程,大数据大概就一刻分钟出来

import random def exp_mod(x,k,m):    if (k==0):        return 1    ans = exp_mod(x,k/2,m)    ans = (ans * ans)%m    if (k%2 == 1):        return (ans * x)%m    else:        return ansdef Rabin_Miller(p):     for times in range(1,10):        a = random.randint(1,p-1)         if (exp_mod(a,p-1,p) != 1):            return False    return Truedef gcd(a,b):    if (b == 0):        return a    else:        return gcd(b,a%b)def Pollard_rho(x,c):    i = 1    k = 2    x0 = random.randint(0,x-1);    y = x0    while (True):        i = i + 1        x0 = (x0*x0+c)%x        d = gcd(y-x0,x)        if (d != 1) and (d != x):            return d        if (y == x0):            return x        if (i == k):            y = x0            k = k + k def findfac(n):    if (Rabin_Miller(n)):         return n    p = n    while (p >= n):        p = Pollard_rho(p,random.randint(1,n-1))    return findfac(p) fo = open('output.txt','w') def judge(x):    t = x    s = ""     while (t > 0):        s = str(t%2) + s        t = t / 2    for p in range(2,11):        m = 0        k = 1        t = x        while (t > 0):            if ( t % 2 == 1):                m = m + k            k = k * p            t = t / 2        if (Rabin_Miller(m)):            return False        fac = findfac(m)        s = s + " " + str(fac)    fo.write(s + "\n")    return True     t = int(input())n = int(input())j = int(input())fo.write("Case #1:\n")    #x = (2**32)+(2**9)+(2**6)+(2**4)+(2**2)+(2**1)+(2**0)x = (2**31)+(2**30)-1while j > 0:    while judge(x) == False:        x = x - 2     j = j-1    x = x - 2fo.close()


0 0
原创粉丝点击