Codejam Qualification Round 2016 - A,B,C题解
2016-04-10 10:15
267 查看
A - Counting Sheep
题意:
给一个数N,问这个数的多少倍后(假设为i),1~iN中0~9就都至少出现一次了。输出iN。
题解:
除了0不可能外,其他的都可以有,暴力的做就好
Code:
B - Revenge of the Pancakes
题意:
有一个栈,里面的元素有-,+两个状态,可以做的操作是从栈顶取出若干个元素,将它们顺序反过来,然后状态反过来放回去,问给一个栈的状态,至少多少次操作可以让栈里的元素都为+。
题解:
观察后,得知操作是将栈中最上方连续的一块-变为+
Code:
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个进程,大数据大概就一刻分钟出来
题意:
给一个数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 ans def 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 True def 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)-1 while j > 0: while judge(x) == False: x = x - 2 j = j-1 x = x - 2 fo.close()
相关文章推荐
- checkbox已设置为checked--true-但不勾选问题解决方法(只第一次勾选有效)
- IP、域名、手机号归属地查询
- lambda表达式的实质
- 票据扫描识别现状分析
- 乐观锁vs悲观锁
- node.js学习之路
- POJ 3258 小牛过河 二分搜索最大的最小值
- Fragment入门
- MySQL常用引擎的锁机制
- Intent传递对象的几种方式
- 计算s=a+aa+aaa+aaaa+aa...a形式的表达式的和,其中a是一个数字
- boost学习与应用笔记之一:Boost.Atomic
- Computer Vision(计算机视觉)方面的期刊会议
- ASP.NET MVC Bundles 用法和说明(打包javascript和css)
- 关于 This tag and its children can be replaced by one <TextView/> and a compound drawable 的问题
- ANDROID中vnd.android.cursor的解释
- go语言注释
- PIXHAWK开发环境搭建(基于Ubuntu14.04系统)
- 64位 ubuntu 运行32位命令 (arm-none-linux-gnueabi-gcc)
- LeetCode 125. Valid Palindrome