水题(2)找规律

目录

CSU 1150: 食用油

CSU 1263: 最少钱币数

CSU 1271: Brackets Sequence(括号匹配问题)

CSU 1284: Cutting Cake

CSU 1346: 变色龙

CSU 1363: Count 101

CSU 1401: 插入排序

CSU 1519: Sum of Integers

CSU 1525: Algebraic Teamwork

CSU 1558: 和与积

CSU 1643: +1+2?

CSU 1723: 想打架吗?算我一个!所有人,都过来!

CSU 1727: The Fake Coin

CSU 1903: Tricky数

CSU 2044: 最短长度

CSU 2046: sequence

CSU 2059: Water Problem(Z字形分隔平面)

CSU 2076: 简单序列

CSU 2117: Palindromic Password

CodeForces 1A Theatre Square

CodeForces 471C MUH and House of Cards

CodeForces 621B Wet Shark and Bishops

CodeForces 701B Cells Not Under Attack

HDU 1049 Climbing Worm

HDU 1197 Specialized Four-Digit Numbers

HDU 1597 find the nth digit

LightOJ 1265 Island of Survival

Switch Game HDU - 2053


CSU 1150: 食用油

题目:

Description

        从前,有一个远得要命的小岛上住着一位打油师。每天有很多顾客去那里购买安全无害的食用油,但打油师是一个吝啬鬼,他从来不会用自己的油桶给顾客打油,因为这样会弄脏了他的油桶,所以顾客都需要带上自己的油桶前往远得要命的小岛上购买一定量的食用油C升。但并不是每个顾客都有刚好为C升的油桶,因此很多顾客都会带着两只容量分别为A,B的油桶赶往远得要命的小岛,因为他们知道,打油师会想办法弄到他们想要的C升食用油,毕竟吝啬鬼不会让上门的生意溜掉。

        打油师现在麻烦了,他必须用顾客带来的容量为A,B的桶,量出所需的C升油出来,这是一项相当庞大的工程,他需要尽量减少自己的操作次数来节约时间,以此来处理大量顾客的需求。

他可以对容量为A,B的油桶进行如下操作:

1. 从仓库中向A添加食用油至A容量已满

2. 从仓库中向B添加食用油至B容量已满

3. 把A桶的食用油倒入B中(不超过B的容量;B可能未满;A可能有剩余)

4. 把B桶的食用油倒入A中(不超过A的容量;A可能未满;B可能有剩余)

5. 把A中的油全部到会仓库

6. 把B中的油全部到会仓库

打油师每次使用以上的任一种操作都要算一次操作次数。

Input

 多组测试数据,每一行为3个整数:A, B, C。所表示的意思如题意 1<=A,B<=100;C<=max(A,B)

Output

 计算最少的操作次数并输出结果,然后输出换行

Sample Input

3 5 4

Sample Output

6

题目的数据其实是保证gcd(a,b)整除c的

思路:把c表示成ax+by的形式,xy一正一负,有2种情况,所以要调用2次f,取较小值

f里面又要分3种情况,c=a,c<a,c>a

代码:

#include<iostream>
#include<algorithm>
using namespace std;
 
int f(int a, int b, int c)
{
	if (a == c)return 1;
	int r = 0;
	while ((b*r + c) % a)r++;
	return ((b*r + c) / a + r - (c < a)) * 2;
}
 
int main()
{
	int a, b, c, rr;
	while (cin >> a >> b >> c)cout << min(f(a, b, c), f(b, a, c)) << endl;
	return 0;
}

CSU 1263: 最少钱币数

题目:

Description

作为A公司的职员,最盼望的日子就是每月的8号了,因为这一天是发工资的日子,养家糊口就靠它了。但是对于公司财务处的工作人员来说,这一天则是很忙碌的一天,财务处的小胡最近就在考虑一个问题:如果每个员工的工资额都知道,最少需要准备多少张人民币,才能在给每位职员发工资的时候都不用老师找零呢?这里假设员工的工资都是正整数,单位元,人民币一共有100元、50元、10元、5元、2元和1元六种。

Input

输入数据包含多个测试实例,每个测试实例的第一行是一个整数n(n<100),表示老师的人数,然后是n个老师的工资(工资<5000)。

Output

每个测试用例输出一行,即凑成钱数值M最少需要的钱币个数。如果凑钱失败,输出“Impossible”。你可以假设,每种待凑钱币的数量是无限多的。

Sample Input

3
1 2 3
2
1 2

Sample Output

4
2

代码:

#include<iostream>
using namespace std;
 
int main()
{
	int n, a;
	while (cin >> n)
	{
		int ans = 0;
		while (n--)
		{
			cin >> a;
			ans += a / 100, a %= 100;
			ans += a / 50, a %= 50;
			ans += a / 10, a %= 10;
			ans += a / 5, a %= 5;
			if (a)ans++;
			if (a > 2)ans++;
		}
		cout << ans << endl;
	}
	return 0;
}

CSU 1271: Brackets Sequence(括号匹配问题)

题目:

Description

Let us define a regular brackets sequence in the following way:

1. Empty sequence is a regular sequence.

2. If S is a regular sequence, then (S) is a regular sequence.

3. If A and B are regular sequences, then AB is a regular sequence.

For example, these sequences of characters are regular brackets sequences: (), (()), ()(), ()(()), ((())())().

And all the following character sequences are not: (, ), ((), ()), ())(, (()(, ()))().

A sequence of characters '(' and ')' is given. You can insert only one '(' or ')' into the left of the sequence, the right of the sequence, or the place between any two adjacent characters, to try changing this sequence to a regular brackets sequence.

Input

The first line has a integer T (1 <= T <= 200), means there are T test cases in total.

For each test case, there is a sequence of characters '(' and ')' in one line. The length of the sequence is in range [1, 105].

Output

For each test case, print how many places there are, into which you insert a '(' or ')', can change the sequence to a regular brackets sequence.

What's more, you can assume there has at least one such place.

Sample Input

4
)
())
(()(())
((())())(()

Sample Output

1
3
7
3

题意:

给定一个括号串,只需插入1个括号即可全部匹配

求有多少个地方可以插入1个括号使得全部匹配成功

思路:

首先,如何判断一个括号串是否完全匹配?

用数组num记录,num[i]表示前i个字符中,)和(的数量差

如果num[i]全部不超过0,而且最后一项为0,那么就可以完全匹配,反之亦然。

所以本题的答案是:

如果num[i]中有出现1,那么第一个1的位置就是答案

否则,最后一个0的位置与括号串长度之差就是答案

代码:

#include<iostream>
#include<string.h>
using namespace std;
 
char ch[100005];
int num[100005];//前i个字符中)和(的数量差
 
int main()
{
	int T, len;
	cin >> T;	
	while (T--)
	{
		cin >> ch + 1;
		len = strlen(ch+1);
		num[0] = 0;
		int i, k0 = 0;
		for (i = 1; i <= len; i++)
		{
			if (ch[i] == '(')num[i] = num[i - 1] - 1;
			else num[i] = num[i - 1] + 1;
			if (num[i] > 0)
			{
				cout << i << endl;
				break;
			}
			if (num[i] == 0)k0 = i;
		}
		if (i > len)cout << len - k0 << endl;
	}
	return 0;
}

CSU 1284: Cutting Cake

题目:

Description

一个蛋糕切N刀,最多能得到多少块?切的过程中不能改变任意一块蛋糕的位置。

Input

输入数据的第一行包含一个整数T (1 <= T <= 100),表示接下来一共有T组测试数据。

每组测试数据占一行,包含一个整数N (1 <= N <= 100),含义同上。

Output

用一行输出一个整数,表示上述问题的答案。

Sample Input

3
2
3
4

Sample Output

4
8
15

代码:

#include<iostream>
using namespace std;
 
int main()
{
	int t, n;
	cin >> t;
	while (t--)
	{
		cin >> n;
		cout << (n*n - 1)*n / 6 + n + 1 << endl;
	}
	return 0;
}

CSU 1346: 变色龙

题目:

Description

    在一个美丽的小岛上住着一群变色龙:其中有X只变色龙是红色的,Y只变色龙是绿色的,Z只变色龙是蓝色的。
    每个时刻会有两只不同颜色的变色龙相遇,相遇后他们会同时变成第三种颜色。比如,如果一只红色的变色龙和一只蓝色的变色龙相遇了,他们就会同时变成绿色的变色龙,如果一只绿色的变色龙和一只蓝色的变色龙相遇了,他们就会同时变成红色的变色龙,等等。
    那么最后是否有可能所有的变色龙都是同一种颜色呢?

Input

    输入的第一行包含一个整数T (1 <= T <= 100),表示接下来一共有T组测试数据。
    每组数据占一行,包含三个整数XYZ (1 <= XYZ <= 109),含义同上。

Output

    对于每组测试数据,如果最后有可能所有的变色龙都是同一种颜色,用一行输出“Yes”(不包括引号),否则输出“No”(不包括引号)。

Sample Input

4
1 1 1
1 2 3
7 1 2
3 7 5

Sample Output

Yes
No
Yes
No

代码:

#include<iostream>
using namespace std;
 
int main()
{
	int t, x, y, z;
	cin >> t;
	while (t--)
	{
		cin >> x >> y >> z;
		if (x % 3 == y % 3 || x % 3 == z % 3 || y % 3 == z % 3)cout << "Yes\n";
		else cout << "No\n";
	}
	return 0;
}

CSU 1363: Count 101

题目:

Description

You know YaoYao is fond of his chains. He has a lot of chains and each chain has n diamonds on it. There are two kinds of diamonds, labeled 0 and 1. We can write down the label of diamonds on a chain. So each chain can be written as a sequence consisting of 0 and 1.
We know that chains are different with each other. And their length is exactly n. And what’s more, each chain sequence doesn’t contain “101” as a substring. 
Could you tell how many chains will YaoYao have at most?

Input

There will be multiple test cases in a test data. For each test case, there is only one number n(n<10000). The end of the input is indicated by a -1, which should not be processed as a case.

Output

For each test case, only one line with a number indicating the total number of chains YaoYao can have at most of length n. The answer should be print after module 9997.

Sample Input

3
4
-1

Sample Output

7
12

代码:

#include<iostream>
using namespace std;
 
int main()
{
	int ans[10001], n;
	ans[0] = 1, ans[1] = 2, ans[2] = 4;
	for (int i = 3; i <= 10000; i++)ans[i] = (ans[i - 1] * 2 - ans[i - 2] + ans[i - 3]) % 9997;
	while (cin >> n)if (n + 1)cout << ans[n] << endl;
	return 0;
}

CSU 1401: 插入排序

题目:

Description

每次选择序列最左边的数,然后将其插入到序列中任意一个位置。求至少需要重复进行多少次上述操作,才可以将序列变为一个递增序列。

Input

输入的第一行包含一个整数T (T > 0),表示一共有T组测试数据.

对于每组测试数据,第一行包含一个整数n (1 ≤ n ≤ 105),表示这个序列中一共有n个整数。第二行包含n个各不相同的整数(这些整数均在[1, 109]范围内),依次描述了这个序列中的各个数。

Output

对于每组测试数据,输出一个整数,表示至少需要重复进行多少次上述操作,才可以将这个序列变为一个递增序列。

Sample Input

3
3
1 2 3
3
1 3 2
5
1 5 4 3 2

Sample Output

0
2
4

代码:

#include<iostream>
using namespace std;
 
int main()
{
	int t, n, m, a, b, k;
	cin >> t;
	while (t--)
	{
		cin >> n;
		m = n, a = 0, k = n - 1;
		while (n--)
		{
			cin >> b;
			if (a > b)k = n;
			a = b;
		}
		cout << m - k - 1 << endl;
	}
	return 0;
}

CSU 1519: Sum of Integers

这个题目,是我目前遇到过的最神坑的题,没有之一!

题目是这样的:

Description

Given two integers A, B (A < B), we can select some different integers (at least one) arbitrarily from A, A+1, ..., B-1, B and sum them. Question: How many different sums can we get?
For example, if select some integers from 1, 2, 3 and sum them, we can get six different sums: 1 (itself), 2 (itself), 3 (itself or 1+2), 4 (1+3), 5 (2+3), 6 (1+2+3). If select some integers from 2, 3, 4 and sum them, we can get seven different sums: 2 (itself), 3 (itself), 4 (itself), 5 (2+3), 6 (2+4), 7 (3+4), 9 (2+3+4).

Input

The first line contains the number of test cases T (0 < T ≤ 200).
For each test case, there is only one line with two integers A, B (0 < A < B < 10^9) separated by a single space.

Output

For each test case, output the number of different sums in one line.

Sample Input

2
1 3
2 4

Sample Output

6
7

使用我们中南的OJ的同学一定要注意了,你在OJ上面看到的肯定是B<109,不过其实是10^9,相信你也已经猜到了。

神坑的是,标程居然是这样的:

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int main()
{
    long long a,b,ans;
    int t;
    scanf("%d",&t);
    while (t--)
    {
        scanf("%lld%lld",&a,&b);
        ans=(b-a+1)*(a+b)/2-a-a+2;
        printf("%lld\n",ans);
    }
}

在我陷入绝境之后,出于好奇,把标程拿去提交了一下,果然AC了。

可是,输入1 3 5,输出的居然是8!

明显只有3,4,5,3+4,3+5,4+5,3+4+5这7种!

无语。。。。。

无语。。。。

无语。。。

无语。。

。。。

。。

好了,说说我的思路吧。

首先,如果取1个数,那么大小就是从a到b

如果取2个数,那么大小就是a+a+1到b-1+b

。。。。。。

如果取b-a+1个数,就只能是a+a+1+...+b

答案就是这些区间的并的长度。

代码1:

#include<iostream>
using namespace std;
 
int main()
{
    int t;
    cin >> t;
    int a, b;
    int sum, dif;
    while (t--)
    {
        cin >> a >> b;
        sum = (a + b)*(b - a + 1) / 2 - a + 1;
        for (int i = 1; i <= b - a; i++)
        {
            dif = (a + a + i)*(i + 1) / 2 - (b + b - i + 1)*i / 2 - 1;
            if (dif > 0)sum -= dif;
        }
        cout << sum << endl;
    }   
    return 0;
}

然而超时了(这是在我发现其实109是10^9之前写的代码)

既然如此,就只能把这个for循环消掉了,也就是把公式求出来。

代码2:

#include<iostream>
#include<math.h>
using namespace std;
 
int main()
{
    int t;
    cin >> t;
    long long a, b;
    long long sum, delta;
    long long x;
    while (t--)
    {
        cin >> a >> b;
        sum = (a + b)*(b - a + 1) / 2 - a + 1;
        delta = (b - a)*(b - a) - 4 * (a - 1);
        if (delta <= 0)sum -= (1 - (b - a)*(b - a))*(b - a) / 6 + (a - 1)*(b - a);
        else
        {
            x = int((b - a - sqrt(delta)) / 2);
            sum -= (x*(x + 1)*(x + x + 1) / 3 - (b - a)*x*(x + 1) + (a - 1)*(x + x + 1));
        }
        cout << sum << endl;
    }
    return 0;
}

我几乎确定这个代码不会有任何问题,但是依然是wrong answer,所以我才会好奇,标程到底长啥样。

看到真相的我哭了。

那年院赛,就是被这个题目耗死的。

CSU 1525: Algebraic Teamwork

题目:

InputOutputSample Input

3
1
2
2171

Sample Output

0
1
6425

题意:

求n个元素的置换中,有多少个不是幂等的

n个元素的置换有n!个,其中幂等的就只有1个,所以答案是n!-1

代码:

#include<iostream>
using namespace std;
 
int p = 1000000007;
long long fac[100001];
 
int main()
{
	fac[0] = 1;
	for (int i = 1; i <= 100000; i++)fac[i] = fac[i - 1] * i%p;
	int t, n;
	cin >> t;
	while (t--)
	{
		cin >> n;
		cout << (fac[n] + p - 1) % p << endl;
	}
	return 0;
}

CSU 1558: 和与积

题目:

Description

构造N个正数(每个数不超过1000000),使所有数的和与所有数的积相差刚好等于D,按非递减序输出。

Input

多组测试数据(不超过1000组),每行两个正整数N和D。(2<=N<=1000,D<=1000)

Output

每行应该按非递减序输出对应的N个数。

Sample Input

2 1
3 5

Sample Output

2 3
1 2 8

代码:

#include<iostream>
using namespace std;
 
int main()
{
	int n, d;
	while (cin >> n >> d)
	{
		d += n, n -= 2;
		while (n--)cout << 1 << " ";
		cout << 2 << " " << d << endl;
	}
	return 0;
}

CSU 1643: +1+2?

题目:

Description

有这样一个金字塔形的二维数表,满足A[i+1][j]=A[i][j]+1且A[i+1][j+1]=A[i][j]+2,0<=j<=i<=10^18。已知A[0][0]=0,若询问你任意的A[i][j]的值,请输出正确的值。

Input

多组输入,每组输入包含两个数x和y,0<=x,y<=10^18;

Output

对于每组合法输入,输出A[x][y]的值,否则输出-1。

Sample Input

0 0

1 2

2 1

Sample Output

0

-1

3

代码:

#include <iostream>
#include<stdio.h>
using namespace std;
 
int main()
{
	long long a, b;
	while (scanf("%lld%lld",&a,&b)!=EOF)
	{
		if (a < b)printf("-1\n");
		else printf("%d\n", a + b);
	}
	return 0;
}

CSU 1723: 想打架吗?算我一个!所有人,都过来!

Description

现在《炉石传说》这款卡牌游戏已经风靡全球。2015年加入环境的“黑石山的火焰”扩展带来了一个新套牌的核心卡片“恐怖的奴隶主”,而这套统治游戏的套牌叫做“奴隶战”。“恐怖的奴隶主”的登场音效“想打架吗?算我一个!”一定在所有这个时代的《炉石传说》玩家心里留下来难以磨灭的印象。

“恐怖的奴隶主”是一个有3点生命值的生物,当其在场上受到非致命伤害时(如3点生命值的奴隶主受到1点或2点伤害时,或者2点生命值的奴隶主受到1点伤害时)会召唤一个新的3点生命值的“恐怖的奴隶主”,受到致命伤害(伤害大于等于现有生命值)时则会直接死去。另外一类卡片可以使全部生物造成1点伤害(降低1点生命),被称为“旋风斩效果”。因此“恐怖的奴隶主”,在场上经过多次“旋风斩效果”就可能由一个变成很多个,同时发出那个令人恐惧的声音“所有人,都过来!”。

另一方面,《炉石传说》规定,场上最多存在7个生物,这极大地限制了“恐怖的奴隶主”“越生越多”。当一次“旋风斩效果”发生时,优先处理受到非致命伤害的“恐怖的奴隶主”,召唤新的“恐怖的奴隶主”,直到生物数量达到7个不再继续召唤新的“恐怖的奴隶主”,然后清除掉生命值降为0或0以下的“恐怖奴隶主”。如场上有7个生命值为1的“恐怖的奴隶主”,则一次“旋风斩效果”后场上有0个“恐怖的奴隶主”。又如,场上有6个生命值为3的“恐怖的奴隶主”,则一次“旋风斩效果”后场上有6个2点生命的“恐怖的奴隶主”以及1个3点生命的“恐怖的奴隶主”。又如,场上有4个1点生命的“恐怖的奴隶主”以及2个2点生命的“恐怖的奴隶主”,则一次“旋风斩效果”后场上有2个1点生命的“恐怖的奴隶主”以及1个3点生命的“恐怖的奴隶主”。

现在场上有1个3点生命的“恐怖的奴隶主”,问n次“旋风斩效果”后场上有多少个“恐怖的奴隶主”,在这n次“旋风斩效果”每次结束时,场上存在“恐怖的奴隶主”最多的个数是多少。

Input

第1行输入一个k(1<=k<=100000)代表有接下来有k组数据。

第2到k+1行 每行输入一个n(0<=n<=10^9),意义如前文所述。

Output

对于每一组数据,用一行输出组数, 1个3点生命的“恐怖的奴隶主”,在n次“旋风斩效果”后场上有多少个“恐怖的奴隶主”,以及在这n次“旋风斩效果”每次结束时,场上存在“恐怖的奴隶主”最多的个数是多少。具体格式见样例。

Sample Input

2
2
4

Sample Output

Case 1: 4 4
Case 2: 6 6

这个题目的k很大,所以用cout是不行的。

既然k这么大,肯定是有一些规律的。

实际上规律特别简单,奴隶主的数量和生命值的状态随着n的变化如下:

(从n=0开始)3——23——1233——122333——112223——11123——1233

这就已经很明显了,开始以4为周期进行循环了。

代码:

#include<iostream>
using namespace std;
 
int main()
{
    int t;
    scanf("%d", &t);
    int n;
    int f=0;
    while (t--)
    {
        f++;
        scanf("%d", &n);
        int a = 1, b = 1;
        if (n < 3)
        {
            if (n == 1)a = 2 ;
            if (n == 2)a = 4;
            b = a;
        }
        else
        {
            n = n % 4;
            a = 6;
            if (n == 1)a = 5;
            else if (n == 2)a = 4;
            b = 6;
        }
        printf("Case %d: %d %d\n",f,a,b);
    }
    return 0;
}

CSU 1727: The Fake Coin

题目:

Description

There are n coins,one of them is fake.The fake coin is heavier than a genuine one.If you have a balance,how many times at least you need to use it to find the fake coin?

Input

The first line contains an integer T (T<=100), means there are T test cases.
For each test case, there is only one line with an integer n (1 <= n <= 10000). The num of coins.

Output

For each test case, output the least times you need.

Sample Input

3
2
3
4

Sample Output

1
1
2

这个题目,为了加快速度,直接硬编码了。

原理就不说了。。。小学生都会。

代码:

#include<iostream>;
using namespace std;
 
int main()
{
    int t;
    cin >> t;
    int n;
    while (t--)
    {
        cin >> n;
        if (n > 6561)cout << 9;
        else if (n > 2187)cout << 8;
        else if (n > 729)cout << 7;
        else if (n > 243)cout << 6;
        else if (n > 81)cout << 5;
        else if (n > 27)cout << 4;
        else if (n > 9)cout << 3;
        else if (n > 3)cout << 2;
        else if (n > 1)cout << 1;
        else cout << 0;
        cout << endl;
    }
    return 0;
}

CSU 1903: Tricky数

题目:

Description

小A很喜欢对着别人”233”,然后他发现有很多数字可以提取出他喜欢的233的子序列。于是乎自以为是的他决定自己定义一种Tricky数,这种数字在不重复利用每一位的条件下,可以提取出k(k>0)个子序列,而且保证提取出这k个子序列都是”233”,且不再剩余任何数字。

子序列:某个序列的子序列是从最初序列中任意取出一些元素,在不破坏原始的顺序基础上排成的序列。

Input

第一行输入一个正整数T,表示数据组数 T<=1000 每组数据一行输入一个数字v(0<v<=10^1000)

Output

对于每组数据如果v是Tricky数,输出”Yes” , 否则输出”No”

Sample Input

2
232333
233323

Sample Output

Yes
No

直接从左往右扫描,更新2的数量和3的数量即可

我在比赛时候提交的代码:

#include<iostream>
#include<string.h>
using namespace std;
 
char c[1005];
 
bool ok()
{
	int len=strlen(c);
	int n2=0,n3=0;
	for(int i=0;i<len;i++)
	{
		if(c[i]=='2')n2++;
		if(c[i]=='3')n3++;
	}
	if(n3!=n2*2)return false;
	if(len!=n2*3)return false;
	n2=0,n3=0;
	int s=0;
	for(int i=0;i<len;i++)
	{
		if(c[i]=='2')s+=2;
		if(c[i]=='3')s--;
		if(s<0)return false;
	}
	return true;
}
 
int main()
{
	int T;
	cin>>T;	
	while(T--)
	{
		scanf("%s",&c);
		if(ok())cout<<"Yes\n";
		else cout<<"No\n";
	}
	return 0;
}

ok函数可以化简成:

bool ok()
{
	int len = strlen(c),s = 0;
	for (int i = 0; i<len; i++)
	{
		if (c[i] == '2')s += 2;
		if (c[i] == '3')s--;
		if (s<0)return false;
	}
	return s==0;
}

CSU 2044: 最短长度

题目:

Description

给定一个整数n,要求找出一个最短的数字字符串S,使得所有1到n的整数都是S的子序列。比如n=10,那么S=”1234056789”的时候,是满足条件的。这个时候S的长度是10。

现在给出一个n,要求输出最短S的长度。

Input

程序有多组输入,每组输入给出一个整数n (1<=n<=1e10000)。

Output

输出最短S的长度

Sample Input

10

Sample Output

10

找规律

代码:

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
 
int main()
{
	char s[10005],minn;
	while(scanf("%s",&s)!=EOF)
	{
		int len=strlen(s+1);
		minn=s[0];
		bool flag=true;
		for(int i=1;i<=len;i++)
		{
			if(s[i]>s[0])break;
			if(s[i]<s[0])flag=false;
		}
		if(!flag)minn--;
		printf("%d\n",len*10+minn-'0');
	}
	return 0;
}

CSU 2046: sequence

题目:

Description

给出一个长度为N的正整数序列a,你有两种变换操作:
1.把数列中的某个数乘 2。
2.把数列中的所有数减 1。
现在你需要通过最少的变换操作把这个数列中的数全部变成 0。

Input

第一行一个N。下面 N 行,每行一个正整数 Ai 描述这个数列。1 < =n < =200000, 1 < =ai < =109

Output

输出一行一个正整数,表示最少的变换次数。

Sample Input

2
1
2

Sample Output

3

思路:

所有操作中,减1操作的数量肯定等于数列中最大的那个数,

而乘2操作是一次操作只能针对一个数的,所以只需要把每个数的乘2操作的次数全部加起来即可。

至于每个数一共需要进行多少次乘2操作,很容易找出规律,答案就是一个数至少需要乘多少次2才能大于等于原数列中最大的那个数。

代码:

#include<iostream>
#include<stdio.h>
using namespace std;
 
int num[200001];
 
int f(int maxx,int minn)
{
	int r=0;
	while(minn<maxx)minn*=2,r++;
	return r;
}
 
int main()
{
	int n,maxx=0,minn=1234567890;
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		scanf("%d",&num[i]);
		if(maxx<num[i])maxx=num[i];
		if(minn>num[i])minn=num[i];
	}
	int s=0;
	for(int i=0;i<n;i++)s+=f(maxx,num[i]);
	printf("%d",maxx+s);
	return 0;
}

CSU 2059: Water Problem(Z字形分隔平面)

题目:

Description

​ 一条‘Z’形线可以将平面分为两个区域,那么由N条Z形线所定义的区域的最大个数是多少呢?每条Z形线由两条平行的无限半直线和一条直线段组成

Input

首先输入一个数字T(T<100),代表有T次询问 每次询问输入一个数字N(N<1e8),代表有N条Z形线

Output

对于每次询问,在一行输出N条‘Z’形线所能划分的区域的最大个数为多少

Sample Input

2
1
2

Sample Output

2
12

思路:

首先考虑一个类似的问题:

有N组直线,每组都由3条平行的直线构成,3条直线的间距可以调整。

那么N组直线最多划分出多少个区域?

这个问题就很容易求出来,3n(3n-1)/2+1

本题的答案,就是把每组3条平行直线变成Z,也就是在3n(3n-1)/2+1的基础上再减2n即可

代码:

#include<iostream>
using namespace std;
 
int main()
{
	long long a;
	cin >> a;
	while (cin >> a)cout << (a * 9 - 7)*a / 2 + 1 << endl;
	return 0;
}

CSU 2076: 简单序列

题目:

Description

​ 小明喜欢数学,尤其喜欢研究不同的序列,发现了一种zz-序列,若序列a是长度为n的zz-序列,那么满足: 1.对于偶数i,ai ≥ ai − 1 2.对于奇数i, ai ≤ ai − 1

比如,[1,2,1,2]和[1,1,1,1]是zz-序列,而[1,2,3,4]不是。

Input

输入第一行包括一个整数n,(1 ≤ n ≤ 1000),表示数列a中有n个元素。 输入第二行为数列a中的元素ai(1 ≤ ai ≤ 109)。

Output

如果数列a可能变成zz-序列,那么输出a变换后的序列,否则输"-1"。

Sample Input

5
1 3 2 2 5

Sample Output

1 5 2 3 2

思路:

不管什么序列都是可以变换成功的,方法有很多种,其中一种简单的方法是:

把所有数排序,然后把不超过中位数的数都放到最终结果的奇数位,超过中位数的数都放到最终结果的偶数位

代码:

#include<iostream>
#include<algorithm>
using namespace std;
 
int main()
{
	int n, list[1000];
	cin >> n;
	for (int i = 0; i < n; i++)cin >> list[i];
	sort(list, list + n);
	for (int i = 0, j = n - 1; i <= j; i++, j--)
	{
		if (i)cout << ' ';
		cout << list[i];
		if (j>i)cout << ' ' << list[j];
	}
	return 0;
}

CSU 2117: Palindromic Password

题目:

Description

The IT department at your school decided to change their password policy. Each password will have to consist of N 6-digit numbers separated by dashes, where N will be determined by the phase of the moon and the weather forecast for the day after it will be generated.

You realized that, if all of the numbers were palindromes (same numbers as the original ones if read backwards), you would have to remember a bunch of 3-digit numbers, which did not sound that bad (at the time).

In order to generate your password of N numbers, you get a list of N randomly generated 6-digit numbers and find the palindromic number closest to them.

Of course, you would like to automate this process...

Input

The first line of the input contains a single positive integer N ≤ 1000 indicating the number of six-digit numbers in the input. Each of the next N lines contains a six-digit number without leading zeroes.

Output

For each six-digit number in the input, output another six-digit number that is closest to it and is also a palindrome. “Closest” in this context means “a number having the smallest absolute difference with the original number”. If there are two different numbers satisfying the above condition, output the smaller one of the two. Remember, no leading zeroes.

Sample Input

2
123321
123322

Sample Output

123321
123321

思路:

答案的前三位,无非三种情况,枚举一下就行

代码:

#include<iostream>
#include<cmath>
using namespace std;
 
int f(int x)//turn abc to abccba
{
	return x * 1000 + x % 10 * 100 + x / 10 % 10 * 10 + x / 100;
}
 
int main()
{
	int n, a, b, ans;
	cin >> n;
	while (n--)
	{
		cin >> a;
		if (a <= 99999)
		{
			cout << 100001 << endl;
			continue;
		}
		b = a / 1000, ans = b;
		if (b < 999 && abs(f(ans) - a) > abs(f(b + 1) - a))ans = b + 1;
		if (b > 100 && abs(f(ans) - a) >= abs(f(b - 1) - a))ans = b - 1;
		cout << f(ans) << endl;
	}
	return 0;
}

CodeForces 1A Theatre Square

题目:

Description

Theatre Square in the capital city of Berland has a rectangular shape with the size n × m meters. On the occasion of the city's anniversary, a decision was taken to pave the Square with square granite flagstones. Each flagstone is of the size a × a.

What is the least number of flagstones needed to pave the Square? It's allowed to cover the surface larger than the Theatre Square, but the Square has to be covered. It's not allowed to break the flagstones. The sides of flagstones should be parallel to the sides of the Square.

Input

The input contains three positive integer numbers in the first line: n,  m and a (1 ≤  n, m, a ≤ 109).

Output

Write the needed number of flagstones.

Sample Input

Input

6 6 4

Output

4

代码:

#include<iostream>
using namespace std;
 
int main()
{
	long long m,n,a;
	while (cin >> m >> n >> a)cout << ((m - 1) / a + 1)*((n - 1) / a + 1) << endl;
	return 0;
}

要注意的地方就是,当a=1,m和n特别大的时候,结果用int来保存是不够的。

CodeForces 471C MUH and House of Cards

题目:

Description

Polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant Horace from the zoo of Kiev decided to build a house of cards. For that they've already found a hefty deck of n playing cards. Let's describe the house they want to make:

  1. The house consists of some non-zero number of floors.

  2. Each floor consists of a non-zero number of rooms and the ceiling. A room is two cards that are leaned towards each other. The rooms are made in a row, each two adjoining rooms share a ceiling made by another card.

  3. Each floor besides for the lowest one should contain less rooms than the floor below.

Please note that the house may end by the floor with more than one room, and in this case they also must be covered by the ceiling. Also, the number of rooms on the adjoining floors doesn't have to differ by one, the difference may be more.

While bears are practicing to put cards, Horace tries to figure out how many floors their house should consist of. The height of the house is the number of floors in it. It is possible that you can make a lot of different houses of different heights out of n cards. It seems that the elephant cannot solve this problem and he asks you to count the number of the distinct heights of the houses that they can make usingexactly n cards.

Input

The single line contains integer n (1 ≤ n ≤ 1012) — the number of cards.

Output

Print the number of distinct heights that the houses made of exactly n cards can have.

Sample Input

Input

13

Output

1

Input

6

Output

0

Hint

In the first sample you can build only these two houses (remember, you must use all the cards):

Thus, 13 cards are enough only for two floor houses, so the answer is 1.

The six cards in the second sample are not enough to build any house.

看图应该就能理解大意了。

输入card的数量,输出摆的层数可能会有多少种情况。

比如上面的图片,只能摆2层,层数只有1种情况,那么答案为1。

如果根本没法摆,答案为0。

然后就是找规律了。

首先按行分。

对于左图,一楼有4个房子,有3个天花板,card有4*3-1个

二楼有1个房子,0个天花板,card有1*3-1个

对于右图,一楼有3个房子,2个天花板,card有3*3-1个

二楼有2个房子,1个天花板,card有2*3-1个。

然后,假设有h层,从上往下各有x1、x2、x3。。。。。。xh个房子。

现在就是要求方程n=(x1*3-1)+(x2*3-1)+......+(xh*3-1)是否有解,而且x1<x2<x3......<xh

方程可以化简为n+h=3*(x1+x2+x3......+xh)

如果n+h不是3的倍数,那么无解,如果n+h是3的倍数,

那么,n+h=3*(x1+x2+x3......+xh)有解等价于n+h>=3*(1+2+3+......+h)

问题的完整表述:

输入n,求有多少个正整数h,使得n+h=3k,且k>=1+2+3+......+h

代码:

#include<iostream>
using namespace std;
 
int main()
{
	long long n;
	cin >> n;
	int sum = 0;
	for (long long h = 3 - (n % 3);; h += 3)
	{
		if (n * 2 < h*h * 3 + h)break;
		sum++;
	}
	cout << sum;
	return 0;
}

CodeForces 621B Wet Shark and Bishops

题目:

Description

Today, Wet Shark is given n bishops on a 1000 by 1000 grid. Both rows and columns of the grid are numbered from 1 to 1000. Rows are numbered from top to bottom, while columns are numbered from left to right.

Wet Shark thinks that two bishops attack each other if they share the same diagonal. Note, that this is the only criteria, so two bishops may attack each other (according to Wet Shark) even if there is another bishop located between them. Now Wet Shark wants to count the number of pairs of bishops that attack each other.

Input

The first line of the input contains n (1 ≤ n ≤ 200 000) — the number of bishops.

Each of next n lines contains two space separated integers xi and yi (1 ≤ xi, yi ≤ 1000) — the number of row and the number of column where i-th bishop is positioned. It's guaranteed that no two bishops share the same position.

Output

Output one integer — the number of pairs of bishops which attack each other.

Sample Input

Input

5
1 1
1 5
3 3
5 1
5 5

Output

6
Input

3
1 1
2 3
3 5

Output

0

这个题目是这次比赛最简单的题目。

在1000*1000的棋盘上面,有1999条左斜线,还有1999条右斜线。

输入数据的同时,统计每条斜线上面的象的数量k。

最后统计k*(k-1)/2求和即可。

代码:

#include<iostream>
#include<string.h>
using namespace std;
 
int sumlist[2000];		//x+y-1,1-1999
int diflist[2000];		//x-y+1000,1-1999
 
int main()
{
	int n;
	int x, y;
	memset(sumlist, 0, sizeof(sumlist));
	memset(diflist, 0, sizeof(diflist));
	cin >> n;
	while (n--)
	{
		cin >> x >> y;
		sumlist[x + y - 1]++;
		diflist[x - y + 1000]++;
	}
	int sum = 0;
	for (int i = 1; i < 2000; i++)
	sum += sumlist[i] * (sumlist[i] - 1) / 2 + diflist[i] * (diflist[i] - 1) / 2;
	cout << sum;
	return 0;
}

CodeForces 701B Cells Not Under Attack

题目:

Description

Vasya has the square chessboard of size n × n and m rooks. Initially the chessboard is empty. Vasya will consequently put the rooks on the board one after another.

The cell of the field is under rook's attack, if there is at least one rook located in the same row or in the same column with this cell. If there is a rook located in the cell, this cell is also under attack.

You are given the positions of the board where Vasya will put rooks. For each rook you have to determine the number of cells which arenot under attack after Vasya puts it on the board.

Input

The first line of the input contains two integers n and m (1 ≤ n ≤ 100 000, 1 ≤ m ≤ min(100 000, n2)) — the size of the board and the number of rooks.

Each of the next m lines contains integers xi and yi (1 ≤ xi, yi ≤ n) — the number of the row and the number of the column where Vasya will put the i-th rook. Vasya puts rooks on the board in the order they appear in the input. It is guaranteed that any cell will contain no more than one rook.

Output

Print m integer, the i-th of them should be equal to the number of cells that are not under attack after first i rooks are put.

Sample Input

Input

3 3
1 1
3 1
2 2

Output

4 2 0 

Input

5 2
1 5
5 1

Output

16 9 

Input

100000 1
300 400

Output

9999800001 

代码:

#include<iostream>
#include<string.h>
using namespace std;
 
int listrow[100001];
int listline[100001];
 
int main()
{
	int n, m, x, y;
	long long row, line;
	cin >> n >> m;
	memset(listrow, 0, sizeof(listrow));
	memset(listline, 0, sizeof(listline));
	row = n;
	line = n;
	while (m--)
	{
		cin >> x >> y;
		if (listrow[x] == 0)
		{
			listrow[x] = 1;
			row--;
		}
		if (listline[y] == 0)
		{
			listline[y] = 1;
			line--;
		}
		cout << row*line << " ";
	}
	return 0;
}

HDU 1049 Climbing Worm

题目:

Description

An inch worm is at the bottom of a well n inches deep. It has enough energy to climb u inches every minute, but then has to rest a minute before climbing again. During the rest, it slips down d inches. The process of climbing and resting then repeats. How long before the worm climbs out of the well? We'll always count a portion of a minute as a whole minute and if the worm just reaches the top of the well at the end of its climbing, we'll assume the worm makes it out. 

Input

There will be multiple problem instances. Each line will contain 3 positive integers n, u and d. These give the values mentioned in the paragraph above. Furthermore, you may assume d < u and n < 100. A value of n = 0 indicates end of output. 

Output

Each input instance should generate a single integer on a line, indicating the number of minutes it takes for the worm to climb out of the well. 

Sample Input

10 2 1
20 3 1
0 0 0

Sample Output

17
19

小学二年级的时候做的题目

只不过是口算,不是编程

代码:

#include<iostream>
using namespace std;
 
int main()
{
	int n, u, d, sum;
	while (cin >> n >> u >> d)
	{
		if (n == 0)break;
		n -= u;
		sum = 1;
		if (n > 0)sum += ((n - 1) / (u - d) + 1) * 2;
		cout << sum << endl;
	}
	return 0;
}

HDU 1197 Specialized Four-Digit Numbers

题目:

Description

Find and list all four-digit numbers in decimal notation that have the property that the sum of its four digits equals the sum of its digits when represented in hexadecimal (base 16) notation and also equals the sum of its digits when represented in duodecimal (base 12) notation. 

For example, the number 2991 has the sum of (decimal) digits 2+9+9+1 = 21. Since 2991 = 1*1728 + 8*144 + 9*12 + 3, its duodecimal representation is 1893(12), and these digits also sum up to 21. But in hexadecimal 2991 is BAF16, and 11+10+15 = 36, so 2991 should be rejected by your program. 

The next number (2992), however, has digits that sum to 22 in all three representations (including BB016), so 2992 should be on the listed output. (We don't want decimal numbers with fewer than four digits - excluding leading zeroes - so that 2992 is the first correct answer.) 

Input

There is no input for this problem. 

Output

Your output is to be 2992 and all larger four-digit numbers that satisfy the requirements (in strictly increasing order), each on a separate line with no leading or trailing blanks, ending with a new-line character. There are to be no blank lines in the output. The first few lines of the output are shown below. 

Sample Input

There is no input for this problem.

Sample Output

2992
2993
2994
2995
2996
2997
2998
2999

题意:求所有满足,10进制的各位和、12进制的各位和、16进制的各位和,都相等的四位数。

这个题目,虽然12进制和16进制涉及到了字母,但是我们计算的时候并不需要,和10进制是一样的。

这样,代码就和很简单,只需要从1000到10000扫一遍就可以了。

代码:

#include<iostream>
using namespace std;

bool ok(int n)
{
	int s10 = n % 10 + n / 10 % 10 + n / 100 % 10 + n / 1000;
	int s12 = n % 12 + n / 12 % 12 + n / 144 % 12 + n / 1728;
	int s16 = n % 16 + n / 16 % 16 + n / 256 % 16 + n / 4096;
	if (s10 != s12)return false;
	return s10 == s16;
}

int main()
{
	for (int i = 1000; i < 10000; i++)if (ok(i))cout << i << endl;
	return 0;
}

HDU 1597 find the nth digit

题目:

Description

假设: 
S1 = 1 
S2 = 12 
S3 = 123 
S4 = 1234 
......... 
S9 = 123456789 
S10 = 1234567891 
S11 = 12345678912 
............ 
S18 = 123456789123456789 
.................. 
现在我们把所有的串连接起来 
S = 1121231234.......123456789123456789112345678912......... 
那么你能告诉我在S串中的第N个数字是多少吗? 

Input

输入首先是一个数字K,代表有K次询问。 
接下来的K行每行有一个整数N(1 <= N < 2^31)。

Output

对于每个N,输出S中第N个对应的数字. 

Sample Input

6

1 2 3 4 5 10

Sample Output

1

1

2

1

2

4

这个题目没什么难度,规律很明显,直接数学求解就是的了。

12分钟就AC了,终于有个题目是我最先做出来的了,不容易啊啊啊啊,他们太强了。

代码:

#include<iostream>
#include<math.h>
using namespace std;
 
int main()
{	
	int k;
	cin >> k;
	int n;
	while (k--)
	{
		cin >> n;
		double x = sqrt(n*2.0);
		long long a = int(x) - 1;
		while (a*(a + 1)/2 < n)a++;
		a--;
		long long b = n - a*(a + 1) / 2;
		cout << (b - 1) % 9 + 1 << endl;
	}
	return 0;
}

LightOJ 1265 Island of Survival

题目:

Description

You are in a reality show, and the show is way too real that they threw into an island. Only two kinds of animals are in the island, the tigers and the deer. Though unfortunate but the truth is that, each day exactly two animals meet each other. So, the outcomes are one of the following

a)      If you and a tiger meet, the tiger will surely kill you.

b)      If a tiger and a deer meet, the tiger will eat the deer.

c)      If two deer meet, nothing happens.

d)      If you meet a deer, you may or may not kill the deer (depends on you).

e)      If two tigers meet, they will fight each other till death. So, both will be killed.

If in some day you are sure that you will not be killed, you leave the island immediately and thus win the reality show. And you can assume that two animals in each day are chosen uniformly at random from the set of living creatures in the island (including you).

Now you want to find the expected probability of you winning the game. Since in outcome (d), you can make your own decision, you want to maximize the probability.

Input

Input starts with an integer T (≤ 200), denoting the number of test cases.

Each case starts with a line containing two integers t (0 ≤ t ≤ 1000) and d (0 ≤ d ≤ 1000) where t denotes the number of tigers and d denotes the number of deer.

Output

For each case, print the case number and the expected probability. Errors less than 10-6 will be ignored.

Sample Input

4

0 0

1 7

2 0

0 10

Sample Output

Case 1: 1

Case 2: 0

Case 3: 0.3333333333

Case 4: 1

这个题目其实可以理解为排序。

把所有的动物排序,然后每次取前2个,取出来之后的操作就和题目一样,如果动物没死,放回原处。

下次又取最前面2个动物。。。直到结束。

如果t为奇数,自然是人死。

如果t为偶数,当且仅当所有的老虎都排在人的前面的时候,人活。

这样问题可以进一步抽象,t(偶数)个老虎和1个人,随机排序,求人排在最后的概率

代码:

#include<iostream>
#include<iomanip>
using namespace std;
 
int main()
{
	int T, t, d;
	cin >> T;
	for (int cas = 0; cas < T; cas++)
	{
		cin >> t >> d;
		t++;
		cout << "Case " << cas + 1 << ": " << fixed << setprecision(10) << (t % 2)*1.0 / t << endl;
	}
	return 0;
}

Switch Game HDU - 2053

题目:

There are many lamps in a line. All of them are off at first. A series of operations are carried out on these lamps. On the i-th operation, the lamps whose numbers are the multiple of i change the condition ( on to off and off to on ).

Input

Each test case contains only a number n ( 0< n<= 10^5) in a line. 

Output

Output the condition of the n-th lamp after infinity operations ( 0 - off, 1 - on ).

Sample Input

1
5

Sample Output

1
0

代码:

#include<iostream>
#include<math.h>
using namespace std;
 
int main()
{
	int a;
	while (cin >> a)cout << (int(sqrt(a))*int(sqrt(a)) == a) << endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/nameofcsdn/article/details/112086849