SDUT 2018 Winter Individual Training (for 17) -5(2.21)

电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额。如果购买一个商品之前,卡上的剩余金额大于或等于5元,就一定可以购买成功(即使购买后卡上余额为负),否则无法购买(即使金额足够)。所以大家都希望尽量使卡上的余额最少。 
某天,食堂中有n种菜出售,每种菜可购买一次。已知每种菜的价格以及卡上的余额,问最少可使卡上的余额为多少。 
Input多组数据。对于每组数据: 
第一行为正整数n,表示菜的数量。n<=1000。 
第二行包括n个正整数,表示每种菜的价格。价格不超过50。 
第三行包括一个正整数m,表示卡上的余额。m<=1000。 

n=0表示数据结束。 
Output对于每组输入,输出一行,包含一个整数,表示卡上可能的最小余额。Sample Input
1
50
5
10
1 2 3 2 1 1 2 3 2 1
50
0
Sample Output
-45
32

背包问题中物品的先后顺序是没有影响的,但是这个题不同,比如说我卡上有10元,有两道菜,分别是5元和6元,如果先买6元的就不能卖5元的,而先买5元的能买6元。所以这个问题物品购买的先后顺序是有影响的。其实很显然先买价格小的一定更优。所以先将物品从小到大排序以后,再dp就行了。dp[i][j]表示前i个物品已经选了,剩下的钱为j(包括负数)的情况是否存在,这样转移dp[i][j]=dp[i-1][j]?true:dp[i][j],或dp[i][j]=dp[i-1][j+val[i]]?true:dp[i][j](当然要加上至少需要5元这个限制),其实空间可以省到一维,代码如下:

  1. #include<stdio.h> 
  2. #include<iostream>  
  3. #include<string>  
  4. #include<string.h>  
  5. #include<vector>  
  6. #include<algorithm>  
  7. #include<queue>  
  8. #include<stack>  
  9. #define nn 1100  
  10. #define inff 0x3fffffff  
  11. #define mod 1000000007  
  12. #define eps 1e-9  
  13. using namespace std;  
  14. typedef long long LL;  
  15. int n,m;  
  16. int a[nn];  
  17. bool dp[nn];  
  18. int main()  
  19. {  
  20.     int i,j;  
  21.     while(scanf("%d",&n),n)  
  22.     {  
  23.         for(i=1;i<=n;i++)  
  24.         {  
  25.             scanf("%d",&a[i]);  
  26.         }  
  27.         scanf("%d",&m);  
  28.         sort(a+1,a+n+1);  
  29.         memset(dp,false,sizeof(dp));  
  30.         dp[m+50]=true;  
  31.         for(i=1;i<=n;i++)  
  32.         {  
  33.             for(j=0;j<=m+50;j++)  
  34.             {  
  35.                 if(j+a[i]-50>=5&&j+a[i]<=m+50)  
  36.                     dp[j]=dp[j+a[i]]?true:dp[j];  
  37.             }  
  38.         }  
  39.         for(i=0;i<=m+50;i++)  
  40.             if(dp[i])  
  41.                 break;  
  42.         printf("%d\n",i-50);  
  43.     }  
  44.     return 0;  
  45. }  

Programmer Rostislav got seriously interested in the Link/Cut Tree data structure, which is based on Splay trees. Specifically, he is now studying the exposeprocedure.

Unfortunately, Rostislav is unable to understand the definition of this procedure, so he decided to ask programmer Serezha to help him. Serezha agreed to help if Rostislav solves a simple task (and if he doesn't, then why would he need Splay trees anyway?)

Given integers lr and k, you need to print all powers of number k within range from l to r inclusive. However, Rostislav doesn't want to spent time doing this, as he got interested in playing a network game called Agar with Gleb. Help him!

Input

The first line of the input contains three space-separated integers lr and k (1 ≤ l ≤ r ≤ 10182 ≤ k ≤ 109).

Output

Print all powers of number k, that lie within range from l to r in the increasing order. If there are no such numbers, print "-1" (without the quotes).

Example
Input
1 10 2
Output
1 2 4 8 
Input
2 4 5
Output
-1
Note

Note to the first sample: numbers 20 = 121 = 222 = 423 = 8 lie within the specified range. The number 24 = 16 is greater then 10, thus it shouldn't be printed.

题意:找出k的x次幂在l还有r之间的所有数字

思路:应该是因为指数函数增长比较快吧,所以直接找就没有超时,但是这里必须要用unsigned long long一般的__int64一直没过,并且这里的数字太大,如果乘的次数太多的话可能会超出unsigned long long的范围,所以判断的时候条件设为r/k

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	unsigned long long l,r,k;
	while(scanf("%I64u%I64u%I64u",&l,&r,&k)!=EOF)
	{
		unsigned long long ans=0,temp=1,cout=0;
		bool flag=false;
		while(true)
		{
			if(temp>=l&&temp<=r)
			{
				flag=true;
				if(cout==0)
					printf("%I64u",temp);
				else
					printf(" %I64u",temp);
				cout=1;
			}
			temp*=k;
			if(temp>r/k)//数据比较大,temp可能会出范围,所以加一步判断,防止出界 
			{
				break;
			}
		}
		if(cout)
			printf("\n");
		if(!flag)
			printf("-1\n");
	}
	return 0;
}

One day Vasya was sitting on a not so interesting Maths lesson and making an origami from a rectangular a mm  ×  b mm sheet of paper (a > b). Usually the first step in making an origami is making a square piece of paper from the rectangular sheet by folding the sheet along the bisector of the right angle, and cutting the excess part.

After making a paper ship from the square piece, Vasya looked on the remaining (a - b) mm  ×  b mm strip of paper. He got the idea to use this strip of paper in the same way to make an origami, and then use the remainder (if it exists) and so on. At the moment when he is left with a square piece of paper, he will make the last ship from it and stop.

Can you determine how many ships Vasya will make during the lesson?

Input

The first line of the input contains two integers ab (1 ≤ b < a ≤ 1012) — the sizes of the original sheet of paper.

Output

Print a single integer — the number of ships that Vasya will make.

Example
Input
2 1
Output
2
Input
10 7
Output
6
Input
1000000000000 1
Output
1000000000000
Note

Pictures to the first and second sample test.


题意:一张纸,居短边对折直到最后一块是正方形。问把原先的矩形分成了多少块。
分析:如果x,y是两边,x比y大2倍以上的话就会一直顺着y边对折,几个二倍关系就折几次,找出这层关系即可。


[cpp]  view plain  copy
  1. #include<iostream>  
  2. #include<cstdio>  
  3. #include<cstring>  
  4. #include<ctime>  
  5. #include<algorithm>  
  6. #include <map>  
  7. #include <queue>  
  8. #include <set>  
  9. using namespace std;  
  10.   
  11. //2015.3.22  527A  10的12次幂  
  12. long long int n,m;  
  13. int main()  
  14. {  
  15.     cin>>n>>m;  
  16.     long long int step=0;  
  17.     while(n&&m)  
  18.     {  
  19.         step+=n/m;  
  20.         n%=m;  
  21.         swap(n,m);  
  22.     }  
  23.     cout<<step;  
  24.     return 0;  
  25. }  

According to rules of the Berland fashion, a jacket should be fastened by all the buttons except only one, but not necessarily it should be the last one. Also if the jacket has only one button, it should be fastened, so the jacket will not swinging open.

You are given a jacket with n buttons. Determine if it is fastened in a right way.

Input

The first line contains integer n (1 ≤ n ≤ 1000) — the number of buttons on the jacket.

The second line contains n integers ai (0 ≤ ai ≤ 1). The number ai = 0 if the i-th button is not fastened. Otherwise ai = 1.

Output

In the only line print the word "YES" if the jacket is fastened in a right way. Otherwise print the word "NO".

Example
Input
3
1 0 1
Output
YES
Input
3
1 0 0
Output
NO

时尚的定义是length大于1的要破个洞,一定要破个洞。。 
According to rules of the Berland fashion, a jacket should be fastened by all the buttons except only one

#include <bits/stdc++.h>
using namespace std;

typedef __int64 LL;
const int N=1e3+10;
int a[N];
int n,sum;
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    if(n==1)
    {
        if(a[1]==1)
            puts("YES");
        else
            puts("NO");
        return 0;
    }
    sum=0;
    for(int i=1;i<=n;i++)
        if(!a[i])
            sum++;
    if(sum!=1)
        puts("NO");
    else
        puts("YES");
    return 0;
}

Let's call a string "s-palindrome" if it is symmetric about the middle of the string. For example, the string "oHo" is "s-palindrome", but the string "aa" is not. The string "aa" is not "s-palindrome", because the second half of it is not a mirror reflection of the first half.

English alphabet

You are given a string s. Check if the string is "s-palindrome".

Input

The only line contains the string s (1 ≤ |s| ≤ 1000) which consists of only English letters.

Output

Print "TAK" if the string s is "s-palindrome" and "NIE" otherwise.

Example
Input
oXoxoXo
Output
TAK
Input
bod
Output
TAK
Input
ER
Output
NIE

  1. #include <bits/stdc++.h>  
  2. using namespace std;  
  3. int main()  
  4. {  
  5.     string s;  
  6.     cin>>s;  
  7.     int len=s.size();  
  8.     char c[19][2]={'A','A','b','d','d','b','H','H','I','I','M','M','O','O','o',  
  9.     'o','p','q','q','p','U','U','V','V','v','v','W','W','w','w','X','X','x','x','Y','Y','T','T'};  
  10.     int flag=0;  
  11.     for(int i=0;i<=len/2;i++)  
  12.     {  
  13.         int fg=0;  
  14.         for(int j=0;j<19;j++)  
  15.         {  
  16.             if(s[i]==c[j][0]&&s[len-i-1]==c[j][1])  
  17.             {  
  18.                 fg=1;  
  19.                 break;  
  20.             }  
  21.         }  
  22.         if(fg==0)  
  23.         {  
  24.             puts("NIE");  
  25.             flag=1;  
  26.             break;  
  27.         }  
  28.     }  
  29.     if(flag==0) puts("TAK");  
  30.     return 0;  
  31. }  
岂能尽如人意,但求无愧我心

Find an n × n matrix with different numbers from 1 to n2, so the sum in each row, column and both main diagonals are odd.

Input

The only line contains odd integer n (1 ≤ n ≤ 49).

Output

Print n lines with n integers. All the integers should be different and from 1 to n2. The sum in each row, column and both main diagonals should be odd.

Example
Input
1
Output
1
Input
3
Output
2 1 4
3 5 7
6 9 8

魔方阵的排列规律如下:
(1)将1放在第一行中间一列;
(2)从2开始直到n×n止各数依次按下列规则存放;每一个数存放的行比前一个数的行数减1,列数加1(例如上面的三阶魔方阵,5在4的上一行后一列);
(3)如果上一个数的行数为1,则下一个数的行数为n(指最下一行);例如1在第一行,则2应放在最下一行,列数同样加1;
(4)当上一个数的列数为n时,下一个数的列数应为1,行数减去1。例如2在第3行最后一列,则3应放在第二行第一列;
(5)如果按上面规则确定的位置上已有数,或上一个数是第一行第n列时,则把下一个数放在上一个数的下面。例如按上面的规定,4应该放在第1行第2列,但该位置已经被占据,所以4就放在3的下面;

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include<bits/stdc++.h>
using  namespace  std;
int  a[50][50];
int  i,j,k,p,n;
int  main()
{
     cin>>n;
     for  (i=1; i<=n; i++)
     {
         for  (j=1; j<=n; j++)
         {
             a[i][j]=0;
         }
     }
     p=1;
     j=n/2+1;
     a[1][j]=1;
     for  (k=2; k<=n*n; k++)
     {
         i=i-1;
         j=j+1;
         if  ((i<1)&&(j>n))
         {
             i=i+2;
             j=j-1;
         }
         else
         {
             if (i<1)
             {
                 i=n;
             }
             if (j>n)
             {
                 j=1;
             }
         }
         if (a[i][j]==0)
         {
             a[i][j]=k;
         }
         else
         {
             i=i+2;
             j=j-1;
             a[i][j]=k;
         }
     }
     for (i=1; i<=n; i++)
     {
         for  (j=1; j<=n; j++)
         {
             if (j!=n)
             {
                 printf ( "%d " ,a[i][j]);
             }
             else
             {
                 printf ( "%d" ,a[i][j]);
             }
         }
         printf ( "\n" );
     }
     return  0;
}

猜你喜欢

转载自blog.csdn.net/beposit/article/details/79342014