暑假第三期---思维题3
Posted PrConstantin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了暑假第三期---思维题3相关的知识,希望对你有一定的参考价值。
A - Vanya and Table– CodeForces 552A
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Description
Vanya has a table consisting of 100 rows, each row contains 100 cells. The rows are numbered by integers from 1 to 100 from bottom to top, the columns are numbered from 1 to 100 from left to right.
In this table, Vanya chose n rectangles with sides that go along borders of squares (some rectangles probably occur multiple times). After that for each cell of the table he counted the number of rectangles it belongs to and wrote this number into it. Now he wants to find the sum of values in all cells of the table and as the table is too large, he asks you to help him find the result.
Input
The first line contains integer n (1 ≤ n ≤ 100) — the number of rectangles.
Each of the following n lines contains four integers x1, y1, x2, y2 (1 ≤ x1 ≤ x2 ≤ 100, 1 ≤ y1 ≤ y2 ≤ 100), where x1 and y1 are the number of the column and row of the lower left cell and x2 and y2 are the number of the column and row of the upper right cell of a rectangle.
Output
In a single line print the sum of all values in the cells of the table.
Sample Input
Input
2
1 1 2 3
2 2 3 3
Output
10
Input
2
1 1 3 3
1 1 3 3
Output
18
Hint
Note to the first sample test:
Values of the table in the first three rows and columns will be as follows:
121
121
110
So, the sum of values will be equal to 10.
Note to the second sample test:
Values of the table in the first three rows and columns will be as follows:
222
222
222
So, the sum of values will be equal to 18.
题意:
1-100行从底到上,1-100列从左到右;
选择多个矩形,统计每个矩形在它其中出现的cell数,如第一个样例;
由第一个矩形和第二个矩形的cell数的和得到;
方法:每输入一个就统计一个;
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
ll ans=0;
int main()
int n;
scanf("%d",&n);
for(int q=0;q<n;q++)
int x1,y1,x2,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
for(int i=x1;i<=x2;i++)
for(int j=y1;j<=y2;j++)
ans++;
cout<<ans;
return 0;
B - Vanya and Books—CodeForces 552B
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Description
Vanya got an important task — he should enumerate books in the library and label each book with its number. Each of the n books should be assigned with a number from 1 to n. Naturally, distinct books should be assigned distinct numbers.
Vanya wants to know how many digits he will have to write down as he labels the books.
Input
The first line contains integer n (1 ≤ n ≤ 109) — the number of books in the library.
Output
Print the number of digits needed to number all the books.
Sample Input
Input
13
Output
17
Input
4
Output
4
Hint
Note to the first test. The books get numbers 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, which totals to 17 digits.
Note to the second sample. The books get numbers 1, 2, 3, 4, which totals to 4 digits.
题意:输入一个数,输出从0->这个数的所有位数之和;
方法:按位计算,先是个位,到十位再到百位;
例如数字21:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
在while里第一次加上21,表示所有的个位数;
第二次表示所有的十位数;
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
ll ans=0;
int main()
long long n,s=1;
scanf("%I64d",&n);
while(n-s>=0)
ans+=n-s+1;
s*=10;
cout<<ans;
return 0;
C - Currency System in Geraldion—CodeForces 560A
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Description
A magic island Geraldion, where Gerald lives, has its own currency system. It uses banknotes of several values. But the problem is, the system is not perfect and sometimes it happens that Geraldionians cannot express a certain sum of money with any set of banknotes. Of course, they can use any number of banknotes of each value. Such sum is called unfortunate. Gerald wondered: what is the minimum unfortunate sum?
Input
The first line contains number n (1 ≤ n ≤ 1000) — the number of values of the banknotes that used in Geraldion.
The second line contains n distinct space-separated numbers a1, a2, …, an (1 ≤ ai ≤ 106) — the values of the banknotes.
Output
Print a single line — the minimum unfortunate sum. If there are no unfortunate sums, print - 1.
Sample Input
Input
5
1 2 3 4 5
Output
-1
题意:
一个小岛上有一套自己的货币流通策略,现在给你n种货币面值,问你由任意数量的这些货币不能得到的最小的数是多少;
方法:
有两种情况:
1.货币里有1,那么就可以根据这些货币组成任意的数;
2.货币里没有1,那么这个不能组成的数就是1;
代码如下:
#include <iostream>
#include <cstdio>
using namespace std;
int main()
int n,m;
bool flag=true;
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&m);
if(m==1)
flag=false;
break;
if(flag==false)cout<<-1<<endl;
else cout<<1<<endl;
return 0;
D - Vanya and Scales—CodeForces 552C
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64d
Description
Vanya has a scales for weighing loads and weights of masses w0, w1, w2, …, w100 grams where w is some integer not less than 2 (exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass m using the given weights, if the weights can be put on both pans of the scales. Formally speaking, your task is to determine whether it is possible to place an item of mass m and some weights on the left pan of the scales, and some weights on the right pan of the scales so that the pans of the scales were in balance.
Input
The first line contains two integers w, m (2 ≤ w ≤ 109, 1 ≤ m ≤ 109) — the number defining the masses of the weights and the mass of the item.
Output
Print word ‘YES’ if the item can be weighted and ‘NO’ if it cannot.
Sample Input
Input
3 7
Output
YES
Input
100 99
Output
YES
Input
100 50
Output
NO
Hint
Note to the first sample test. One pan can have an item of mass 7 and a weight of mass 3, and the second pan can have two weights of masses 9 and 1, correspondingly. Then 7 + 3 = 9 + 1.
Note to the second sample test. One pan of the scales can have an item of mass 99 and the weight of mass 1, and the second pan can have the weight of mass 100.
Note to the third sample test. It is impossible to measure the weight of the item in the manner described in the input.
题意:
这个题是天平平衡问题,现有一堆砝码,它们的重量是呈等比数列排列,即w,w^1,w^2,w^3……
现有物品m,砝码随意放,问有没有一种情况使得物品与砝码平衡。
可以根据进制转化的思想,将m变为如下形式:
m=(w-w^i+w^j+w^k+…..w^z)则天平平衡。
那么每次左右同时除以w:
如: m=w(1-w^i-1+w^j-1+w^k-1+….+w^z-1)
即:m/w=-w^i-1+w^j-1+w^k-1+….+w^z-1+1
化为:(m+1)/w可以整除那么上式成立;
又如:m=w(w^i-1+w^j-1+w^k-1+….+w^z-1-1)
即:m/w=w^i-1+w^j-1+w^k-1+….+w^z-1-1
化为:(m-1)/w可以整除则上式成立;
再如:m=w(w^i-1+w^j-1+w^k-1+….+w^z-1)
即:直接被m整除
那么重复上述过程,并更新m的值,则当m==0时可以平衡;
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
int main()
ll w,m;
scanf("%I64d%I64d",&w,&m);
while((m+1)%w==0||(m-1)%w==0||m%w==0)
if((m-1)%w==0)m=(m-1)/w;
else if ((m+1)%w==0)m=(m+1)/w;
else if (m%w==0)m=m/w;
if(m==0)
break;
if(m==0)cout<<"YES"<<endl;
else cout<<"NO"<<endl;
return 0;
注意坑点:
在while里一定把判断,(m-1)%2==0放在第一个位置;
因为如果w是2的话:
当m=1时
首先是判断(m+1)%2==0那么n就会出不来;
仅代表个人观点,欢迎交流探讨!
以上是关于暑假第三期---思维题3的主要内容,如果未能解决你的问题,请参考以下文章