A - Prime Substring
Given a string of digits, your task is to find the largest prime number which presents in that string.Our prime numbers are values between 2 to 100,000 only.
Input
Each line contains a string of digits (255 digits at most). The line contains only ‘0’ indicates the endwhich will not be processed. The input does not exceed 1,000 lines.
Output
Print out in each line the largest prime number found in each input string.
Sample Input
11245
91321150448
12264060
Sample Output
11
1321
2
这道水题不解释,我是先用线筛弄出素数,再枚举从每位开始的1-5位数,然后就过了。。过了,学长给的题解更简单。。
学长曰:打个素数表会嘛。然后转成string,用string.find()搞定(这个函数是kmp实现的)
我的
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int prime[100000 + 5];
int p[100000 + 5];
char s[300];
int ans = 0;
int main()
{
int cnt = 0;
for(int i = 2; i < 100001; ++i){
if(!prime[i])p[cnt++] = i;
for(int j = 0; j < cnt && p[j] * i < 100001; ++j){
prime[i * p[j]] = 1;
if(i % p[j] == 0)
break;
}
}
memset(prime,0,sizeof(prime));
for(int i = 0; i < cnt; ++i)
prime[p[i]] = 1;
int len = 0;
while(scanf("%s",s) && s[0] != 0 && s[1] != '\0'){
int len = strlen(s);
ans = 0;
for(int i = 0; i < len; ++i){
for(int j = 0; j < 5 && j + i < len; ++j){
int num = 0;
for(int k = i; k <= i + j; ++k){
num = num * 10 + s[k] - '0';
}
if(prime[num])ans = max(ans,num);
}
}
printf("%d\n",ans);
}
return 0;
}
这学长的:
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <cctype>
using std::string;
int h[100000+10], pri[10000+10];
int Cnt;
string sp[10000 + 10];
void ItoA(int x, string &a)
{
a = "";
while(x)
{
char ch = (x % 10) + '0';
a = ch + a;
x /= 10;
}
//printf("%s\n", a.c_str());
}
void make_prime_list(int maxp)
{
Cnt = 0;
for(int i = 2; i <= maxp; i++)
{
if(!h[i]) pri[Cnt++] = i;
for(int j = 0; j < Cnt && pri[j] <= maxp / i; j++)
{
h[i * pri[j]] = true;
if(i % pri[j] == 0) break;
}
}
//printf("%d\n", Cnt); // 9592
for(int i = 0; i < Cnt; i++)
ItoA(pri[i], sp[i]);
}
char s[1010];
int main()
{
make_prime_list(100000);
while(scanf("%s", s) == 1)
{
string ss(s);
if(ss == "0") break;
for(int i = Cnt - 1; i >= 0; i--)
{
if((int)ss.find(sp[i]) != -1)
{
puts(sp[i].c_str());
break;
}
}
}
return 0;
}
UVA 12543
B - Longest Word
A word is composed of only letters of the alphabet (a-z, A-Z) and may contain one hyphen (-) or more.Given a text containing words, and other characters (punctuations, numbers, symbols, etc), you are towrite a program to find the longest word.Each letter or a hyphen in a word is counted as 1.
For example,
The length of Apple is 5
The length of son-in-law is 10
The length of ACM-ICPC is 8
Input
A text may contain several lines and paragraphs but the text does not exceed 10,000 characters. Noword can exceed 100 characters. The word ‘E-N-D’ indicates the end of input.
Output
Print out the longest word in small letters. If there exist several longest words, print only the first onefound in the text.
Sample Input
ACM International Collegiate Programming Contest (abbreviated asACM-ICPC or just ICPC) is an annual multi-tiered computer programmingcompetition among the universities of the world. The contest issponsored by IBM. Headquartered at Baylor University, with autonomousregions on six continents, the ICPC is directed by Baylor ProfessorWilliam B. Poucher, Executive Director, and operates under theauspices of the Association for Computing Machinery (ACM).The 2012 ACM-ICPC Asia Hatyai Regional Programming Contest isheld during 15-16 November 2012. It is hosted by Prince of SongklaUniversity, Hatyai campus. E-N-D
Sample Output
international
这题反正是把我坑了,碰到字符串每次都会被坑很久,首先每次读入一个单词,判断有没有不能算入长度的减去,还有注意的是输出为小写, 其他还是简单,我是直接用的STL。。。。
#include<cstdio>
#include<iostream>
#include<string>
#include<cctype>
#include<algorithm>
using namespace std;
string s,ss;
string end = "E-N-D";
int maxlen = -1;
bool mlf = 0,mrf = 0;
int main()
{
while(cin >> s){
if(s == end)break;
int len = s.length();
bool lf = 0,rf = 0;
if((s[0]>='A'&&s[0]<='Z') || (s[0]>='a'&&s[0]<='z') || s[0] == '-')lf = 0;
else len -= 1,lf = 1;
if((s[len-1]>='A'&&s[len-1]<='Z') || (s[len-1]>='a'&&s[len-1]<='z') || s[len] == '-')rf = 0;
else len -= 1,rf = 1;
if(maxlen < len)ss = s,maxlen = len,mlf = lf,mrf = rf;
}
transform(ss.begin(),ss.end(),ss.begin(),::tolower);
cout << ss.substr(0 + mlf,maxlen) << endl;
return 0;
}
ZOJ 2697
C - Colorful Rainbows
Description
Evelyn likes drawing very much. Today, she draws lots of rainbows on white paper of infinite size, each using a different color. Since there're too many rainbows now, she wonders, how many of them can be seen?
For simplicity, each rainbow Li is represented as a non-vertical line specified by the equation: y=aix+bi. A rainbow Li can be seen if there exists some x-coordinate x0 at which, its y-coordinate is strictly greater than y-coordinates of any other rainbows: aix0+bi > ajx0+bjfor all j != i.
Now, your task is, given the set of rainbows drawn, figure out the number of rainbows that can be seen.
Input
Standard input will contain multiple test cases. The first line of the input is a single integer T (1 <= T <= 60) which is the number of test cases. And it will be followed by T consecutive test cases.
There's a blank line before every case. In each test case, there will first be an integer n (1 <= n <= 5000), which is the number of rainbows. Then n consecutive real number pairs follow. Each pair contains two real numbers, ai and bi, representing rainbow Li: y=aix+bi. No two rainbows will be the same, that is to say, have the same a and b.
Output
Results should be directed to standard output. The output of each test case should be a single integer, which is the number of rainbows that can be seen.
Sample Input
2 1 1 1 3 1 0 2 0 3 0
Sample Output
1 2
这道题想到了就比较简单,首先可以画图分析,当斜率相等时,那么肯定只有b最大的可以看见,当斜率不等时,就看其交点x之间的关系,首先按斜率由小到大排序,先将一条直线压入栈中,x设为-inf,然后枚举每条直线求其与栈顶元素的交点,如果x <= 栈顶元素与其下一条直线的交点的x1,则栈顶元素出栈,继续判断,直到x > x1或栈为空,将本条直线入栈,栈底的元素的x始终为-inf,这样每条直线都会被判断一遍,最后的ans就是stack.size()。。
细节方面我在重载 < 时顺便把b也比较了,然后在直线去重那里和栈初始化那里有点细节。。。
代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<stack>
using namespace std;
struct li{
double a,b;
bool operator < (const li &x)const{return this -> a < x.a || this -> a == x.a && this -> b < x.b;}
}line[5000 + 5];
typedef pair<li,double>pii;
int T;
int n = 0;
double getx(li a1,li a2){
return (a2.b - a1.b)/(a1.a - a2.a);
}
int main()
{
freopen("C.in", "r", stdin);
freopen("C.out", "w", stdout);
scanf("%d",&T);
while(T--){
stack<pii>s;
scanf("%d", &n);
for(int i = 1; i <= n; ++i){
scanf("%lf%lf",&line[i].a,&line[i].b);
}
sort(line + 1, line + 1 + n);
int k = 1;
for(int i = 1; i < n; ++i){
if(line[i].a == line[i + 1].a)continue;//如果a相等,那么b小的肯定看不到
line[k++] = line[i];
}
line[k++] = line[n];
if(k <= 2){
printf("%d\n",k - 1);
continue;
}
s.push(make_pair(line[1],-99999999.0));
s.push(make_pair(line[2],getx(line[1],line[2])));
for(int i = 3; i < k; ++i){
double x = -99999999.0;
while(s.size()){
x = getx(s.top().first,line[i]);
if(x <= s.top().second)
s.pop();
else break;
}
s.push(make_pair(line[i],x));
}
printf("%d\n",s.size());
}
return 0;
}
ZOJ 3785
Description
It's Saturday today, what day is it after 11 + 22 + 33 + ... + NN days?
Input
There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:
There is only one line containing one integer N (1 <= N <= 1000000000).
Output
For each test case, output one string indicating the day of week.
Sample Input
2 1 2
Sample Output
Sunday Thursday
Hint
A week consists of Sunday, Monday, Tuesday, Wednesday, Thursday, Friday and Saturday.
这道题如果数据小还是很容易的,但N的范围10^9,就算是O(n)的算法都果断超,于是乎这种题果断先打表
由此表可知,N每过294就是一个循环,于是乎答案就好做了,把0 - 294的对应天数找出来,然后就直接出答案了。。
我用sum[i]表示到i时的应该是星期几记录出来,到时直接sum[N % 294]就行了,首先快速幂取模预处理(打表也可以,反正只用打到294)然后就随便乱搞
学长的题解给的。。。反正我看不懂。什么费马小定理。。QAQ。。
我的:
#include<cstdio>
#include<iostream>
using namespace std;
int sum[300];
int T;
char day[10][10] = {"Saturday","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday"};
int quick(int m, int q){
int d = 1,t = m;
while(q > 0){
if(t == 1)return d;
if(q%2) d = d * t % 7;
q /= 2;
t = t * t % 7;
}
return d;
}
int main()
{
sum[0] = 0;
for(int i = 1; i <= 300; ++i){
sum[i] = sum[i - 1] + quick(i,i);
sum[i] %= 7;
}
scanf("%d",&T);
while(T--){
int n;
scanf("%d",&n);
printf("%s\n",day[sum[n%294]]);
}
return 0;
}
<pre name="code" class="cpp">#include <cstdio>
char day[7][100] = {"Saturday", "Sunday", "Monday",
"Tuesday", "Wednesday", "Thursday", "Friday"};
int f[100];
int mul(int x, int y, int mod)
{
if(x == 0) return 0;
if(y == 0) return 1;
if(y == 1) return x;
if(y&1) return mul(x * x % mod, y / 2, mod) * x % mod;
else return mul(x * x % mod, y / 2, mod) % mod;
}
int main()
{
for(int i = 1; i <= 42; i++)
{
f[i] = f[i-1] + mul(i % 7, i % 6, 7);
f[i] %= 7;
//printf("%d ", f[i] % 7);
//if(i % 42 == 0) printf("\n");
//printf("%d\n", f[i] % 7);
}
int T; scanf("%d", &T);
while(T--)
{
int n; scanf("%d", &n);
int ans = (int)(((long long)n / 42 * f[42] + f[n % 42]) % 7);
//printf("ans = %d\n", ans);
puts(day[ans]);
}
return 0;
}
Description
The 11th Zhejiang Provincial Collegiate Programming Contest is coming! As a problem setter, Edward is going to arrange the order of the problems. As we know, the arrangement will have a great effect on the result of the contest. For example, it will take more time to finish the first problem if the easiest problem hides in the middle of the problem list.
There are N problems in the contest. Certainly, it's not interesting if the problems are sorted in the order of increasing difficulty. Edward decides to arrange the problems in a different way. After a careful study, he found out that the i-th problem placed in the j-th position will add Pij points of "interesting value" to the contest.
Edward wrote a program which can generate a random permutation of the problems. If the total interesting value of a permutation is larger than or equal to M points, the permutation is acceptable. Edward wants to know the expected times of generation needed to obtain the first acceptable permutation.
Input
There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:
The first line contains two integers N (1 <= N <= 12) and M (1 <= M <= 500).
The next N lines, each line contains N integers. The j-th integer in the i-th line is Pij (0 <= Pij <= 100).
Output
For each test case, output the expected times in the form of irreducible fraction. An irreducible fraction is a fraction in which the numerator and denominator are positive integers and have no other common divisors than 1. If it is impossible to get an acceptable permutation, output "No solution" instead.
Sample Input
2 3 10 2 4 1 3 2 2 4 5 3 2 6 1 3 2 4
Sample Output
3/1 No solution
这道题恶心了好久,什么概率完全不会啊。。。期望次数什么鬼,随后还把两个比搞错了WA了。。
但是思路还是比较简单,首先因为n <= 12,所以可以状压dp。。首先枚举每一种状态,然后就状压dp。。枚举每个位置,设dp[i][j]为i状态的j趣味值的操作数。要注意的就是位运算要熟悉,并且二进制低位从0算起。。
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int T;
int p[15][15];
int dp[(1 << 12) + 10][500 + 5];//dp[i][j] 为到i状态时趣味值为j时的操作次数
int fac(int n){
int ret = 1;
for(int i = 1; i <= n; ++i)ret *= i;
return ret;
}
int gcd(int a,int b){
return b ? gcd(b, a % b) : a;
}
int main()
{
scanf("%d",&T);
while(T--){
memset(dp,0,sizeof(dp));
int n,m;
scanf("%d%d",&n,&m);
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j)
scanf("%d",&p[i][j]);
}
dp[0][0] = 1;
int tot = (1 << n);
for(int i = 0; i < tot; ++i){
int cnt = 0;
for(int k = 1; k <= n; ++k)
if(i & (1 << (k - 1)))cnt++;
for(int j = 1; j <= n; ++j){
if(i & (1 << (j - 1)))continue;
for(int k = 0; k <= m; ++k){
int hp = k + p[cnt + 1][j];
if(hp > m)hp = m;
dp[i | (1 << (j - 1))][hp] += dp[i][k];
}
}
}
int fm = dp[tot - 1][m],fz = fac(n);
if(fm){
int g = gcd(fz,fm);
printf("%d/%d\n",fz / g, fm / g);
}
else printf("No solution\n");
}
return 0;
}