Instruction stringlengths 0 6.61k | Rejected stringlengths 70 490k | Chosen stringlengths 54 490k | Language stringclasses 1
value | Aspect stringclasses 1
value | Source stringclasses 1
value |
|---|---|---|---|---|---|
QQ
==
Write a program which prints multiplication tables in the following format:
```
1x1=1
1x2=2
.
.
9x8=72
9x9=81
```
Input
-----
No input.
Output
------
```
1x1=1
1x2=2
.
.
9x8=72
9x9=81
```
Template for C
--------------
```
#include<stdio.h>
int main(){
return 0;
}
```
Template for C++
----------------
```
#include<iostream>
using namespace std;
int main(){
return 0;
}
```
Template for Java
-----------------
```
class Main{
public static void main(String[] a){
}
}
```
| #include <iostream>
using namespace std;
int main()
{
for(int i=1;i<=9;i++)
{
for(int j=1;j<=9;j++)
{
cout<<i<<"x"<<j<<"="<<(i*j)<<endl;
}
}
return 0;
} | #include<stdio.h>
int main()
{
int i,j,mt=0;
for(i=1;i<10;i++)
{
for(j=1;j<10;j++)
{
mt = i * j;
printf("%dx%d=%d\n",i,j,mt);
}
}
return 0;
} | C++ | Runtime Efficiency | PIE4PERF_HQTRAIN |
Digit Number
============
Write a program which computes the digit number of sum of two integers a and b.
Input
-----
There are several test cases. Each test case consists of two non-negative integers a and *b* which are separeted by a space in a line. The input terminates with EOF.
Constraints
-----------
* 0 ≤ a, b ≤ 1,000,000
* The number of datasets ≤ 200
Output
------
Print the number of digits of a + b for each data set.
Sample Input
------------
```
5 7
1 99
1000 999
```
Output for the Sample Input
---------------------------
```
2
3
4
```
| #import <iostream>
#import <sstream>
using namespace std;
void problemOne(){
int a;
int b;
while (cin >> a >> b){
int sum = a + b;
int intLength = 0;
while (sum != 0) {
sum /= 10;
intLength += 1;
}
cout << intLength << endl;
}
}
int main(){
problemOne();
} | #include<cstdio>
int main(){
int n,n1,n2,count,a;
while(scanf("%d%d",&n1,&n2)!=EOF){
count=0;
a=n1+n2;
while(a!=0){
a=a/10;
count++;
}
printf("%d\n",count);
}
return 0;
}
| C++ | Runtime Efficiency | PIE4PERF_HQTRAIN |
GCD and LCM
===========
Write a program which computes the greatest common divisor (GCD) and the least common multiple (LCM) of given a and b.
Input
-----
Input consists of several data sets. Each data set contains a and b separated by a single space in a line. The input terminates with EOF.
Constraints
-----------
* 0 < a, b ≤ 2,000,000,000
* LCM(a, b) ≤ 2,000,000,000
* The number of data sets ≤ 50
Output
------
For each data set, print GCD and LCM separated by a single space in a line.
Sample Input
------------
```
8 6
50000000 30000000
```
Output for the Sample Input
---------------------------
```
2 24
10000000 150000000
```
| #include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int main()
{
long long int num1 , num2, gcd , res, lcm;
while(scanf("%lld%lld",&num1,&num2)!=EOF){
res = num1 * num2 ;
while(num1 != num2){
if(num1 > num2)
num1 = num1 - num2 ;
else
num2 = num2 - num1 ;
}
gcd = num1 ;
lcm = res / gcd ;
printf("%lld %lld\n", gcd, lcm);
}
return 0;
} | #include<stdio.h>
#define LL long long
LL GCD(LL a,LL b)
{
return b==0?a:GCD(b,a%b);
}
int main()
{
LL a,b;
while(scanf("%lld%lld",&a,&b)!=EOF)
{
LL c=GCD(a,b);
printf("%lld %lld\n",c,a/c*b);
}
return 0;
} | C++ | Runtime Efficiency | PIE4PERF_HQTRAIN |
Prime Number
============
Write a program which reads an integer n and prints the number of prime numbers which are less than or equal to n. A prime number is a natural number which has exactly two distinct natural number divisors: 1 and itself. For example, the first four prime numbers are: 2, 3, 5 and 7.
Input
-----
Input consists of several datasets. Each dataset has an integer n (1 ≤ n ≤ 999,999) in a line.
The number of datasets is less than or equal to 30.
Output
------
For each dataset, prints the number of prime numbers.
Sample Input
------------
```
10
3
11
```
Output for the Sample Input
---------------------------
```
4
2
5
```
| #include<stdio.h>
#include<math.h>
bool flag[1000000];
int main(void)
{
long long i,j,p,n,count;
p = (long long)sqrt(1000000.0);
for(i = 3; i <= p; i = i + 2)
{
if(flag[i] == false)
{
for(j = i * i; j <= 1000000; j = j + 2 * i)
{
flag[j] = true;
}
}
}
while((scanf("%lld",&n)) != EOF)
{
if(n == 1 || n == 0)
count = 0;
else
{
count = 1;
for(i = 3; i <= n; i = i + 2)
{
if(flag[i] == false)
count++;
}
}
printf("%lld\n",count);
}
return 0;
} | #include<cstdio>
using namespace std;
#define MAX 1000000
int prime[MAX];
bool is_prime[MAX];
int sieve(int n){ //埃氏?
int p=0;
for(int i=0;i<=n;i++)
is_prime[i]=true;
is_prime[0]=is_prime[1]=false;
for(int i=2;i<=n;i++){
if(is_prime[i]){
prime[p++]=i; //??素数的个数和素数的?
for(int j=2*i;j<=n;j+=i)
is_prime[j]=false;
}
}
return p;
}
int main()
{
int num;
while(scanf("%d",&num)!=EOF){
printf("%d\n",sieve(num));
}
return 0;
} | C++ | Runtime Efficiency | PIE4PERF_HQTRAIN |
Prime Number
============
Write a program which reads an integer n and prints the number of prime numbers which are less than or equal to n. A prime number is a natural number which has exactly two distinct natural number divisors: 1 and itself. For example, the first four prime numbers are: 2, 3, 5 and 7.
Input
-----
Input consists of several datasets. Each dataset has an integer n (1 ≤ n ≤ 999,999) in a line.
The number of datasets is less than or equal to 30.
Output
------
For each dataset, prints the number of prime numbers.
Sample Input
------------
```
10
3
11
```
Output for the Sample Input
---------------------------
```
4
2
5
```
| #include <cstdio>
using namespace std;
int main() {
long NUM = 1000000;
long n;
long counter = 0;
bool primeNum[NUM + 10];
long i, ii, j, len = NUM + 10;
primeNum[0] = primeNum[1] = false;
for (i = 2; i < len; i++) primeNum[i] = true;
ii = 4;
for(i = 2; ii < NUM;) {
if (primeNum[i]) {
for (j = ii; j < NUM; j += i) {
primeNum[j] = false;
}
}
i++;
ii = i * i;
}
while (scanf("%ld", &n) != EOF) {
counter = 0;
for (i = 0; i <= n; i++) {
if (primeNum[i]) counter++;
}
printf("%ld\n", counter);
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
static const long MAX = 1000000L;
int main() {
vector<long> prime;
vector<long> dp;
bool isPrime;
long n, m;
long i, len = sqrt(MAX) + 1;
long j, lenJ;
long k;
prime.push_back(2);
dp.push_back(0);
dp.push_back(0);
dp.push_back(1);
for (i = 3; i <= len; i += 2) {
isPrime = true;
lenJ = prime.size();
k = sqrt(i) + 1;
for (j = 0; j < lenJ; j++) {
if (k <= prime[j]) break;
if (i % prime[j] == 0) {
isPrime = false;
break;
}
}
if (isPrime) prime.push_back(i);
}
lenJ = prime.size();
while (cin >> n) {
if (n < dp.size()) {
cout << dp[n] << endl;
continue;
}
m = dp.size();
for (i = m; i <= n; i += 2) {
isPrime = true;
k = sqrt(i) + 1;
for (j = 0; j < lenJ; j++) {
if (k <= prime[j]) break;
if (i % prime[j] == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
dp.push_back(dp.back() + 1);
dp.push_back(dp.back());
} else {
dp.push_back(dp.back());
dp.push_back(dp.back());
}
}
cout << dp[n] << endl;
}
return 0;
} | C++ | Runtime Efficiency | PIE4PERF_HQTRAIN |
Prime Number
============
Write a program which reads an integer n and prints the number of prime numbers which are less than or equal to n. A prime number is a natural number which has exactly two distinct natural number divisors: 1 and itself. For example, the first four prime numbers are: 2, 3, 5 and 7.
Input
-----
Input consists of several datasets. Each dataset has an integer n (1 ≤ n ≤ 999,999) in a line.
The number of datasets is less than or equal to 30.
Output
------
For each dataset, prints the number of prime numbers.
Sample Input
------------
```
10
3
11
```
Output for the Sample Input
---------------------------
```
4
2
5
```
| #include <iostream>
using namespace std;
int main() {
int judge[1000000];
for(int k=0;k<1000000;k++)judge[k]=1;
int n,sum;
while(cin>>n){
if(n==0||n==1){
sum = 0;
}else{
sum = 1;
for(int i=3;i<n+1;i+=2){
if (judge[i]==1){
sum++;
for(int j=2;j<=n/i;j++)
judge[i*j] = 0;
}
}
}
cout << sum <<endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
vector<int> ns;
int temp, max_n = 0;
while(cin >> temp){
ns.push_back(temp);
max_n = max(temp, max_n);
}
max_n++;
vector<bool> is_prime(max_n, true);
vector<int> primes;
for(ll i=2;i<max_n;i++){
if(is_prime[i]){
primes.push_back(i);
for(ll j=i*i;j<max_n;j+=i){
is_prime[j] = false;
}
}
}
for(int n: ns){
int ans = upper_bound(primes.begin(), primes.end(), n) - primes.begin();
cout << ans << endl;
}
return 0;
} | C++ | Runtime Efficiency | PIE4PERF_HQTRAIN |
Prime Number
============
Write a program which reads an integer n and prints the number of prime numbers which are less than or equal to n. A prime number is a natural number which has exactly two distinct natural number divisors: 1 and itself. For example, the first four prime numbers are: 2, 3, 5 and 7.
Input
-----
Input consists of several datasets. Each dataset has an integer n (1 ≤ n ≤ 999,999) in a line.
The number of datasets is less than or equal to 30.
Output
------
For each dataset, prints the number of prime numbers.
Sample Input
------------
```
10
3
11
```
Output for the Sample Input
---------------------------
```
4
2
5
```
| #include<iostream>
using namespace std;
int main(){
int n, cnt;
while(cin >> n){
bool prime[1000000] = {false};
prime[1] = true; //1???????????\
// ??¨??????????????????????????????
for(int j = 2; j*j <= n; j++){
if(prime[j] == false){
for(int k = j; k*j <= n; k++){
prime[j*k] =true;
}
}
}
cnt = 0;
for(int i = 1; i <= n; i++){
if(prime[i] == false) cnt++;
}
cout << cnt << endl;
}
} | #include<iostream>
using namespace std;
int main(){
int n, cnt;
while(cin >> n){
bool prime[n+1] = {false};
prime[1] = true;
// ??¨??????????????????????????????
for(int j = 2; j*j <= n; j++){
if(prime[j] == false){
for(int k = j; k*j <= n; k++){
prime[j*k] = true;
}
}
}
cnt = 0;
for(int i = 1; i <= n; i++){
if(prime[i] == false) cnt++;
}
cout << cnt << endl;
}
} | C++ | Runtime Efficiency | PIE4PERF_HQTRAIN |
Maximum Sum Sequence
====================
Given a sequence of numbers a1, a2, a3, ..., an, find the maximum sum of a contiguous subsequence of those numbers. Note that, a subsequence of one element is also a *contiquous* subsequence.
Input
-----
The input consists of multiple datasets. Each data set consists of:
```
n
a1
a2
.
.
an
```
You can assume that 1 ≤ n ≤ 5000 and -100000 ≤ ai ≤ 100000.
The input end with a line consisting of a single 0.
Output
------
For each dataset, print the maximum sum in a line.
Sample Input
------------
```
7
-5
-1
6
4
9
-6
-7
13
1
2
3
2
-2
-1
1
2
3
2
1
-2
1
3
1000
-200
201
0
```
Output for the Sample Input
---------------------------
```
19
14
1001
```
| #include<iostream>
#include<algorithm>
using namespace std;
int main(){
int n;
int a[5000];
int tmp;
int ans;
while(1){
cin >> n;
if(!n)break;
for(int i=0;i<n;i++){
cin >> a[i];
}
ans = a[0];
for(int i=0;i<n;i++){
for(int j=i;j<n;j++){
if(i==j)tmp = a[i];
else tmp += a[j];
ans = max(tmp,ans);
}
}
cout << ans << endl;
}
} | #include<cstdio>
#include<algorithm>
using namespace std;
int main(){
int n,tmp,ans;
int a[5000];
while(scanf("%d",&n) && n){
for(int i=0;i<n;i++)scanf("%d",&a[i]);
ans = a[0];
for(int i=0;i<n;i++)
for(int j=i;j<n;j++){
if(i==j)tmp = a[i];
else tmp += a[j];
ans = max(tmp,ans);
}
printf("%d\n",ans);
}
} | C++ | Runtime Efficiency | PIE4PERF_HQTRAIN |
Maximum Sum Sequence
====================
Given a sequence of numbers a1, a2, a3, ..., an, find the maximum sum of a contiguous subsequence of those numbers. Note that, a subsequence of one element is also a *contiquous* subsequence.
Input
-----
The input consists of multiple datasets. Each data set consists of:
```
n
a1
a2
.
.
an
```
You can assume that 1 ≤ n ≤ 5000 and -100000 ≤ ai ≤ 100000.
The input end with a line consisting of a single 0.
Output
------
For each dataset, print the maximum sum in a line.
Sample Input
------------
```
7
-5
-1
6
4
9
-6
-7
13
1
2
3
2
-2
-1
1
2
3
2
1
-2
1
3
1000
-200
201
0
```
Output for the Sample Input
---------------------------
```
19
14
1001
```
| #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
vector<int> v,s(10001);
int a,b,n,o=0;
while(cin>>b){
fill(s.begin(),s.end(),0);
o =0;
if(b==0) break;
else{
for(int i=0;i<b;i++){
cin>>a;
v.push_back(a);
}
for(int i = 0; i < b; i++){
int sum = 0;
for(int j = i; j < b; j++){
sum += v[j];
s.push_back(sum);
}
}
int d = *max_element(s.begin(),s.end());
cout<<d<<endl;
}
s.clear(),v.clear();
}
} | #include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <complex>
#include <ctime>
#include <cstdlib>
using namespace std;
inline int to_int(string s) {int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string to_str(T x) {ostringstream sout; sout << x; return sout.str();}
typedef long long ll;
int main()
{
int n, T[5001], dp[5001];
while(cin >> n, n)
{
for(int i = 0; i < n; i++)
{
cin >> T[i];
}
memset(dp, -1, sizeof(dp));
dp[0] = T[0];
for(int i = 1; i < n; i++)
{
dp[i] = max(dp[i-1] + T[i], T[i]);
}
int res = -10000000;
for(int i = 0; i < n; i++)
{
res = max(res, dp[i]);
}
cout << res << endl;
}
} | C++ | Runtime Efficiency | PIE4PERF_HQTRAIN |
玉
=

図のように二股に分かれている容器があります。1 から 10 までの番号が付けられた10 個の玉を容器の開口部 A から落とし、左の筒 B か右の筒 C に玉を入れます。板 D は支点 E を中心に左右に回転できるので、板 D を動かすことで筒 B と筒 C のどちらに入れるか決めることができます。
開口部 A から落とす玉の並びを与えます。それらを順番に筒 B 又は筒 Cに入れていきます。このとき、筒 B と筒 C のおのおのが両方とも番号の小さい玉の上に大きい玉を並べられる場合は YES、並べられない場合は NO と出力するプログラムを作成してください。ただし、容器の中で玉の順序を入れ替えることはできないものとします。また、続けて同じ筒に入れることができるものとし、筒 B, C ともに 10 個の玉がすべて入るだけの余裕があるものとします。
Input
-----
複数のデータセットが与えられます。1行目にデータセット数 N が与えられます。つづいて、N 行のデータセットが与えられます。各データセットに 10 個の番号が左から順番に空白区切りで与えられます。
Output
------
各データセットに対して、YES または NO を1行に出力して下さい。
Sample Input
------------
```
2
3 1 4 2 5 6 7 8 9 10
10 9 8 7 6 5 4 3 2 1
```
Output for the Sample Input
---------------------------
```
YES
NO
```
| #include<bits/stdc++.h>
using namespace std;
int a[20];
bool check(int x)
{
int pre = 0;
for (int i = 0; i < 10; i++) if (x & (1 << i))
{
if (a[i] < pre) return false;
pre = a[i];
}
return true;
}
bool dfs(int i, int x, int pre)
{
if (i == 10)
{
return check(x);
}
bool temp = false;
if (a[i] > pre) temp = dfs(i + 1, x, a[i]);
return temp || dfs(i + 1, x | (1 << i), pre);
}
int main()
{
int T;
cin >> T;
while (T--)
{
for (int i = 0; i < 10; i++) scanf("%d", &a[i]);
printf("%s\n", dfs(1, 0, a[0]) ? "YES" : "NO");
}
return 0;
} | #include<cstdio>
int a,l1,l2,n;
bool f;
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
f=0;l1=0;l2=-1;
for(int j=1;j<=10;j++)
{
scanf("%d",&a);
if(!f&&a<l2&&a<l1){printf("NO\n");f=1;continue;}
if(!f&&l1>l2)
{
if(l1<a)l1=a;
else l2=a;
}
if(!f&&l2>l1)
{
if(l2<a)l2=a;
else l1=a;
}
}
if(!f)printf("YES\n");
}
} | C++ | Runtime Efficiency | PIE4PERF_HQTRAIN |
アフィン暗号
======
簡単な暗号法の一つに、アフィン暗号というものがあります。まず、アルファベット a〜z を a = 0, b = 1, c = 2,..., x = 23, y = 24, z = 25 と 0〜25 の数字に置き換えます。そして、以下の式で、原文のアルファベットを置換します。
$F(\gamma) = (\alpha \cdot \gamma + \beta)$ mod $26$
ただし、mod 26 は 26 で割った余りを表します。例えば、$\alpha = 3, \beta = 2$ のとき、アルファベットの 'a' (=0) は、$F(0) = (3 \cdot 0 + 2)$ mod $26 = 2$ で 'c' に、アルファベットの 'n' (=13) は $F(13) = (3 \cdot 13 + 2)$ mod $26 = 15$ で 'p' に置換されます。
このとき、$\gamma$ に対する $F(\gamma)$ が必ず 1 対 1 で対応付けられるように、$\alpha$ と $\beta$ は慎重に選ばれているものとします($\alpha$ と 26 が互いに素であることが条件)。$\alpha = 4, \beta = 7$ のときのように、$F('a') = 7, F('n') = 7$ と、'a' も 'n' も同じ 'h' に置換されるようなことはありません。また、アルファベット以外の文字は置換されません。
暗号化された文字列を元の文章に復号したものを出力するプログラムを作成してください。元の文章には、キーワードとして
```
that
this
```
のいずれかが必ず含まれているものとします。
Input
-----
複数のデータセットが与えられます。1行目にデータセット数 $n$ ($n \leq 30$) が与えられます。続いて $n$ 行のデータが与えられます。各データセットに英小文字と空白からなる 256 文字以内の暗号化された文章が1行に与えられます。
Output
------
各データセットに対して、復号した元の文章を1行に出力して下さい。
Sample Input
------------
```
1
y eazqyp pnop pngtg ye obmpngt xmybp mr lygw
```
Output for the Sample Input
---------------------------
```
i submit that there is another point of view
```
| #include <iostream>
#include <map>
#include <sstream>
#include <string.h>
using namespace std;
map<int, char> AlphabetTable;
map<int, int> table;
char decode(char a){
int z = a - 'a';
for(int i = 0; i < 26; i++){
if(table[i] == z) return AlphabetTable[i];
}
}
void makeTable(int a, int b){
for(int i = 0; i < 26; i++){
table[i] = (a*i+b) % 26;
}
}
void makeAlphabetTable(){
char c = 'a';
for(int i = 0; i < 26; i++){
AlphabetTable[i] = c;
c++;
}
}
int main(){
string code, tmp;
int n;
getline (cin, code);
stringstream ssn(code);
ssn >> n;
makeAlphabetTable();
for(int i = 0; i < n; i++){
getline(cin, code );
tmp = code;
for(int b = 0; b < 26; b++){
for(int a = 1; a < 26; a+=2){
if(a%13 != 0){
makeTable(a, b);
code = tmp;
for(int j = 0; j < code.length(); j++){
if('a' <= code[j] && code[j] <= 'z'){
code[j] = decode(code[j]);
}
}
if(code.find("this") != -1 || code.find("that")!= -1){
cout << code << endl;
break;
}
}
}
}
}
return 0;
} | #include <iostream>
#include <string>
#include <vector>
using namespace std;
void solve()
{
int N;
scanf("%d\n", &N);
while(N--)
{
string s;
getline(cin, s);
string sbuf = s;
sbuf += ' ';
int n = sbuf.size();
int start = 0;
int end = 0;
vector<string> Vec;
string temp;
for(int i = 0; i < n; ++i)
{
if(sbuf[i] == ' ')
{
if(end - start == 4)
{
Vec.push_back(temp);
}
start = end = i + 1;
temp.clear();
continue;
}
temp += sbuf[i];
++end;
}
int a = 0;
int b = 0;
for(int i = 0; i < Vec.size(); ++i)
{
for(int j = 0; j < 4; ++j)
{
Vec[i][j] %= 26;
}
}
for(int i = 0; i < 26; ++i)
{
for(int j = 0; j < 27; ++j)
{
for(int k = 0; k < Vec.size(); ++k)
{
string check = "asdf";
for(int l = 0; l < 4; ++l)
{
check[l] = (i * Vec[k][l] + j) % 26 + 'a';
}
if(check == "this" || check == "that")
{
a = i;
b = j;
goto END;
}
}
}
}
END:
n = s.size();
for(int i = 0; i < n; ++i)
{
if(s[i] == ' ')
{
cout << s[i];
continue;
}
s[i] = (a * (s[i] % 26) + b) % 26 + 'a';
cout << s[i];
}
cout << endl;
}
}
int main()
{
solve();
return(0);
} | C++ | Runtime Efficiency | PIE4PERF_HQTRAIN |
アフィン暗号
======
簡単な暗号法の一つに、アフィン暗号というものがあります。まず、アルファベット a〜z を a = 0, b = 1, c = 2,..., x = 23, y = 24, z = 25 と 0〜25 の数字に置き換えます。そして、以下の式で、原文のアルファベットを置換します。
$F(\gamma) = (\alpha \cdot \gamma + \beta)$ mod $26$
ただし、mod 26 は 26 で割った余りを表します。例えば、$\alpha = 3, \beta = 2$ のとき、アルファベットの 'a' (=0) は、$F(0) = (3 \cdot 0 + 2)$ mod $26 = 2$ で 'c' に、アルファベットの 'n' (=13) は $F(13) = (3 \cdot 13 + 2)$ mod $26 = 15$ で 'p' に置換されます。
このとき、$\gamma$ に対する $F(\gamma)$ が必ず 1 対 1 で対応付けられるように、$\alpha$ と $\beta$ は慎重に選ばれているものとします($\alpha$ と 26 が互いに素であることが条件)。$\alpha = 4, \beta = 7$ のときのように、$F('a') = 7, F('n') = 7$ と、'a' も 'n' も同じ 'h' に置換されるようなことはありません。また、アルファベット以外の文字は置換されません。
暗号化された文字列を元の文章に復号したものを出力するプログラムを作成してください。元の文章には、キーワードとして
```
that
this
```
のいずれかが必ず含まれているものとします。
Input
-----
複数のデータセットが与えられます。1行目にデータセット数 $n$ ($n \leq 30$) が与えられます。続いて $n$ 行のデータが与えられます。各データセットに英小文字と空白からなる 256 文字以内の暗号化された文章が1行に与えられます。
Output
------
各データセットに対して、復号した元の文章を1行に出力して下さい。
Sample Input
------------
```
1
y eazqyp pnop pngtg ye obmpngt xmybp mr lygw
```
Output for the Sample Input
---------------------------
```
i submit that there is another point of view
```
| #include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#include <set>
#include <map>
using namespace std;
class AffineCipher
{
public:
string decode (string, vector <int> );
vector<int> make_alpha (void);
private:
int gcd (int, int );
bool decide_variable (int, int, string );
bool check_code (string );
};
int AffineCipher::gcd (int a, int b )
{
if (a > b){
swap(a,b);
} // end if
while (b % a != 0 ){
b = b % a;
if (a > b ){
swap(a, b );
} // end if
} // end while
return a;
}
bool AffineCipher::decide_variable (int a, int b, string str )
{
set <char> code;
set <char> source;
int i;
for (i = 0; i < str.length(); ++i ){
if (isalpha(str[i]) ){
source.insert (str[i] );
} // end if
} // end for
set <char>::iterator it = source.begin();
for (; it != source.end(); ++it ){
char c = (a*((*it) - 'a' ) + b ) % 26 + 'a';
if (code.count (c) ){
return false;
}else{
code.insert (c);
} // end if
} // end for
return true;
}
bool AffineCipher::check_code (string str )
{
const string keyword[] = { "this", "that" };
for (int i = 0; i < sizeof(keyword)/sizeof(keyword[0]); ++i){
if (str.find(keyword[i] ) != string::npos ){
return true;
} // end if
} // end for
return false;
}
string AffineCipher::decode (string str, vector <int> alpha )
{
int len = str.length();
int a, b;
multimap <int, int> table;
int i;
for (i = 0; i < alpha.size(); ++i){
a = alpha[i];
for (b = 0; b < 26; ++b){
if (!decide_variable (a, b, str ) ){
continue;
}else{
table.insert (make_pair (a, b ) );
} // end if
} // end for
} // end for
string res = "";
multimap <int, int>::iterator it = table.begin();
for (; it != table.end(); ++it){
a = (*it).first;
b = (*it).second;
res = "";
for (i = 0; i < len; ++i){
char c = 0;
if (isalpha(str[i] ) ){
c = (a*(str[i] - 'a' ) + b ) % 26 + 'a';
}else{
c = str[i];
} // end if
res += c;
} // end for
if (!check_code (res ) ){
continue;
}else{
break;
} // end if
} // end for
return res;
}
vector <int> AffineCipher::make_alpha (void)
{
vector <int> alpha;
int i;
for (i = 1; i < 26; ++i){
if (gcd(i, 26 ) == 1 ){
alpha.push_back(i);
} // end if
} // end for
return alpha;
}
int main()
{
AffineCipher AC;
// cut here before submit
// freopen("testcase.ac", "r", stdin);
string str = "";
int n;
getline (cin, str );
stringstream ssn(str);
ssn >> n;
vector <int> alpha = AC.make_alpha();
int i;
for (i = 0; i < n; ++i){
string res = "";
getline (cin, str );
res = AC.decode (str, alpha );
cout << res << endl;
} // end for
return 0;
} | #include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cstdlib> // require abs exit atof atoi
#include <cstdio> // require scanf printf
#include <functional>
#include <numeric> // require accumulate
#include <cmath> // require fabs
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip> // require setw
#include <sstream> // require stringstream
#include <cstring> // require memset
#include <cctype> // require tolower, toupper
#include <fstream> // require freopen
#include <ctime> // require srand
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const string keyword[] = {"that", "this" };
int affine (int a, int b, int x )
{
return ((a*x + b ) % 26 );
}
int main()
{
// cut here before submit
// freopen ("testcase.AC", "r", stdin );
int n;
string sn = "";
getline (cin, sn );
stringstream ssn (sn );
ssn >> n;
// scanf ("%d", &n );
rep (i, n ){
string str = "";
getline (cin, str );
int m = str.length();
string res = "";
for (int a = 1; a <= 26; a++ ){
if (__gcd (a, 26 ) != 1 ) continue;
for (int b = 0; b <= 26; b++ ){
string s = str;
rep (i, m ){
if (isalpha(s[i] ) ){
char c = (char)(affine (a, b, (int)(s[i] - 'a') ) + 'a' );
if (isalpha (c ) ){
s[i] = c;
} // end if
} // end if
} // end rep
// cerr << "s: " << s << endl;
rep (i, 2 ){
if (s.find (keyword[i] ) != string::npos ){
res = s;
break;
} // end if
} // end rep
if (!res.empty() ) break;
} // end for
if (!res.empty() ) break;
} // end for
cout << res << endl;
} // end loop
return 0;
} | C++ | Runtime Efficiency | PIE4PERF_HQTRAIN |
アフィン暗号
======
簡単な暗号法の一つに、アフィン暗号というものがあります。まず、アルファベット a〜z を a = 0, b = 1, c = 2,..., x = 23, y = 24, z = 25 と 0〜25 の数字に置き換えます。そして、以下の式で、原文のアルファベットを置換します。
$F(\gamma) = (\alpha \cdot \gamma + \beta)$ mod $26$
ただし、mod 26 は 26 で割った余りを表します。例えば、$\alpha = 3, \beta = 2$ のとき、アルファベットの 'a' (=0) は、$F(0) = (3 \cdot 0 + 2)$ mod $26 = 2$ で 'c' に、アルファベットの 'n' (=13) は $F(13) = (3 \cdot 13 + 2)$ mod $26 = 15$ で 'p' に置換されます。
このとき、$\gamma$ に対する $F(\gamma)$ が必ず 1 対 1 で対応付けられるように、$\alpha$ と $\beta$ は慎重に選ばれているものとします($\alpha$ と 26 が互いに素であることが条件)。$\alpha = 4, \beta = 7$ のときのように、$F('a') = 7, F('n') = 7$ と、'a' も 'n' も同じ 'h' に置換されるようなことはありません。また、アルファベット以外の文字は置換されません。
暗号化された文字列を元の文章に復号したものを出力するプログラムを作成してください。元の文章には、キーワードとして
```
that
this
```
のいずれかが必ず含まれているものとします。
Input
-----
複数のデータセットが与えられます。1行目にデータセット数 $n$ ($n \leq 30$) が与えられます。続いて $n$ 行のデータが与えられます。各データセットに英小文字と空白からなる 256 文字以内の暗号化された文章が1行に与えられます。
Output
------
各データセットに対して、復号した元の文章を1行に出力して下さい。
Sample Input
------------
```
1
y eazqyp pnop pngtg ye obmpngt xmybp mr lygw
```
Output for the Sample Input
---------------------------
```
i submit that there is another point of view
```
| #include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<vector>
#include<cmath>
#include<cstdio>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define it ::iterator
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double ESP=1e-10;
using namespace std;
int main(){
int n;
cin>>n;
rep(i,n){
string s;
string ans;
bool han=false;
if(i==0)getline(cin,s);
getline(cin,s);
rep(a,27){
rep(b,27){
string tmp=s;
rep(j,tmp.size())if(isalpha(tmp[j]))tmp[j]=(a*(tmp[j]-'a')+b)%26+'a';
rep(j,tmp.size()-3){
if(tmp.substr(j,4)=="this"){ans=tmp;han=true;}
if(tmp.substr(j,4)=="that"){ans=tmp;han=true;}
}
if(han==true)break;
}
if(han==true)break;
}
cout<<ans<<endl;
}
} | #include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<vector>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<time.h>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double EPS=1e-10;
const int inf=1e8;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
int main(){
int n;
cin>>n;
string s;
getline(cin,s);
while(n--){
getline(cin,s);
bool h=false;
rep(i,26){
string tmp;
rep(j,26){
tmp=s;
rep(k,tmp.size())if(isalpha(tmp[k]))tmp[k]=(i*(tmp[k]-'a')+j)%26+'a';
rep(k,tmp.size()-3){
string a=tmp.substr(k,4);
if(a=="this"||a=="that"){h=true;break;}
}
if(h)break;
}
if(h){s=tmp;break;}
}
cout<<s<<endl;;
}
} | C++ | Runtime Efficiency | PIE4PERF_HQTRAIN |
式
=
与えられた 4 つの 1 から 9 の整数を使って、答えが 10 になる式をつくります。
4 つの整数 a, b, c, d を入力したとき、下記の条件に従い、答えが 10 になる式を出力するプログラムを作成してください。また、答えが複数ある時は、最初に見つかった答えだけを出力するものとします。答えがない時は、0 と出力してください。
* 演算子として、加算 (+)、減算 (-)、乗算 (\*) だけを使います。除算 (/) は使いません。使用できる演算子は3個です。
* 数を4つとも使わなければいけません。
* 4つの数の順番は自由に入れ換えてかまいません。
* カッコを使ってもかまいません。使用できるカッコは3組(6個)以下です。
Input
-----
複数のデータセットが与えられます。各データセットの形式は以下のとおり:
```
a b c d
```
入力は4つの 0 で終了します。データセットの数は 40 を超えません。
Output
------
各データセットについて、与えられた 4 つの整数と上記の演算記号およびカッコを組み合わせて値が 10 となる式または 0 を1行に出力してください。式の文字列が 1024 文字を超えてはいけません。
Sample Input
------------
```
8 7 9 9
4 4 4 4
5 5 7 5
0 0 0 0
```
Output for the Sample Input
---------------------------
```
((9 * (9 - 7)) - 8)
0
((7 * 5) - (5 * 5))
```
| #include <cstdio>
using namespace std;
int a[4];
int c[8];
int k;
int calc(){
int op = c[k++];
int ret = 0;
if(op >= 0){
ret = op;
}
else if(op >= -3){
int x = calc();
int y = calc();
if(op == -1){
ret = x + y;
}
else if(op == -2){
ret = x - y;
}
else{
ret = x * y;
}
}
else{
throw 0;
}
return ret;
}
void print(){
int op = c[k++];
if(op >= 0){
printf("%d", op);
}
else{
char ch;
if(op == -1){ ch = '+'; }
else if(op == -2){ ch = '-'; }
else{ ch = '*'; }
putchar('(');
print();
printf(" %c ", ch);
print();
putchar(')');
}
}
void solve(int i, int S, int oprem){
if(i == 7){
try{
if(S == 0){
k = 0;
if(calc() == 10 && k == 7){
k = 0;
print();
puts("");
throw "";
}
}
}catch(int){}
return;
}
if(oprem > 0){
for(int op = -1; op >= -3; --op){
c[i] = op;
solve(i + 1, S, oprem - 1);
}
}
for(int j = 0; j < 4; ++j){
if(S >> j & 1){
c[i] = a[j];
solve(i + 1, S ^ 1 << j, oprem);
}
}
}
int main(){
while(1){
for(int i = 0; i < 8; ++i){
c[i] = -9;
}
for(int i = 0; i < 4; ++i){
scanf("%d", &a[i]);
}
if(a[0] == 0){ break; }
try{
solve(0, 15, 3);
puts("0");
}catch(...){}
}
} | #include <cstdio>
using namespace std;
int a[4];
int c[8];
int k;
int calc(){
int op = c[k++];
int ret = 0;
if(op >= 0){
ret = op;
}
else if(op >= -3){
int x = calc();
int y = calc();
if(op == -1){
ret = x + y;
}
else if(op == -2){
ret = x - y;
}
else{
ret = x * y;
}
}
else{
throw 0;
}
return ret;
}
void print(){
int op = c[k++];
if(op >= 0){
printf("%d", op);
}
else{
char ch;
if(op == -1){ ch = '+'; }
else if(op == -2){ ch = '-'; }
else{ ch = '*'; }
putchar('(');
print();
printf(" %c ", ch);
print();
putchar(')');
}
}
void solve(int i, int S, int oprem, int numrem){
if(i == 6){
for(int j = 0; j < 4; ++j){
if(S >> j & 1){
c[6] = a[j];
}
}
try{
k = 0;
if(calc() == 10 && k == 7){
k = 0;
print();
puts("");
throw "";
}
}catch(int){}
return;
}
if(oprem > 0){
for(int op = -1; op >= -3; --op){
c[i] = op;
solve(i + 1, S, oprem - 1, numrem + 1);
}
}
if(numrem > 0){
for(int j = 0; j < 4; ++j){
if(S >> j & 1){
c[i] = a[j];
solve(i + 1, S ^ 1 << j, oprem, numrem - 1);
}
}
}
}
int main(){
while(1){
for(int i = 0; i < 8; ++i){
c[i] = -9;
}
for(int i = 0; i < 4; ++i){
scanf("%d", &a[i]);
}
if(a[0] == 0){ break; }
try{
solve(0, 15, 3, 0);
puts("0");
}catch(...){}
}
} | C++ | Runtime Efficiency | PIE4PERF_HQTRAIN |
泥棒
==
宝物がたくさん収蔵されている博物館に、泥棒が大きな風呂敷を一つだけ持って忍び込みました。盗み出したいものはたくさんありますが、風呂敷が耐えられる重さが限られており、これを超えると風呂敷が破れてしまいます。そこで泥棒は、用意した風呂敷を破らず且つ最も価値が高くなるようなお宝の組み合わせを考えなくてはなりません。
風呂敷が耐えられる重さ W、および博物館にある個々のお宝の価値と重さを読み込んで、重さの総和が W を超えない範囲で価値の総和が最大になるときの、お宝の価値総和と重さの総和を出力するプログラムを作成してください。ただし、価値の総和が最大になる組み合わせが複数あるときは、重さの総和が小さいものを出力することとします。
Input
-----
複数のデータセットが与えられます。各データセットは以下のような形式で与えられます。
```
W
N
v1,w1
v2,w2
:
vN,wN
```
1行目に風呂敷の耐えられる重さを表す整数 W (W ≤ 1,000)、2行目にお宝の数 N (1 ≤ N ≤ 1,000) が与えられます。続く N 行に i 番目のお宝の価値を表す整数 vi (0 ≤ vi ≤ 10,000) とその重さを表す整数 wi (0 ≤ wi ≤ W) の組がそれぞれ1行に与えられます。
W が 0 のとき入力の最後とします。データセットの数は 50 を超えません。
Output
------
各データセットに対して以下のように出力して下さい。
```
Case データセットの番号:
風呂敷に入れたお宝の価値総和
そのときのお宝の重さの総和
```
Sample Input
------------
```
50
5
60,10
100,20
120,30
210,45
10,4
50
5
60,10
100,20
120,30
210,45
10,4
0
```
Output for the Sample Input
---------------------------
```
Case 1:
220
49
Case 2:
220
49
```
| #include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <sstream>
#include <vector>
#include <string>
#include <iomanip>
#include <bitset>
#define INF 100000000
#define pb push_back
#define fi first
#define sec second
#define SS stringstream
using namespace std;
typedef pair<int, int> P;
typedef long long int ll;
int w, n;
int t = 0;
int dp[1001][1001];
vector<P> vec;
int rec(int i, int j){
if(dp[i][j] >= 0) return dp[i][j];
int res;
if(i == n) res = 0;
else if(j < vec[i].sec) res = rec(i+1, j);
else res = max(rec(i+1, j), rec(i+1, j-vec[i].sec)+vec[i].fi);
return dp[i][j] = res;
}
int main(){
while(scanf("%d", &w)){
t++;
if(w == 0) break;
memset(dp, -1, sizeof(dp));
scanf("%d", &n);
for(int i = 0; i < n; i++){
int temp1, temp2;
char trash;
scanf("%d%c%d", &temp1, &trash, &temp2);
vec.pb(P(temp1, temp2));
}
printf("Case %d:\n", t);
printf("%d\n", rec(0, w));
for(int i = 1; i <= w; i++){
if(rec(0, w-i) != rec(0,w)){
printf("%d\n", w-i+1);
break;
}
}
vec.clear();
}
} | #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
const double EPS = 1e-10;
const ll INF = 100000000;
const ll MOD = 1000000007;
struct ITEM {
int v, w;
double x;
bool operator>(const ITEM& right) const {
return x > right.x;
}
} item[1000];
int N, W;
int v[1000], w[1000];
int ans;
int ans2;
void dfs (int pos, int val, int sum, bool flag) {
if (ans < val) {
ans = val;
ans2 = sum;
}
if (ans == val) {
ans2 = min(ans2, sum);
}
if (pos == N) return;
if (flag) {
int _W = W-sum;
double lim = val;
for (int i = pos; i < N; i++) {
if (_W >= item[i].w) {
lim += item[i].v;
_W -= item[i].w;
} else {
lim += _W * item[i].x;
break;
}
}
if (lim+EPS < ans) {
return;
}
}
if (sum+item[pos].w <= W) {
dfs(pos+1, val+item[pos].v, sum+item[pos].w, false);
}
dfs(pos+1, val, sum, true);
}
int main() {
int testcase = 1;
while (cin >> W) {
if (W == 0) break;
ans = 0;
cin >> N;
rep(i,N) {
char tmp;
cin >> item[i].v >> tmp >> item[i].w;
item[i].x = item[i].v / (double)item[i].w;
}
sort(item, item+N, greater<ITEM>());
dfs(0, 0, 0, false);
cout << "Case " << testcase << ":" << endl;
cout << ans << endl;
cout << ans2 << endl;
testcase++;
}
}
| C++ | Runtime Efficiency | PIE4PERF_HQTRAIN |
泥棒
==
宝物がたくさん収蔵されている博物館に、泥棒が大きな風呂敷を一つだけ持って忍び込みました。盗み出したいものはたくさんありますが、風呂敷が耐えられる重さが限られており、これを超えると風呂敷が破れてしまいます。そこで泥棒は、用意した風呂敷を破らず且つ最も価値が高くなるようなお宝の組み合わせを考えなくてはなりません。
風呂敷が耐えられる重さ W、および博物館にある個々のお宝の価値と重さを読み込んで、重さの総和が W を超えない範囲で価値の総和が最大になるときの、お宝の価値総和と重さの総和を出力するプログラムを作成してください。ただし、価値の総和が最大になる組み合わせが複数あるときは、重さの総和が小さいものを出力することとします。
Input
-----
複数のデータセットが与えられます。各データセットは以下のような形式で与えられます。
```
W
N
v1,w1
v2,w2
:
vN,wN
```
1行目に風呂敷の耐えられる重さを表す整数 W (W ≤ 1,000)、2行目にお宝の数 N (1 ≤ N ≤ 1,000) が与えられます。続く N 行に i 番目のお宝の価値を表す整数 vi (0 ≤ vi ≤ 10,000) とその重さを表す整数 wi (0 ≤ wi ≤ W) の組がそれぞれ1行に与えられます。
W が 0 のとき入力の最後とします。データセットの数は 50 を超えません。
Output
------
各データセットに対して以下のように出力して下さい。
```
Case データセットの番号:
風呂敷に入れたお宝の価値総和
そのときのお宝の重さの総和
```
Sample Input
------------
```
50
5
60,10
100,20
120,30
210,45
10,4
50
5
60,10
100,20
120,30
210,45
10,4
0
```
Output for the Sample Input
---------------------------
```
Case 1:
220
49
Case 2:
220
49
```
| #include <iostream>
#include <cstdio>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
int main(){
int weight;
int n,v,w,c=0;
int dp[1001][1001];
while(scanf("%d\n",&weight) && weight){
c++;
rep(i,1001)rep(j,1001)dp[i][j]=0;
scanf("%d\n",&n);
for(int k=1;k<=n;k++){
scanf("%d,%d\n",&v,&w);
for(int i=0;i<=w;i++){
dp[k][i] = dp[k-1][i];
}
for(int i=w;i<=weight;i++){
dp[k][i] = max(dp[k-1][i],dp[k-1][i-w]+v);
}
}
/*for(int k=0;k<=n;k++){
for(int i=0;i<=weight;i++)
printf("%4d",dp[k][i]);
puts("");
}*/
printf("Case %d:\n",c);
printf("%d\n",dp[n][weight]);
for(int i=0;i<=weight;i++){
if(dp[n][i]==dp[n][weight]){
printf("%d\n",i);
break;
}
}
}
} | #include <bits/stdc++.h>
using namespace std;
bool cmp(const pair<int,int> &a,const pair<int,int> &b){
if( a.first != b.first ) return a.first < b.first;
else return a.second > b.second;
}
int main(){
int n,W;
int t = 1;
while( cin >> W >> n && W ){
vector< pair<int,int> > cur;
cur.push_back({0,0});
for(int i = 0 ; i < n ; i++){
int w,c;
scanf("%d,%d",&c,&w);
for(int j = 0 , t = cur.size() ; j < t ; j++ )
cur.push_back(pair<int,int>(cur[j].first+w,cur[j].second+c));
sort(cur.begin(),cur.end(),cmp);
int maxi = -1;
vector< pair<int,int> > nex;
for(int j = 0 ; j < cur.size() ; j++){
if( maxi >= cur[j].second ) continue;
if( cur[j].first <= W ) nex.push_back(cur[j]);
maxi = cur[j].second;
}cur = nex;
}
int ans = 0, ans2 ;
for(int i = 0 ; i < cur.size() ; i++){
if( cur[i].first <= W ){
ans = cur[i].second;
ans2 = cur[i].first;
}
}
cout << "Case " << t++ << ":" << endl;
cout << ans << endl;
cout << ans2 << endl;
}
} | C++ | Runtime Efficiency | PIE4PERF_HQTRAIN |
泥棒
==
宝物がたくさん収蔵されている博物館に、泥棒が大きな風呂敷を一つだけ持って忍び込みました。盗み出したいものはたくさんありますが、風呂敷が耐えられる重さが限られており、これを超えると風呂敷が破れてしまいます。そこで泥棒は、用意した風呂敷を破らず且つ最も価値が高くなるようなお宝の組み合わせを考えなくてはなりません。
風呂敷が耐えられる重さ W、および博物館にある個々のお宝の価値と重さを読み込んで、重さの総和が W を超えない範囲で価値の総和が最大になるときの、お宝の価値総和と重さの総和を出力するプログラムを作成してください。ただし、価値の総和が最大になる組み合わせが複数あるときは、重さの総和が小さいものを出力することとします。
Input
-----
複数のデータセットが与えられます。各データセットは以下のような形式で与えられます。
```
W
N
v1,w1
v2,w2
:
vN,wN
```
1行目に風呂敷の耐えられる重さを表す整数 W (W ≤ 1,000)、2行目にお宝の数 N (1 ≤ N ≤ 1,000) が与えられます。続く N 行に i 番目のお宝の価値を表す整数 vi (0 ≤ vi ≤ 10,000) とその重さを表す整数 wi (0 ≤ wi ≤ W) の組がそれぞれ1行に与えられます。
W が 0 のとき入力の最後とします。データセットの数は 50 を超えません。
Output
------
各データセットに対して以下のように出力して下さい。
```
Case データセットの番号:
風呂敷に入れたお宝の価値総和
そのときのお宝の重さの総和
```
Sample Input
------------
```
50
5
60,10
100,20
120,30
210,45
10,4
50
5
60,10
100,20
120,30
210,45
10,4
0
```
Output for the Sample Input
---------------------------
```
Case 1:
220
49
Case 2:
220
49
```
| #include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int main(){
for(int c=1,w; cin>>w,w; c++){
int n,tr[1001][2] = {0};
cin >>n;
for(int i=1; i<=n; i++){
scanf("%d,%d",&tr[i][0],&tr[i][1]);
}
int dp[1001][1001] = {0};
for(int i=1; i<=n; i++){
for(int j=0; j<=w; j++){
if(j>=tr[i][1]){dp[j][i] = max(dp[j][i-1],dp[j-tr[i][1]][i-1]+tr[i][0]);}
else{dp[j][i] = dp[j][i-1];}
}
}
int pr = dp[w][n], val;
for(int j=0; j<=w; j++){
if(pr == dp[j][n]){val = j;break;}
}
cout <<"Case "<<c<<":"<<endl;
cout <<pr<<endl<<val<<endl;
}
return 0;
} | #include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int main(){
int w,n,tre[1001][2],dp[1001][1001] = {0},x;
for(int c = 1; cin >>w,w; c++){
cin >>n;
for(int i=1; i<=n; i++){
scanf("%d,%d",&tre[i][0],&tre[i][1]);
}
for(int i=1; i<=n; i++){
for(int j=1; j<=w; j++){
dp[i][j] = dp[i-1][j];
if(j>=tre[i][1]){dp[i][j] = max(dp[i][j],dp[i-1][j-tre[i][1]]+tre[i][0]);}
}
}
for(int j=0; j<=w; j++){
if(dp[n][j] == dp[n][w]){x = j;break;}
}
cout <<"Case "<<c<<":"<<endl<<dp[n][w]<<endl<<x<<endl;
}
return 0;
} | C++ | Runtime Efficiency | PIE4PERF_HQTRAIN |
泥棒
==
宝物がたくさん収蔵されている博物館に、泥棒が大きな風呂敷を一つだけ持って忍び込みました。盗み出したいものはたくさんありますが、風呂敷が耐えられる重さが限られており、これを超えると風呂敷が破れてしまいます。そこで泥棒は、用意した風呂敷を破らず且つ最も価値が高くなるようなお宝の組み合わせを考えなくてはなりません。
風呂敷が耐えられる重さ W、および博物館にある個々のお宝の価値と重さを読み込んで、重さの総和が W を超えない範囲で価値の総和が最大になるときの、お宝の価値総和と重さの総和を出力するプログラムを作成してください。ただし、価値の総和が最大になる組み合わせが複数あるときは、重さの総和が小さいものを出力することとします。
Input
-----
複数のデータセットが与えられます。各データセットは以下のような形式で与えられます。
```
W
N
v1,w1
v2,w2
:
vN,wN
```
1行目に風呂敷の耐えられる重さを表す整数 W (W ≤ 1,000)、2行目にお宝の数 N (1 ≤ N ≤ 1,000) が与えられます。続く N 行に i 番目のお宝の価値を表す整数 vi (0 ≤ vi ≤ 10,000) とその重さを表す整数 wi (0 ≤ wi ≤ W) の組がそれぞれ1行に与えられます。
W が 0 のとき入力の最後とします。データセットの数は 50 を超えません。
Output
------
各データセットに対して以下のように出力して下さい。
```
Case データセットの番号:
風呂敷に入れたお宝の価値総和
そのときのお宝の重さの総和
```
Sample Input
------------
```
50
5
60,10
100,20
120,30
210,45
10,4
50
5
60,10
100,20
120,30
210,45
10,4
0
```
Output for the Sample Input
---------------------------
```
Case 1:
220
49
Case 2:
220
49
```
|
// dpの練習
// dpでなく、愚直に組んだプログラミング
// ナップサック問題とほぼ同じ
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int wei; // 持てる最大の重さ
int n; // お宝の数
int v[1001]; // お宝の価値
int w[1001]; // お宝の重さ
// グローバル配列は0で初期化されている
int dp[1001][1001]; //メモテーブル
int max_value(int i, int j) {
int value;
if( dp[i][j] >= 0 ){
return dp[i][j];
//すでに1回は調べている
}
if (i == n) {
// 品物がもう残っていないときは、価値の和の最大値は0で確定
//もうお宝がないつまり、価値は0
value = 0;
} else if (j < w[i]) {
//風呂敷が持てる重さを超えてしまう
// 残りの容量が足りず品物iを入れられないので、入れないパターンだけ処理
// i+1 以降の品物のみを使ったときの最大値をそのままこの場合の最大値にする
value = max_value(i + 1, j);
} else {
//左がお宝を入れない、右がお宝を入れた場合
// 品物iを入れるか入れないか選べるので、両方試して価値の和が大きい方を選ぶ
value = max( max_value(i + 1, j), max_value(i + 1, j - w[i]) + v[i] );
}
return dp[i][j] = value;
//メモに記憶させる
}
void max_weight( int ans ){
int w_min;
for (int i = wei -1 ; i >= 0 ; i--){
if( ans != max_value( 0 , i ) ){
w_min = i + 1;
break;
}
}
cout << w_min << endl;
}
int main(void){
int cnt = 1;
while( cin >> wei ){
memset( dp , -1 , sizeof(dp) );
//まだ調べてないところを-1で初期化
if( wei == 0 )break;
cin >> n;
char ten;
for( int i = 0 ; i < n ; i++ ){
cin >> v[i] >> ten >> w[i];
}
cout << "Case " << cnt << ":" << endl;
int ans = max_value( 0 , wei );
cout << ans << endl;
max_weight(ans);
cnt++;
}
return 0;
} | #include <iostream>
#include <cstring>
#include <algorithm>
#define MAX 1000
using namespace std;
int main(void){
int n,W;
int w[MAX+1];
int v[MAX+1];
int dp[MAX+1][MAX+1];
char ten;
int cnt = 1;
while( cin >> W , W ){
cin >> n;
for( int i = 0 ; i < n ; i++ ){
cin >> v[i] >> ten >> w[i];
}
for( int i = 0 ; i < MAX ; i++ ){
for( int j = 0 ; j < MAX ; j++ ){
dp[i][j] = 0;
}
}
for( int i = 0 ; i < n ; i++ ){
for( int j = 0 ; j <= W ; j++ ){
if(j < w[i] ) dp[i+1][j] = dp[i][j];
else dp[i+1][j] = max( dp[i][j] , dp[i][j-w[i]] + v[i] );
}
}
cout << "Case " << cnt++ << ":" << endl;
for( int i = W ; i >= 0 ; i-- ){
if( dp[n][i] != dp[n][W] ){
cout << dp[n][W] << endl << i+1 << endl;
break;
}
}
}
return 0;
} | C++ | Runtime Efficiency | PIE4PERF_HQTRAIN |
島の数
===
地勢を示す縦 12, 横 12 のマスからなる平面図があります。おのおののマスは白か黒に塗られています。白は海を、黒は陸地を表します。二つの黒いマスが上下、あるいは左右に接しているとき、これらは地続きであるといいます。この平面図では、黒いマス一つのみ、あるいは地続きの黒いマスが作る領域を「島」といいます。例えば下図には、5 つの島があります。
```
■■■■□□□□■■■■
■■■□□□□□■■■■
■■□□□□□□■■■■
■□□□□□□□■■■■
□□□■□□□■□□□□
□□□□□□■■■□□□
□□□□□■■■■■□□
■□□□■■■■■■■□
■■□□□■■■■■□□
■■■□□□■■■□□□
■■■■□□□■□□□□
□□□□□□□□□□□□
```
マスのデータを読み込んで、島の数を出力するプログラムを作成してください。
Input
-----
入力は複数のデータセットからなります。各データセットに1つの平面図が与えられます。黒いマスを 1、白いマスを 0 で表現した 12 個の数字の列 12 行でひとつの平面図を表します。データセットの間は1つの空行で区切られています。
データセットの数は 20 を超えません。
Output
------
データセットごとに、島の数を1行に出力します。
Sample Input
------------
```
111100001111
111000001111
110000001111
100000001111
000100010000
000000111000
000001111100
100011111110
110001111100
111000111000
111100010000
000000000000
010001111100
110010000010
010010000001
010000000001
010000000110
010000111000
010000000100
010000000010
010000000001
010010000001
010010000010
111001111100
000000000000
111111111111
100010100001
100010100001
100010100001
100010100001
100100100101
101000011101
100000000001
100000000001
111111111111
100000000001
```
Output for the Sample Input
---------------------------
```
5
13
4
```
Hint
----
以下はサンプルインプットを■と□で表したものです。
```
■■■■□□□□■■■■ □■□□□■■■■■□□ □□□□□□□□□□□□
■■■□□□□□■■■■ ■■□□■□□□□□■□ ■■■■■■■■■■■■
■■□□□□□□■■■■ □■□□■□□□□□□■ ■□□□■□■□□□□■
■□□□□□□□■■■■ □■□□□□□□□□□■ ■□□□■□■□□□□■
□□□■□□□■□□□□ □■□□□□□□□■■□ ■□□□■□■□□□□■
□□□□□□■■■□□□ □■□□□□■■■□□□ ■□□□■□■□□□□■
□□□□□■■■■■□□ □■□□□□□□□■□□ ■□□■□□■□□■□■
■□□□■■■■■■■□ □■□□□□□□□□■□ ■□■□□□□■■■□■
■■□□□■■■■■□□ □■□□□□□□□□□■ ■□□□□□□□□□□■
■■■□□□■■■□□□ □■□□■□□□□□□■ ■□□□□□□□□□□■
■■■■□□□■□□□□ □■□□■□□□□□■□ ■■■■■■■■■■■■
□□□□□□□□□□□□ ■■■□□■■■■■□□ ■□□□□□□□□□□■
```
| #include<cstdio>
#include<iostream>
#include<vector>
#include<string>
#include<deque>
#include<map>
#include<algorithm>
using namespace std;
int masu[14][14];
int cont;
void saiki(int x,int y){
masu[x][y]=0;
if(masu[x+1][y]==1)
saiki(x+1,y);
if(masu[x][y+1]==1)
saiki(x,y+1);
if(masu[x-1][y]==1)
saiki(x,y-1);
if(masu[x][y-1]==1)
saiki(x,y-1);
}
int main(){
while(1){
for(int i=0;i<14;i++){
for(int j=0;j<14;j++){
masu[i][j]=0;
}
}
for(int i=1;i<=12;i++){
char st[13];
if(scanf("%s",st)==EOF){
goto A;
}
for(int j=0;j<12;j++){
if(st[j]=='0'){
masu[i][j+1]=0;
}else{
masu[i][j+1]=1;
}
}
}
cont=0;
for(int i=1;i<=12;i++){
for(int j=1;j<=12;j++){
if(masu[i][j]==1){
cont++;
saiki(i,j);
}
}
}
printf("%d\n",cont);
}
A:;
} | #include<cstdio>
#include<algorithm>
using namespace std;
int masu[14][14];
int cont;
void saiki(int x,int y){
masu[x][y]=0;
if(masu[x+1][y]==1){
saiki(x+1,y);
}
if(masu[x-1][y]==1){
saiki(x-1,y);
}
if(masu[x][y+1]==1){
saiki(x,y+1);
}
if(masu[x][y-1]==1){
saiki(x,y-1);
}
}
int main()
{
while(1){
for(int i=0;i<14;i++){
for(int j=0;j<14;j++)
masu[i][j]=0;
}
cont=0;
char str[100];
for(int i=1;i<=12;i++){
if(scanf("%s",str)==EOF){
goto A;
}
for(int j=1;j<=12;j++){
if(str[j-1]=='1'){
masu[i][j]=1;
}
}
}
for(int i=1;i<=12;i++){
for(int j=1;j<=12;j++){
if(masu[i][j]==1){
saiki(i,j);
cont++;
}
}
}
printf("%d\n",cont);
}
A:;
} | C++ | Runtime Efficiency | PIE4PERF_HQTRAIN |
Combination of Number Sequences
===============================
0 から 9 までの整数を使った n 個の数の並び k1, k2, ..., kn を考えます。正の整数 n と s を読み込んで、
k1 + 2 × k2 + 3 × k3 + ... + n × kn = s
となっているような n 個の数の並びが何通りあるかを出力するプログラムを作成してください。ただし、1 つの「n 個の数の並び」には同じ数が 2 回以上現われないものとします。
Input
-----
入力は複数のデータセットからなります。各データセットとして、n (1 ≤ n ≤ 10) と s (0 ≤ s ≤ 10,000)が空白区切りで1行に与えられます。
データセットの数は 100 を超えません。
Output
------
データセットごとに、n 個の整数の和が s になる組み合わせの個数を1行に出力します。
Sample Input
------------
```
3 10
3 1
```
Output for the Sample Input
---------------------------
```
8
0
```
| #include<cstdio>
using namespace std;
int n,s;
int memo[1024][1000];
int search(int stat,int sum,int dep){
if(dep==n) return memo[stat][sum]=(s==sum);
if(~memo[stat][sum]) return memo[stat][sum];
// if(sum>s) return 0;
int cnt=0;
for(int i=0;i<10;i++){
if((stat&(1<<i))==0){
stat|=1<<i;
cnt+=search(stat,sum+i*(n-dep),dep+1);
stat&=~(1<<i);
}
}
return memo[stat][sum]=cnt;
}
int main(){
for(;~scanf("%d%d",&n,&s);){
for(int i=0;i<1024;i++)for(int j=0;j<1000;j++) memo[i][j]=-1;
if(n>10 || s>=1000){
puts("0");
continue;
}
printf("%d\n",search(0,0,0));
}
return 0;
} | #include<cstdio>
using namespace std;
int n,s,memo[1024][500];
int search(int stat,int sum,int dep){
if(dep==n) return memo[stat][sum]=(s==sum);
if(~memo[stat][sum]) return memo[stat][sum];
if(sum>s) return 0;
int cnt=0;
for(int i=0;i<10;i++){
if((stat&(1<<i))==0){
stat|=1<<i;
cnt+=search(stat,sum+i*(n-dep),dep+1);
stat&=~(1<<i);
}
}
return memo[stat][sum]=cnt;
}
int main(){
for(;~scanf("%d%d",&n,&s);){
for(int i=0;i<1024;i++)for(int j=0;j<500;j++) memo[i][j]=-1;
if(n>10 || s>=500){ puts("0"); continue; }
printf("%d\n",search(0,0,0));
}
return 0;
} | C++ | Runtime Efficiency | PIE4PERF_HQTRAIN |
3乗根
===
$x^3 = q$ の解は漸化式 $x\_{n+1} = x\_n - \frac{x\_{n}^3 - q}{3x\_{n}^2}$ を計算していくことで近似的に求めることができます。
$x\_1$ に正の数 $\frac{q}{2}$ をいれ
$x\_2 = x\_1 - \frac{x\_{1}^3 - q}{3x\_{1}^2}$、$x\_3 = x\_2 - \frac{x\_{2}^3 - q}{3x\_{2}^2}$、… と計算します。
この計算をしながら、
$|x^3 - q|$ の値が、十分小さくなったところで、計算をやめ、最後に計算した $x\_n$ を $x^3 = q$ の近似解とします。
この方法に従って、入力された正の整数 $q$ に対し、 $q$ の3乗根の近似値を出力するプログラムを作成してください。ただし、「十分小さくなった」という判定は $|x^3 - q| < 0.00001 q$ を用いてください。
入力
--
複数のデータセットが与えられる。各データセットに $q$ ($1 \leq q < 2^{31}$)(整数)が一行に与えられる。入力の終わりは -1 である。
データセットの数は 50 を超えない。
出力
--
各データセットに対して $x$ (実数)を1行に出力する。出力結果に 0.00001 以下の誤差を含んでもよい。
Sample Input
------------
```
15
15
-1
```
Output for the Sample Input
---------------------------
```
2.466212
2.466212
```
| #include <iostream>
#include <algorithm>
#include <cmath>
#include <iomanip>
using namespace std;
bool eq(double x,double y){
if(abs(x-y)<0.00001*y)return true;
else return false;
}
int main(void){
long long q;
double x[100], x1;
while(cin >> q){
if(q == -1) break;
x[1] = q/2.0;
for(int i = 2; ; i++){
x[i] = x[i-1]-(x[i-1]*x[i-1]*x[i-1]-(double)q)/(3*x[i-1]*x[i-1]);
if(eq(x[i]*x[i]*x[i],(double)q)){
x1 = x[i];
break;
}
}
cout <<fixed;
cout << setprecision(7) << x1 << endl;
}
return 0;
}
| #include <stdio.h>
#include <math.h>
int main(void){
double q;
while(scanf("%lf", &q)!= EOF && q != -1) {
double x, a;
x=q/2;
while(1){
if(fabs(x*x*x-q)<0.00001*q) break;
x=x-((x*x*x-q)/(3*x*x));
x = x;
}
printf("%.7f\n",x);
}
return 0;
}
| C++ | Runtime Efficiency | PIE4PERF_HQTRAIN |
正方形探索
=====
縦に n 行、横に n 列並べられた、合計 n × n のマス目があります。いくつかのマス目には印がついています。各マス目の印の状態を読み込み、印のついていないマス目だけからなる最大の正方形の辺の長さを出力として表示するプログラムを作成してください。
たとえば各データセットで以下のようなデータが与えられます。
```
10
...*....**
..........
**....**..
........*.
..*.......
..........
.*........
..........
....*..***
.*....*...
```
入力データの一行が、一行のマス目を表現します。入力データの文字列のうち、.(ピリオド)は印のついていないマス目、\*(アスタリスク)は印のついているマス目を示しています。
上記の例では、下図の 0 で示される正方形が最大となります。
```
...*....**
..........
**....**..
...00000*.
..*00000..
...00000..
.*.00000..
...00000..
....*..***
.*....*...
```
よって、5 と出力すれば正解になります。
なお、すべてのマス目に印がついている場合には、0 を出力してください。
Input
-----
上記形式で複数のデータセットが与えられます。
n が 0 のとき入力の最後とします。n は 1000 以下とします。入力データの文字列には、ピリオド、アスタリスク、改行以外の文字は含まれません。データセットの数は 50 を超えません。
Output
------
各データセットに対し、最大の正方形の辺の長さ(整数)を1行に出力して下さい。
Sample Input
------------
```
10
...*....**
..........
**....**..
........*.
..*.......
..........
.*........
..........
....*..***
.*....*...
10
****.*****
*..*.*....
****.*....
*....*....
*....*****
..........
****.*****
*..*...*..
****...*..
*..*...*..
0
```
Output for the Sample Input
---------------------------
```
5
3
```
| #include<cstdio>
#include<iostream>
#define INF 2100000000
using namespace std;
int minix(int a,int b,int c){
if(b>=a && c>=a){
return a;
}
if(a>=b && c>=b){
return b;
}
if(b>=c && a>=c){
return c;
}
}
int main(){
int n;
int masu[1002][1002];
int ura[1002][1002];
char str[1002];
while(1){
for(int i=0;i<=1001;i++){
for(int j=0;j<=1001;j++){
masu[i][j]=0;
}
}
scanf("%d",&n);
if(n==0){
break;
}
for(int i=1;i<=n;i++){
scanf("%s",str);
for(int j=1;j<=n;j++){
if(str[j-1]=='.'){
ura[i][j]=0;
}else{
ura[i][j]=1;
}
}
}
int maxi=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(ura[i][j]==0){
masu[i][j]=minix(masu[i-1][j],masu[i][j-1],masu[i-1][j-1])+1;
maxi=max(masu[i][j],maxi);
}
}
}
printf("%d\n",maxi);
}
} | #include<cstdio>
#include<iostream>
#define INF 2100000000
using namespace std;
int minix(int a,int b,int c){
if(b>=a && c>=a){
return a;
}
if(a>=b && c>=b){
return b;
}
if(b>=c && a>=c){
return c;
}
}
int main(){
int n;
int masu[1002][1002];
int ura[1002][1002];
char str[1002];
while(1){
scanf("%d",&n);
if(n==0){
break;
}
for(int i=1;i<=n;i++){
scanf("%s",str);
for(int j=1;j<=n;j++){
if(str[j-1]=='.'){
ura[i][j]=0;
}else{
ura[i][j]=1;
}
}
}
for(int i=0;i<=n;i++){
masu[i][0]=masu[0][i]=0;
}
int maxi=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(ura[i][j]==0){
masu[i][j]=minix(masu[i-1][j],masu[i][j-1],masu[i-1][j-1])+1;
maxi=max(masu[i][j],maxi);
}else{
masu[i][j]=0;
}
}
}
printf("%d\n",maxi);
}
} | C++ | Runtime Efficiency | PIE4PERF_HQTRAIN |
正方形探索
=====
縦に n 行、横に n 列並べられた、合計 n × n のマス目があります。いくつかのマス目には印がついています。各マス目の印の状態を読み込み、印のついていないマス目だけからなる最大の正方形の辺の長さを出力として表示するプログラムを作成してください。
たとえば各データセットで以下のようなデータが与えられます。
```
10
...*....**
..........
**....**..
........*.
..*.......
..........
.*........
..........
....*..***
.*....*...
```
入力データの一行が、一行のマス目を表現します。入力データの文字列のうち、.(ピリオド)は印のついていないマス目、\*(アスタリスク)は印のついているマス目を示しています。
上記の例では、下図の 0 で示される正方形が最大となります。
```
...*....**
..........
**....**..
...00000*.
..*00000..
...00000..
.*.00000..
...00000..
....*..***
.*....*...
```
よって、5 と出力すれば正解になります。
なお、すべてのマス目に印がついている場合には、0 を出力してください。
Input
-----
上記形式で複数のデータセットが与えられます。
n が 0 のとき入力の最後とします。n は 1000 以下とします。入力データの文字列には、ピリオド、アスタリスク、改行以外の文字は含まれません。データセットの数は 50 を超えません。
Output
------
各データセットに対し、最大の正方形の辺の長さ(整数)を1行に出力して下さい。
Sample Input
------------
```
10
...*....**
..........
**....**..
........*.
..*.......
..........
.*........
..........
....*..***
.*....*...
10
****.*****
*..*.*....
****.*....
*....*....
*....*****
..........
****.*****
*..*...*..
****...*..
*..*...*..
0
```
Output for the Sample Input
---------------------------
```
5
3
```
| #include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
int n;
int s[1001][1024];
int calc(int y,int x,int n){
int a,b,c,d;
n--;
a=s[y-1][x-1];
b=s[y-1][x+n];
c=s[y+n][x-1];
d=s[y+n][x+n];
return d-c-b+a;
}
int main(){
while(cin>>n,n){
memset(s,0,sizeof(s));
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
char c;
cin>>c;
s[i][j] = (c=='*')+s[i-1][j]+s[i][j-1]-s[i-1][j-1];
}
}
int ans=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
int lb=0,ub=n-max(i,j)+2;
while(ub-lb>1){
int m=(lb+ub)/2;
if(calc(i,j,m))ub=m;
else lb=m;
}
ans=max(ans,lb);
}
}
cout<<ans<<endl;
}
return 0;
} | #include<iostream>
#include<algorithm>
using namespace std;
int n;
int s[1001][1024];
int calc(int y,int x,int n){
int a,b,c,d;
n--;
a=s[y-1][x-1];
b=s[y-1][x+n];
c=s[y+n][x-1];
d=s[y+n][x+n];
return d-c-b+a;
}
int main(){
while(cin>>n,n){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
char c;
cin>>c;
s[i][j] = (c=='*')+s[i-1][j]+s[i][j-1]-s[i-1][j-1];
}
}
int ans=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
int lb=ans,ub=n-max(i,j)+2;
if(ub+1<=ans)continue;
while(ub-lb>1){
int m=(lb+ub)/2;
if(calc(i,j,m))ub=m;
else lb=m;
}
ans=max(ans,lb);
}
}
cout<<ans<<endl;
}
return 0;
} | C++ | Runtime Efficiency | PIE4PERF_HQTRAIN |
正方形探索
=====
縦に n 行、横に n 列並べられた、合計 n × n のマス目があります。いくつかのマス目には印がついています。各マス目の印の状態を読み込み、印のついていないマス目だけからなる最大の正方形の辺の長さを出力として表示するプログラムを作成してください。
たとえば各データセットで以下のようなデータが与えられます。
```
10
...*....**
..........
**....**..
........*.
..*.......
..........
.*........
..........
....*..***
.*....*...
```
入力データの一行が、一行のマス目を表現します。入力データの文字列のうち、.(ピリオド)は印のついていないマス目、\*(アスタリスク)は印のついているマス目を示しています。
上記の例では、下図の 0 で示される正方形が最大となります。
```
...*....**
..........
**....**..
...00000*.
..*00000..
...00000..
.*.00000..
...00000..
....*..***
.*....*...
```
よって、5 と出力すれば正解になります。
なお、すべてのマス目に印がついている場合には、0 を出力してください。
Input
-----
上記形式で複数のデータセットが与えられます。
n が 0 のとき入力の最後とします。n は 1000 以下とします。入力データの文字列には、ピリオド、アスタリスク、改行以外の文字は含まれません。データセットの数は 50 を超えません。
Output
------
各データセットに対し、最大の正方形の辺の長さ(整数)を1行に出力して下さい。
Sample Input
------------
```
10
...*....**
..........
**....**..
........*.
..*.......
..........
.*........
..........
....*..***
.*....*...
10
****.*****
*..*.*....
****.*....
*....*....
*....*****
..........
****.*****
*..*...*..
****...*..
*..*...*..
0
```
Output for the Sample Input
---------------------------
```
5
3
```
| #include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
int dp[1001][1001];
int latte(int i,int j,int k){
return dp[i+k][j+k]-dp[i+k][j-1]-dp[i-1][j+k]+dp[i-1][j-1];
}
int main(){
int n;
while(cin>>n,n){
fill(dp[0],dp[0]+1001*1001,0);
for(int i=0;i<n;i++){
string str;
cin>>str;
for(int j=0;j<n;j++){
dp[i+1][j+1]=dp[i][j+1]+dp[i+1][j]-dp[i][j];
if(str[j]=='*')dp[i+1][j+1]++;
}
}
int Max=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(latte(i,j,0))continue;
int lb=0,ub=min(n-i,n-j)+1;
while(ub-lb>1){
int k=(ub+lb)/2;
//if(i+k>n||j+k>n)ub=k;
if(latte(i,j,k))ub=k;
else lb=k;
}
Max=max(Max,lb+1);
}
}
cout<<Max<<endl;
}
} | #include<bits/stdc++.h>
using namespace std;
int main(){
int n;
while(scanf("%d",&n),n){
int dp[2][1010]={{0}};
int ma=0;
for(int i=0;i<n;i++){
char str[1010];
scanf("%s",str);
for(int j=0;j<n;j++){
if(str[j]=='*')dp[(i+1)&1][j+1]=0;
else{
dp[(i+1)&1][j+1]=min(dp[i&1][j],min(dp[(i+1)&1][j],dp[i&1][j+1]))+1;
}
ma=max(ma,dp[(i+1)&1][j+1]);
}
}
printf("%d\n",ma);
}
return 0;
} | C++ | Runtime Efficiency | PIE4PERF_HQTRAIN |
正方形探索
=====
縦に n 行、横に n 列並べられた、合計 n × n のマス目があります。いくつかのマス目には印がついています。各マス目の印の状態を読み込み、印のついていないマス目だけからなる最大の正方形の辺の長さを出力として表示するプログラムを作成してください。
たとえば各データセットで以下のようなデータが与えられます。
```
10
...*....**
..........
**....**..
........*.
..*.......
..........
.*........
..........
....*..***
.*....*...
```
入力データの一行が、一行のマス目を表現します。入力データの文字列のうち、.(ピリオド)は印のついていないマス目、\*(アスタリスク)は印のついているマス目を示しています。
上記の例では、下図の 0 で示される正方形が最大となります。
```
...*....**
..........
**....**..
...00000*.
..*00000..
...00000..
.*.00000..
...00000..
....*..***
.*....*...
```
よって、5 と出力すれば正解になります。
なお、すべてのマス目に印がついている場合には、0 を出力してください。
Input
-----
上記形式で複数のデータセットが与えられます。
n が 0 のとき入力の最後とします。n は 1000 以下とします。入力データの文字列には、ピリオド、アスタリスク、改行以外の文字は含まれません。データセットの数は 50 を超えません。
Output
------
各データセットに対し、最大の正方形の辺の長さ(整数)を1行に出力して下さい。
Sample Input
------------
```
10
...*....**
..........
**....**..
........*.
..*.......
..........
.*........
..........
....*..***
.*....*...
10
****.*****
*..*.*....
****.*....
*....*....
*....*****
..........
****.*****
*..*...*..
****...*..
*..*...*..
0
```
Output for the Sample Input
---------------------------
```
5
3
```
|
#include <stdio.h>
#include <queue>
using namespace std;
int ifield[1001][1001] = {0};
int main()
{
queue<int> x,y,longth;
int n,memlongth,ans;
int dx1[9] = {-1,-1,-1,0,0,0,1,1,1},dy1[9] = {-1,0,1,-1,0,1,-1,0,1},dx2[3] = {1,0,1},dy2[3] = {0,1,1};
while(1)
{
memlongth = 0;
char cfield[1010][1010] = {0},N;
scanf("%d",&n);
if(n == 0)return 0;
scanf("%c",&N);
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n + 1; j++)
{
ifield[j][i] = 0;
}
}
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n + 1; j++)
{
scanf("%c",&cfield[j][i]);
if(cfield[j][i] == '*')
{
x.push(j);
y.push(i);
longth.push(0);
ifield[j][i] = 9;
}
}
}
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
if(ifield[j][i] == 0 && (i == 0 || i == n - 1 || j == 0 || j == n - 1))
{
ifield[j][i] = 1;
x.push(j);
y.push(i);
longth.push(1);
memlongth = 1;
}
}
}
while(x.size())
{
for(int i = 0; i < 9; i++)
{
if(0 <= x.front() + dx1[i] && x.front() + dx1[i] < n &&
0 <= y.front() + dy1[i] && y.front() + dy1[i] < n &&
ifield[x.front() + dx1[i]][y.front() + dy1[i]] == 0)
{
ifield[x.front() + dx1[i]][y.front() + dy1[i]] = longth.front() + 1;
x.push(x.front() + dx1[i]);
y.push(y.front() + dy1[i]);
longth.push(longth.front() + 1);
memlongth = longth.front() + 1;
}
}
x.pop();
y.pop();
longth.pop();
}
if(memlongth == 0)ans = 0;
else ans = memlongth * 2 - 1;
for(int i = 0; i < n - 1; i++)
{
for(int j = 0; j < n - 1; j++)
{
if(ifield[j][i] == memlongth)
{
for(int k = 0; k < 3; k++)
{
if(ifield[j + dx2[k]][i + dy2[k]] == memlongth);
else break;
if(k == 2)ans = memlongth * 2;
}
}
}
}
printf("%d\n",ans);
}
} | #include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
int n,ans = 0;
int dp[1100][1100];
char field;
while(scanf("%d",&n),n)
{
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
{
scanf(" %c",&field);
dp[j + 1][i + 1] = 0;
if(field == '*')dp[j + 1][i + 1] = -1;
}
for(int i = 1; i < n + 1; i++)
{
for(int j = 1; j < n + 1; j++)
{
if(dp[j][i] == -1);
else if(j == 1 || i == 1)dp[j][i] = 1;
else dp[j][i] = max(1,min(dp[j - 1][i - 1],min(dp[j - 1][i],dp[j][i - 1])) + 1);
ans = max(ans,dp[j][i]);
}
}
/*for(int i = 1; i < n + 1; i++)
{
for(int j = 1; j < n + 1; j++)
{
printf("%2d",dp[j][i]);
}
printf("\n");
}*/
printf("%d\n",ans);
ans = 0;
}
return 0;
} | C++ | Runtime Efficiency | PIE4PERF_HQTRAIN |
Sale Result
===========
There is data on sales of your company. Your task is to write a program which identifies good workers.
The program should read a list of data where each item includes the employee ID *i*, the amount of sales *q* and the corresponding unit price *p*. Then, the program should print IDs of employees whose total sales proceeds (i.e. sum of p × q) is greater than or equal to 1,000,000 in the order of inputting. If there is no such employees, the program should print "NA". You can suppose that *n* < 4000, and each employee has an unique ID. The unit price *p* is less than or equal to 1,000,000 and the amount of sales *q* is less than or equal to 100,000.
Input
-----
The input consists of several datasets. The input ends with a line including a single 0. Each dataset consists of:
```
n (the number of data in the list)
i p q
i p q
:
:
i p q
```
Output
------
For each dataset, print a list of employee IDs or a text "NA"
Sample Input
------------
```
4
1001 2000 520
1002 1800 450
1003 1600 625
1001 200 1220
2
1001 100 3
1005 1000 100
2
2013 5000 100
2013 5000 100
0
```
Output for the Sample Input
---------------------------
```
1001
1003
NA
2013
```
| #include <iostream>
#include <map>
#include <vector>
using namespace std;
int main(){
int n,a,b;
bool flag;
string number;
map<string,int> info;
vector<string> seq;
while(cin >> n && n){
info.clear();
seq.clear();
while(n--){
cin >> number >> a >> b;
if(!info[number])seq.push_back(number);
while(b-- && info[number]<1000000){
info[number] += a;
}
}
flag = true;
for(int i=0;i<seq.size();i++){
if(info[seq[i]]>=1000000){
flag = false;
cout << seq[i] << endl;
}
}
if(flag)cout << "NA" << endl;
}
} | #include <iostream>
#include <map>
#include <vector>
using namespace std;
int main(){
long long n,a,b;
bool flag;
int number;
map<int,long long> info;
vector<int> seq;
while(cin >> n && n){
info.clear();
seq.clear();
while(n--){
cin >> number >> a >> b;
if(!info[number])seq.push_back(number);
info[number] += a*b;
}
flag = true;
for(int i=0;i<seq.size();i++){
if(info[seq[i]]>=1000000){
flag = false;
cout << seq[i] << endl;
}
}
if(flag)cout << "NA" << endl;
}
} | C++ | Runtime Efficiency | PIE4PERF_HQTRAIN |
Sale Result
===========
There is data on sales of your company. Your task is to write a program which identifies good workers.
The program should read a list of data where each item includes the employee ID *i*, the amount of sales *q* and the corresponding unit price *p*. Then, the program should print IDs of employees whose total sales proceeds (i.e. sum of p × q) is greater than or equal to 1,000,000 in the order of inputting. If there is no such employees, the program should print "NA". You can suppose that *n* < 4000, and each employee has an unique ID. The unit price *p* is less than or equal to 1,000,000 and the amount of sales *q* is less than or equal to 100,000.
Input
-----
The input consists of several datasets. The input ends with a line including a single 0. Each dataset consists of:
```
n (the number of data in the list)
i p q
i p q
:
:
i p q
```
Output
------
For each dataset, print a list of employee IDs or a text "NA"
Sample Input
------------
```
4
1001 2000 520
1002 1800 450
1003 1600 625
1001 200 1220
2
1001 100 3
1005 1000 100
2
2013 5000 100
2013 5000 100
0
```
Output for the Sample Input
---------------------------
```
1001
1003
NA
2013
```
| #include <iostream>
#include <string>
using namespace std;
typedef struct{
string id;
long long sales;
} staff;
int main(void)
{
long long n;
while(cin>>n){
if(!n) break;
staff staff1[4001];
int pt = 0;
for(int i=0; i< n; i++){
string id;
cin >> id;
long long price;
int bnum;
cin >> price >> bnum;
int m=-1;
for(int j=0;j < i; j++){
if(id==staff1[j].id){
pt++;
m=j;
break;
}
}
if(m==-1){
staff1[i-pt].id = id;
staff1[i-pt].sales = price * bnum;
}else{
staff1[m].sales += price * bnum;
}
}
int count = 0;
for(int i=0; i < n-pt; i++){
if(staff1[i].sales >= 1000000){
count++;
cout << staff1[i].id << endl;
}
}
if(!count) cout << "NA" << endl;
}
return 0;
} | #include<iostream>
#include<map>
#include<vector>
#include<string>
using namespace std;
#define mp make_pair
int main(){
int n;
string a;
long long b,c;
map<string,long long> data;
vector<string> id;
while(true){
id.clear();
data.clear();
cin>>n;
if(!n)
break;
for(int i=0;i<n;i++){
cin>>a>>b>>c;
c*=b;
if(data.find(a)==data.end()){
data.insert(mp(a,c));
id.push_back(a);
}
else{
data[a]+=c;
}
}
bool empty = true;
for(int i=0;i<id.size();i++){
if(data[id[i]]>=1000000){
cout<<id[i]<<endl;
empty = false;
}
}
if (empty)
cout<<"NA"<<endl;
}
return 0;
} | C++ | Runtime Efficiency | PIE4PERF_HQTRAIN |
Discounts of Buckwheat
======================
Aizu is famous for its buckwheat. There are many people who make buckwheat noodles by themselves.
One day, you went shopping to buy buckwheat flour. You can visit three shops, A, B and C. The amount in a bag and its unit price for each shop is determined by the follows table. Note that it is discounted when you buy buckwheat flour in several bags.
| | Shop A | Shop B | Shop C |
| --- | --- | --- | --- |
| Amount in a bag | 200g | 300g | 500g |
| Unit price for a bag (nominal cost) | 380 yen | 550 yen | 850 yen |
| Discounted units | per 5 bags | per 4 bags | per 3 bags |
| Discount rate | reduced by 20 % | reduced by 15 % | reduced by 12 % |
For example, when you buy 12 bags of flour at shop A, the price is reduced by 20 % for 10 bags, but not for other 2 bags. So, the total amount shall be (380 × 10) × 0.8 + 380 × 2 = 3,800 yen.
Write a program which reads the amount of flour, and prints the lowest cost to buy them. Note that you should buy the flour of exactly the same amount as the given input.
Input
-----
The input consists of multiple datasets. For each dataset, an integer a (500 ≤ a ≤ 5000, a is divisible by 100) which represents the amount of flour is given in a line.
The input ends with a line including a zero. Your program should not process for the terminal symbol. The number of datasets does not exceed 50.
Output
------
For each dataset, print an integer which represents the lowest cost.
Sample Input
------------
```
500
2200
0
```
Output for the Sample Input
---------------------------
```
850
3390
```
| #include <iostream>
using namespace std;
int soba[][2] = {
{200, 380},
{300, 550},
{500, 850}
};
int main() {
int n, m = 5000 * 500;
cin >> n;
if (n == 0){
return 0;
}
for (int i = n / soba[2][0] + 100; i >= 0; i--) {
for (int j = n / soba[1][0] + 100; j >= 0; j--) {
for (int k = n / soba[0][0] + 100; k >= 0; k--) {
int w = i * soba[2][0] + j * soba[1][0] + k * soba[0][0];
if (w == n) {
int p = (i / 3 * 3 * soba[2][1] * 0.88) + (i % 3 * soba[2][1]) + (j / 4 * 4 * soba[1][1] * 0.85) + (j % 4 * soba[1][1]) + (k / 5 * 5 * soba[0][1] * 0.8) + (k % 5 * soba[0][1]);
if (p < m) {
m = p;
}
}
}
}
}
cout << m << endl;
return main();
} | #include <iostream>
using namespace std;
int soba[][2] = {
{200, 380},
{300, 550},
{500, 850}
};
int main() {
int n, m = 5000 * 500;
cin >> n;
if (n == 0){
return 0;
}
for (int i = n / soba[2][0]; i >= 0; i--) {
for (int j = n / soba[1][0]; j >= 0; j--) {
for (int k = n / soba[0][0]; k >= 0; k--) {
int w = i * soba[2][0] + j * soba[1][0] + k * soba[0][0];
if (w == n) {
int p = (i / 3 * 3 * soba[2][1] * 0.88) + (i % 3 * soba[2][1]) + (j / 4 * 4 * soba[1][1] * 0.85) + (j % 4 * soba[1][1]) + (k / 5 * 5 * soba[0][1] * 0.8) + (k % 5 * soba[0][1]);
if (p < m) {
m = p;
}
}
}
}
}
cout << m << endl;
return main();
} | C++ | Runtime Efficiency | PIE4PERF_HQTRAIN |
ミルクショップ
=======
鈴木さんは会津地域に新しく搾りたてミルクの移動販売のお店を開きました。その日買い求めに来るお客さんは全員持ち帰るためのボトルを持って既にお店に並んでいて、それ以上増えないものとします。お客さんはそれぞれ1回だけしか注文しません。タンクの蛇口が一つしかないので、一人ずつ順番に販売しなければなりません。そこで、鈴木さんはなるべく並んでいるお客さんの待ち時間を少なくしたいと考えています。
お客さんの人数とお客さんが牛乳を注ぎきるのに要する時間が入力として与えられます。あなたはお客さんの「一人一人の待ち時間の合計」(以下「待ち時間の合計」とする)を最小にするための注文の順序を鈴木さんに代わって調べ、そのときの「待ち時間の合計」を出力して終了するプログラムを作成してください。ただし、お客さんは 10,000 人以下で 1 人あたりに要する時間は 60 分以下とします。
例えば、お客さんの人数が 5 人で、各お客さんが要する時間が順に 2,6,4,3,9 分の場合、そのままの順序だと「待ち時間の合計」は 37 分になります。次の例では、最初の列の順の 2 人目と 3 人目を入れ替えています。この場合、「待ち時間の合計」は 35 分になります。最適な順序だと 31 分で済みます。
| | 待ち時間 | |
| --- | --- | --- |
| 1 人目 2 分 | 0 分 | |
| 2 人目 6 分 | 2 分 | |
| 3 人目 4 分 | 8 分 | |
| 4 人目 3 分 | 12 分 | |
| 5 人目 9 分 | 15 分 | |
| | 37 分 | ← 「待ち時間の合計」 |
2 人目と 3 人目を入れ替えた例
| | 待ち時間 | |
| --- | --- | --- |
| 1 人目 2 分 | 0 分 | |
| 2 人目 4 分 | 2 分 | |
| 3 人目 6 分 | 6 分 | |
| 4 人目 3 分 | 12 分 | |
| 5 人目 9 分 | 15 分 | |
| | 35 分 | ← 「待ち時間の合計」 |
Input
-----
複数のデータセットが与えられます。各データセットは以下の形式で与えられます。
```
n
t1
t2
:
tn
```
1 行目にお客さんの人数 n (n ≤ 10,000) が与えられます。続く n 行に i 人目のお客さんが要する時間を表す整数 ti (0 ≤ ti ≤ 60) がそれぞれ1行に与えられます。
入力は1つの 0 を含む行で終わります。データセットの数は 50 を超えません。
Output
------
各データセットごとに、待ち時間の合計(整数)を1行に出力してください。
Sample Input
------------
```
5
2
6
4
3
9
0
```
Output for the Sample Input
---------------------------
```
31
```
| #include<list>
#include<iostream>
using namespace std;
int main(){
int i;
int n;
long long c;
for(;;){
cin>>n;
if(n==0)
break;
list<int> t;
c=0;
for(i=0;i<n;i++){
int tmp;
cin>>tmp;
t.push_back(tmp);
}
t.sort();
list<int>::iterator it=t.begin();
for(i=0;i<n-1;i++){
c+=(*it)*(n-i-1);
it++;
}
cout<<c<<endl;
}
return 0;
} | #include<algorithm>
#include<cstdio>
using namespace std;
int main(){
int i;
int n;
while(scanf("%d",&n),n){
int a[10000];
for(i=0;i<n;++i)
scanf("%d",a+i);
sort(a,a+n);
long long sm=0;
for(i=0;i<n-1;++i)
sm+=a[i]*(n-i-1);
printf("%lld\n",sm);
}
return 0;
} | C++ | Runtime Efficiency | PIE4PERF_HQTRAIN |
電子蝿
===
ある計算機学者が電子空間に棲息する電子蝿という奇妙な生命体を見つけました。電子蝿の行動を観察しているうちに、この空間の (x, y, z) 地点にいる電子蝿は、次に以下の規則で示される (x', y', z')に移動することが分かりました。

ただし、a1, m1, a2, m2, a3, m3 は電子蝿の個体ごとに定まる正の整数です。A mod B は正の整数 A を正の整数 B で割ったときの余りです。
さらに観察をすると、ある種の電子蝿は (1,1,1) に置いてからしばらくすると、必ず (1,1,1) に戻ってくることがわかりました。このような蝿を戻り蝿と名付けました(1) 。
戻り蝿のデータを入力とし、(1,1,1) に戻ってくる最小の移動回数 (>0) を出力するプログラムを作成してください。なお 1< a1, m1, a2, m2, a3, m3 < 215 とします。
(1) a1 と m1, a2 と m2, a3 と m3 がそれぞれ互いに素 (公約数が 1) である時に戻ります。
Input
-----
複数のデータセットが与えられます。各データセットは以下の形式で与えられます。
```
a1 m1 a2 m2 a3 m3
```
入力は6つの 0 を含む行で終わります。データセットの数は 50 を超えません。
Output
------
各データセットごとに、(1,1,1) に戻ってくる最小の移動回数(整数)を1行に出力してください。
Sample Input
------------
```
2 5 3 7 6 13
517 1024 746 6561 4303 3125
0 0 0 0 0 0
```
Output for the Sample Input
---------------------------
```
12
116640000
```
| #include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
long long int gcd(long long int a, long long int b){
if(b == 0)return a;
return gcd(b,a % b);
}
long long int lcm(long long int a, long long int b){
return a * b / gcd(a,b);
}
int main(){
long long int a[3], m[3], p[3] = {1,1,1}, c[3] = {0};
while(1){
memset(c,0,sizeof(c));
cin >> a[0] >> m[0] >> a[1] >> m[1] >> a[2] >> m[2];
if(!a[0])break;
for(int i=0;i<3;i++){
do{
p[i] = (a[i] * p[i]) % m[i];
//cout << "Turn " << c[i] << ": " << p[i] << endl;
c[i] += 1;
}while(p[i] != 1);
}
//cout << c[0] << c[1] << c[2] << endl;
//int gcd = rec(c[0],rec(c[1],c[2])),
//lcm = rec(c[0],gcd) * rec(c[1],gcd) * rec(c[2],gcd) * gcd;
//cout << gcd << " " << lcm << endl;
cout << lcm(lcm(c[0],c[1]),c[2]) << endl;
}
return 0;
} | #include <cstdio>
#include <cstring>
long long gcd(long long a, long long b){
return b?gcd(b,a % b):a;
}
long long lcm(long long a, long long b){
return a * b / gcd(a,b);
}
int main(){
int a[3], m[3], p[3] = {1,1,1}, c[3] = {0};
while(scanf("%d%d%d%d%d%d",a,m,a+1,m+1,a+2,m+2),a[0]){
memset(c,0,sizeof(c));
for(int i=0;i<3;i++){
do{
p[i] = a[i] * p[i] % m[i];
c[i]++;
}while(p[i] != 1);
}
printf("%lld\n",lcm(lcm(c[0],c[1]),c[2]));
}
return 0;
} | C++ | Runtime Efficiency | PIE4PERF_HQTRAIN |
長方形探索
=====
縦に H 行、横に W 列並べられた、合計 W × H のマス目があります。いくつかのマス目には印がついています。各マス目の印の状態を読み込み、印のついていないマス目だけからなる最大の長方形の面積を出力するプログラムを作成してください。
入力データは 1 行 W 文字から構成され、H 行が与えられます。たとえば以下のようなデータが与えられます。
```
..*....**.
..........
**....***.
....*.....
..*.......
...**.....
.*.*......
..........
..**......
.*..*.....
```
入力データの一行が、一行のマス目を表現します。入力データの文字列のうち、. (ピリオド) は印のついていないマス目、\* (アスタリスク) は印のついているマス目を示しています。入力データの文字列には、ピリオド、アスタリスク、改行以外の文字は含まれません。
上記の例では、下図の 0 で示される長方形が最大となります。
```
..*....**.
..........
**....***.
....*00000
..*..00000
...**00000
.*.*.00000
.....00000
..**.00000
.*..*00000
```
よって、35 と出力すれば正解になります。なお、すべてのマス目に印がついている場合には、0 を出力してください。
Input
-----
複数のデータセットが与えられます。各データセットはスペースで区切られた H と W からなる行から始まり、つづいて H × W の長方形が与えられます。H, W はともに 500 以下とします。
入力は2つの 0 を含む行で終わります。データセットの数は 20 を超えません。
Output
------
各データセットごとに、最大の長方形の面積を1行に出力してください。
Sample Input
------------
```
10 10
...*....**
..........
**....**..
........*.
..*.......
**........
.*........
..........
....*..***
.*....*...
10 10
..*....*..
.*.*...*..
*****..*..
*...*..*..
*...*..*..
..........
****.*...*
..*..*...*
.*...*...*
****..***.
2 3
...
...
0 0
```
Output for the Sample Input
---------------------------
```
28
12
6
```
| #include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <cstdlib>
#include <algorithm>
#include <utility>
#define YNUM(X) ( X < 0 ? X + 3 : X )
typedef std::pair<int, int> WH;
typedef std::vector<WH> whvec;
int main(){
int h, w;
while( std::cin >> h >> w, w + h != 0 ){
bool map[502][502];
std::string in;
std::vector<std::vector<whvec> > data(3, std::vector<whvec>(502));
int data_max = 0;
int height_sum[502][502], width_sum[502][502];
WH wh;
memset( map, 0, sizeof(map) );
memset( height_sum, 0, sizeof(height_sum) );
memset( width_sum, 0, sizeof(width_sum) );
for( int i = 1; i <= h; ++i ){
std::cin >> in;
for( int l = 1; l <= w; ++l ){
map[i][l] = ( in[l - 1] == '.' );
}
}
for( int i = 1; i <= h; ++i ){
for( int l = 1; l <= w; ++l ){
if(map[i][l]){
width_sum[i][l] = width_sum[i][l - 1] + 1;
height_sum[i][l] = height_sum[i - 1][l] + 1;
}
}
}
for( int i = 1, n = 1; n <= h; n++, i = n % 3 ){
data[i] = std::vector<whvec>(502);
for( int l = 1; l <= w; l++ ){
if( map[n][l] ){
if( map[n - 1][l] && map[n][l - 1] ){
int x_cnt = width_sum[n][l], y_cnt = height_sum[n][l];
for( int m = 0; m < data[YNUM(i - 1)][l].size(); ++m ){
wh = WH( std::min(data[YNUM(i - 1)][l][m].first, x_cnt), data[YNUM(i - 1)][l][m].second + 1 );
if( std::find( data[i][l].begin(), data[i][l].end(), wh ) == data[i][l].end() ){
data[i][l].push_back( wh );
data_max = std::max( data_max, wh.first * wh.second );
}
}
for( int m = 0; m < data[i][l - 1].size(); ++m ){
wh = WH( data[i][l - 1][m].first + 1, std::min(data[i][l - 1][m].second, y_cnt) );
if( std::find( data[i][l].begin(), data[i][l].end(), wh ) == data[i][l].end() ){
data[i][l].push_back( wh );
data_max = std::max( data_max, wh.first * wh.second );
}
}
}else if( map[n - 1][l] ){
for( int m = 0; m < data[YNUM(i - 1)][l].size(); ++m ){
data[i][l].push_back( wh = WH( 1, data[YNUM(i - 1)][l][m].second + 1 ) );
data_max = std::max( data_max, wh.first * wh.second );
}
}else if( map[n][l - 1] ){
for( int m = 0; m < data[i][l - 1].size(); ++m ){
data[i][l].push_back( wh = WH( data[i][l - 1][m].first + 1, 1 ) );
data_max = std::max( data_max, wh.first * wh.second );
}
}else data[i][l].push_back( wh = WH( 1, 1 ) ), data_max = std::max( data_max, wh.first * wh.second );
}
}
}
std::cout << data_max << std::endl;
}
return 0;
} | #include <iostream>
#include <string>
#include <cstring>
#include <vector>
#include <cstdlib>
#include <algorithm>
#include <utility>
#define YNUM(X) ( X < 0 ? X + 3 : X )
typedef std::pair<int, int> WH;
//typedef std::vector<WH> whvec;
int h, w;
bool map[502][502];
std::string in;
//std::vector<std::vector<whvec> > data(3, std::vector<whvec>(502));
WH data[3][502][500];
int data_max = 0;
int height_sum[502][502], width_sum[502][502];
int data_pt[3][502];
WH wh;
int main(){
while( std::cin >> h >> w, w + h != 0 ){
data_max = 0;
memset( data_pt, 0, sizeof(data_pt) );
memset( height_sum, 0, sizeof(height_sum) );
memset( width_sum, 0, sizeof(width_sum) );
for( int i = 1; i <= h; ++i ){
std::cin >> in;
for( int l = 1; l <= w; ++l ){
map[i][l] = ( in[l - 1] == '.' );
if(map[i][l]){
width_sum[i][l] = width_sum[i][l - 1] + 1;
height_sum[i][l] = height_sum[i - 1][l] + 1;
}
}
}
for( int i = 1, n = 1; n <= h; n++, i = n % 3 ){
for( int l = 1; l <= w; l++ ){
data_pt[i][l] = 0;
if( map[n][l] ){
if( map[n - 1][l] && map[n][l - 1] ){
int x_cnt = width_sum[n][l], y_cnt = height_sum[n][l];
for( int m = 0; m < data_pt[YNUM(i - 1)][l]; ++m ){
wh = WH( std::min(data[YNUM(i - 1)][l][m].first, x_cnt), data[YNUM(i - 1)][l][m].second + 1 );
if( std::find( data[i][l], data[i][l] + data_pt[i][l], wh ) == data[i][l] + data_pt[i][l] ){
data[i][l][ data_pt[i][l] ] = wh, data_pt[i][l]++;
data_max = std::max( data_max, wh.first * wh.second );
}
}
for( int m = 0; m < data_pt[i][l - 1]; ++m ){
wh = WH( data[i][l - 1][m].first + 1, std::min(data[i][l - 1][m].second, y_cnt) );
if( std::find( data[i][l], data[i][l] + data_pt[i][l], wh ) == data[i][l] + data_pt[i][l] ){
data[i][l][ data_pt[i][l] ] = wh, data_pt[i][l]++;
data_max = std::max( data_max, wh.first * wh.second );
}
}
}else if( map[n - 1][l] ){
for( int m = 0; m < data_pt[YNUM(i - 1)][l]; ++m ){
data[i][l][ data_pt[i][l] ] = (wh = WH( 1, data[YNUM(i - 1)][l][m].second + 1 )), data_pt[i][l]++;
data_max = std::max( data_max, wh.first * wh.second );
}
}else if( map[n][l - 1] ){
for( int m = 0; m < data_pt[i][l - 1]; ++m ){
data[i][l][ data_pt[i][l] ] = (wh = WH( data[i][l - 1][m].first + 1, 1 )), data_pt[i][l]++;
data_max = std::max( data_max, wh.first * wh.second );
}
}else data[i][l][ data_pt[i][l] ] = (wh = WH( 1, 1 )), data_pt[i][l]++, data_max = std::max( data_max, wh.first * wh.second );
}
}
}
std::cout << data_max << std::endl;
}
return 0;
} | C++ | Runtime Efficiency | PIE4PERF_HQTRAIN |
#include<iostream>
#include<vector>
#include<sstream>
#include<cmath>
using namespace std;
const int MAX = 13;
vector<double> Cake;
double box;
double dp[(1<<MAX)][MAX];
void init(){
Cake.clear();
fill(dp[0],dp[0]+(1<<MAX)*MAX,100000000);
dp[0][0] = 0;
}
void make(){
string s;
getline(cin,s);
stringstream ss(s);
double num;
while(ss >> num) Cake.push_back(num);
}
void solve(){
for(int i = 0; i < (1<<Cake.size()); i++)
for(int j = 0; j < (int)Cake.size(); j++){
if(i == 0) {dp[(1<<j)][j] = Cake[j]; continue;}
for(int k = 0; k < (int)Cake.size(); k++)
if(!(i&(1<<k)) && j != k){
dp[i+(1<<k)][k] = min(dp[i+(1<<k)][k],dp[i][j]+2.0*sqrt(Cake[j]*Cake[k]));
// if(dp[i][j] == 0) cout << "error" << endl;
}
}
for(int i = 0; i < (int)Cake.size(); i++){
// cout << "cake " << Cake[i] << endl;
//cout << dp[(1<<Cake.size())-1][i]+Cake[i] << endl;
if(dp[(1<<Cake.size())-1][i]+Cake[i] <= box) {cout << "OK" << endl;return;}
}
cout << "NA" << endl;
}
int main(){
while(cin >> box){
init();
make();
solve();
}
return 0;
} | #include<iostream>
#include<vector>
#include<sstream>
#include<cmath>
using namespace std;
const int MAX = 12;
vector<double> Cake;
double box;
double dp[(1<<MAX)][MAX];
void init(){
Cake.clear();
fill(dp[0],dp[0]+(1<<MAX)*MAX,100000000);
dp[0][0] = 0;
}
void make(){
string s;
getline(cin,s);
stringstream ss(s);
double num;
while(ss >> num) Cake.push_back(num);
}
void solve(){
for(int i = 0; i < (1<<Cake.size()); i++)
for(int j = 0; j < (int)Cake.size(); j++){
if(i == 0) {dp[(1<<j)][j] = Cake[j]; continue;}
for(int k = 0; k < (int)Cake.size(); k++)
if(!(i&(1<<k)) && j != k)
dp[i+(1<<k)][k] = min(dp[i+(1<<k)][k],dp[i][j]+2.0*sqrt(Cake[j]*Cake[k]));
}
for(int i = 0; i < (int)Cake.size(); i++){
if(dp[(1<<Cake.size())-1][i]+Cake[i] <= box) {cout << "OK" << endl;return;}
}
cout << "NA" << endl;
}
int main(){
while(cin >> box){
init();
make();
solve();
}
return 0;
} | C++ | Runtime Efficiency | PIE4PERF_HQTRAIN | |
博士の光電管
======
博士 : ピーター君。やったよ。
ピーター : またですか。今度はどんなくだらない発明ですか。
博士 : あの幻の素粒子アキシオンの検出器を発明したんじゃ。
ピーター : アキシオンといえば、欧州合同原子核研究機構 (CERN) をはじめとする研究者たちが血眼で追っかけているという、あれですよね。本当ですかぁ?
博士 : 本当だってばよ。細かい説明は省くが、非常に強力な磁場を内蔵する特殊な光電管が光ることによって、通過するアキシオンを検出する。
ピーター : 他 に先んじて検出すれば、小柴先生のニュートリノ検出に匹敵するノーベル賞級の研究ですよ。
これで役立たずの研究ばかりしている「ダメ研究室」などという汚名も返上できますね。
博士 : そうだとも。小柴先生の「スーパーカミオカンデ」にあやかって、この装置を、(悪口言ったら)「タダジャオカンデ」と命名した。
ピーター : ちょっと苦しいって言うか、卑屈って言うか・・・。
博士 : それはいいとして、この装置ちょっとした癖があるんじゃよ。
アキシオン粒子がある光電管を通過すると、感度の関係でその光電管と隣接する上下左右の光電管が反応する。
| 図1 | | 図2 |
| --- | --- | --- |
| | | ★ | ● | ● | ● | ● | | --- | --- | --- | --- | --- | | ● | ● | ● | ★ | ● | | ● | ● | ● | ● | ● | | ● | ● | ● | ● | ● | | ● | ● | ★ | ● | ● | | | | | --- | | | | → | | | | | | | ○ | ○ | ● | ○ | ● | | --- | --- | --- | --- | --- | | ○ | ● | ○ | ○ | ○ | | ● | ● | ● | ○ | ● | | ● | ● | ○ | ● | ● | | ● | ○ | ○ | ○ | ● | | | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | | | | | ● | ○ | ○ | ● | ○ | | --- | --- | --- | --- | --- | | ○ | ★ | ★ | ○ | ☆ | | ● | ● | ○ | ● | ● | | ○ | ● | ● | ○ | ● | | ● | ○ | ○ | ○ | ● | | | | | --- | | | | → | | | | | | | ● | ● | ● | ● | ● | | --- | --- | --- | --- | --- | | ● | ● | ● | ○ | ● | | ● | ○ | ● | ● | ○ | | ○ | ● | ● | ○ | ● | | ● | ○ | ○ | ○ | ● | | | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | |
ピーター : つまり、図1の左側★印の光電管を粒子が通過した場合、
右側のように点灯すると言うことですね。
(図は 5 × 5 の例。黒は消灯・白は点灯状態。以下同じ。)
博士 : また反応とは、光電管の状態が反転する。つまり消えている光電管は光り、
光っている光電管は消える。
ピーター : つまり、図2の左側の★印や☆印を粒子が通過した場合、右側のような状態になると。
博士 : これを正方形になんと 100 個 (10 × 10) 配置して待ち受けるわけだ。
ピーター : これだけの大発明、ノーベル賞選考委員会も「ホッチャオカンデ」なんて。
博士 : おぉピーター君、君も我が研究室の作風になじんできたようだね。いい感じだ。
では、早速実験を始めよう。まずはこの装置、現在ランダムに光電管が点いているので、
実験が始められるように全部消えた状態にリセットしてくれたまえ。
なあに、どの光電管にアキシオン粒子を当てれば全部消えるか考えればいいだけじゃよ。
簡単だろう?
ピーター : 考えるのはいいんですが博士。当てるためには、
幻のアキシオン粒子を発生させて打ち込める装置がないといけないんじゃないですか。
博士 : ・・・。
博士とピータ (同時に)コリャアカンデ!だははは・・・・。
ー:
ということで、本日も和気あいあいと暮れてゆく博士の研究室ですが、例によって話は全く進みそうもありません。しょうがないので、ピーター君に代わってプログラムを作成してあげてください。
プログラムは次のようなものになります。
A. 装置の光電管の状態を 10 × 10 の配列として入力します。0は消灯中、1は点灯中を表します。
0 と 1 以外のデータは含まれません。
B. 入力した装置の状態をすべて消灯にするために、アキシオン粒子を通過させる位置を計算して出力します。これは、入力と同じ 10 × 10の配列で光電管の位置を表します。「0 は通過させない」、「1 は通過させる」ことを表します。すべてを消灯する方法は必ず 1 通りだけ存在するものとします。
Input
-----
複数のデータセットが与えられます。1行目にデータセットの数 n (n ≤ 20) が与えられます。各データセットは以下の形式で与えられます。
```
a1,1 a1,2 ... a1,10
a2,1 a2,2 ... a2,10
:
a10,1 a10,2 ... a10,10
```
ai,j は装置の i 行目 j 列目の光電管の状態を示す整数 (0 または 1) を表します。
Output
------
各データセットについて、粒子を通過させる位置を以下の形式で出力してください。
```
b1,1 b1,2 ... b1,10
b2,1 b2,2 ... b2,10
:
b10,1 b10,2 ... b10,10
```
bi,j は装置の i 行目 j 列目の光電管に粒子を通過させるかいなかを示す整数 (0 または 1) を表します。
Sample Input
------------
```
1
0 1 0 0 0 0 0 0 0 0
1 1 1 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0
0 0 0 0 1 1 0 0 0 0
0 0 0 1 0 0 1 0 0 0
0 0 0 0 1 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 1 1 1
0 0 0 0 0 0 0 0 1 0
```
Output for the Sample Input
---------------------------
```
0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 0 0
```
| #include<iostream>
using namespace std;
int t[10][10],u[10][10],ans[10][10],n,flg;
int compute();
void dfs(int);
void change(int,int);
int main(){
cin>>n;
while(n--){
flg=0;
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
cin>>t[i][j];
ans[i][j]=0;
}
}
dfs(0);
}
return 0;
}
void dfs(int x){
if(x==10){
/*
for(int i=0;i<10;i++)cout<<ans[0][i];
cout<<endl;
*/
if(compute()){
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
if(j)cout<<' ';
cout<<ans[i][j];
}
cout<<endl;
}
}
return;
}
ans[0][x]=0;
dfs(x+1);
ans[0][x]=1;
dfs(x+1);
}
int compute(){
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
u[i][j]=t[i][j];
if(i)ans[i][j]=0;
}
}
for(int i=0;i<10;i++){
if(ans[0][i])change(0,i);
}
for(int i=1;i<10;i++){
for(int j=0;j<10;j++){
if(u[i-1][j]){
ans[i][j]=1;
change(i,j);
}
}
}
for(int i=0;i<10;i++){
if(u[9][i])return 0;
}
return 1;
}
void change(int y,int x){
u[y][x]=!u[y][x];
if(y>0)u[y-1][x]=!u[y-1][x];
if(x>0)u[y][x-1]=!u[y][x-1];
if(y<9)u[y+1][x]=!u[y+1][x];
if(x<9)u[y][x+1]=!u[y][x+1];
} | #include<iostream>
using namespace std;
int I,flg;
int t[10][10],u[10][10];
int ans[10][10];
void check();
void dfs(int);
void compute(int,int);
int main(){
cin>>I;
while(I--){
flg=0;
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
cin>>t[i][j];
u[i][j]=t[i][j];
ans[i][j]=0;
}
}
dfs(0);
}
return 0;
}
void dfs(int x){
if(flg)return;
if(x==10){
check();
}else{
ans[0][x]=0;
dfs(x+1);
if(flg)return;
ans[0][x]=1;
dfs(x+1);
}
}
void check(){
if(flg)return;
for(int i=0;i<10;i++)for(int j=0;j<10;j++)t[i][j]=u[i][j];
for(int i=0;i<10;i++)if(ans[0][i]==1)compute(0,i);
for(int i=1;i<10;i++){
for(int j=0;j<10;j++){
ans[i][j]=0;
if(t[i-1][j]==1){
ans[i][j]=1;
compute(i,j);
}
}
}
/*
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
if(j)cout<<' ';
cout<<t[i][j];
}
cout<<endl;
}
*/
int cnt=0;
for(int i=0;i<10;i++)cnt+=t[9][i];
if(cnt==0){
flg=1;
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
if(j)cout<<' ';
cout<<ans[i][j];
}
cout<<endl;
}
}
}
void compute(int y,int x){
t[y][x]=(t[y][x]*-1)+1;
if(y>0)t[y-1][x]=(t[y-1][x]*-1)+1;
if(x>0)t[y][x-1]=(t[y][x-1]*-1)+1;
if(y<9)t[y+1][x]=(t[y+1][x]*-1)+1;
if(x<9)t[y][x+1]=(t[y][x+1]*-1)+1;
} | C++ | Runtime Efficiency | PIE4PERF_HQTRAIN |
素数の性質
=====
4 で割ると 3 あまる素数 n (11、19、23 など) には、面白い性質があります。1 以上 n 未満の自然数 (1, 2,... , n - 1) を 2 乗したものを n で割ったあまりを計算した結果を並べると、同じ数になるものがあるため、互いに異なった数の個数は、(n - 1)/2 になります。
この様にして得られた数の集合には、特別な性質があります。得られた数の集合から、互いに異なる 2 つ a と b を選んでその差を計算します。差が負になったときは、その差に n を足します。さらに結果が (n - 1)/2 より大きいときは、その差を n から引きます。
例えば、n = 11 のとき 1 と 9 の差は、1 − 9 = −8 → −8 + n = −8 + 11 = 3 になります。9 と 1 の差も 9 −1 = 8 → n − 8 = 11 − 8 = 3 で、同じ値 3 になります。この差は、円周上に 0, 1, ・・・, n - 1 を書いて、二つの数字の間の短い方の円弧を考えるとわかりやすくなります。(下図参照)

こうして得られた数の「差」は、1, 2, . . ., (n - 1)/2 のいずれかであり、同じ回数出現します。
【例】 n = 11 の時は、以下のようになります。
1. 1 から n-1 までの数を 2 乗したものを n で割った余りを計算します。
```
12 = 1 → 1
22 = 4 → 4
32 = 9 → 9
42 = 16 → 5
52 = 25 → 3
62 = 36 → 3
72 = 49 → 5
82 = 64 → 9
92 = 81 → 4
102 = 100 → 1
```
2. a, b の「差」の計算
1. 1 で得られた 1, 3, 4, 5, 9 について異なる数同士の差を計算します。
2. 計算結果が負の場合、n = 11 を加算します。
3. さらに、計算結果が (n-1)/2 = 5 より大きい場合 n = 11 から減算します。
3. 出現回数を求める
計算結果 1, 2, 3, 4, 5 の出現回数をそれぞれ数え上げます。
これらの計算結果から 1, 2, 3, 4, 5 の出現回数が 4 回であることがわかります。この性質は 4 で割ると 3 あまる素数特有の性質であり 4 で割ると 1 あまる素数ではこのようなことはおきません。このことを確認するため、10000 以下の奇数 n を入力とし、例題にあるような計算 (n で割ったあまりの 2 乗の差の頻度を求める)を実行し、その出現回数を出力するプログラムを作成してください。
Input
-----
複数のデータセットが与えられます。各データセットとして1つの整数 n (n ≤ 10000) が1行に与えられます。入力は 0 を1つ含む行でおわります。
Output
------
各データセットについて、出現頻度を以下の形式で出力してください。
```
剰余の平方の差が 1 である(a, b)の出現個数(整数)
剰余の平方の差が 2 である(a, b)の出現個数(整数)
:
:
剰余の平方の差が (n-1)/2 である(a, b)の出現個数(整数)
```
Sample Input
------------
```
11
15
0
```
Output for the Sample Input
---------------------------
```
4
4
4
4
4
2
2
4
2
4
4
2
```
| #include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <cstring>
#include <deque>
#include <cstdio>
#include <cmath>
using namespace std;
#define reep(i,f,t) for(int i=f ; i<int(t) ; ++i)
#define rep(i,n) reep(i, 0, n)
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
int main()
{
int n;
while(scanf("%d", &n), n){
vi nums(n, 0);
reep(i, 1, n)
nums[(i*i)%n] |= 1;
reep(i, 1, (n+1)/2){
int sum = 0;
rep(j, n)
sum += nums[j] & nums[(j+i)%n];
printf("%d\n", sum * 2);
}
}
return 0;
} | #include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <cstring>
#include <deque>
#include <cstdio>
#include <cmath>
using namespace std;
#define reep(i,f,t) for(int i=f ; i<int(t) ; ++i)
#define rep(i,n) reep(i, 0, n)
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
int main()
{
int nums[16384], ans[8192];
int i, j, n, size, half;
while(scanf("%d", &n), n){
size = 0;
half = (n-1) / 2;
for(i=1; i<n; ++i)
nums[size++] = (i*i) % n;
sort(nums, nums + size);
size = unique(nums, nums + size) - nums;
memset(ans, 0, (half+1)*4);
for(i=0; i<size; ++i){
for(j=0; j<i; ++j){
int p = abs(nums[i]-nums[j]);
if(p>half) p = n - p;
++ans[p];
}
}
for(i=1; i<=half; ++i)
printf("%d\n", ans[i]*2);
}
return 0;
} | C++ | Runtime Efficiency | PIE4PERF_HQTRAIN |
牽牛と織女
=====
織女は天帝の子供でしたが、父の言いつけであけてもくれても機を織っていました。
織女の織る雲錦という見事な布で仕立てた服を着るのが天帝の楽しみでした。雲錦は寿命が短くすぐに劣化してしまいますが、働き者の織女が毎日織ってくれるので、問題はありませんでした。織女は、父の言いつけを守り、毎日毎日雲錦を織り続けていたので、ボーイフレンドはいませんでした。かわいそうに思った父は、天の川の向こう岸に住む牽牛という働き者を紹介し、嫁入りさせました。
すると、織女は、結婚の楽しさに夢中になって、機織りなどそっちのけで、牽牛と遊び呆けています。天帝のお気に入りの雲錦の服も新しく仕立てられないためボロボロになってしまいました。
これには父も怒って、織女を宮殿に連れ戻したいと思いました。しかし人間である牽牛の前にボロボロの服で姿を現すわけにはいきません。遊び呆けている二人を 3 角形の壁で遮断し自分以外の全てのものが行き来できなくすることを考えました。そして、牽牛に見つからずに、織女に会って、まじめに機を織るか、さもなければ強制的に連れ帰ると宣言するというのです。

天帝はこの作戦を遂行するために 3 角形の壁生成装置を開発することにしました。3 角形の 3 頂点の位置 (xp1, yp1), (xp2, yp2), (xp3, yp3)、牽牛の位置 (xk, yk)、および織女の位置 (xs, ys)、を入力とし、三角形が牽牛と織女を遮断しているか否かを判定し、遮断できている場合は OK、遮断できていない場合には NG を出力するプログラムを作成してください。ただし、遮断しているとは、牽牛と織女のいずれかが三角形の内側にあり、他方が外側にある場合を言います。牽牛と織女は三角形の頂点もしくは辺の上にはいないものとします。
織女と牽牛は時々刻々場所を変えるため、プログラムは様々な位置情報を入力し質問に答えなければなりません。
Input
-----
入力は以下の形式で与えられます。
```
n
query1
query2
:
queryn
```
1行目に判別したい情報の個数 n (n ≤ 10000)、続く n 行に i 番目の質問 queryi が与えられます。各質問は以下の形式で与えられます。
```
xp1 yp1 xp2 yp2 xp3 yp3 xk yk xs ys
```
各質問として、3 角形の 3 頂点の位置、牽牛の位置、および織女の位置 (-1000 ≤ xp1, yp1, xp2, yp2, xp3, yp3, xk, yk, xs, ys ≤ 1000) が1行に与えられます。入力はすべて整数です。
Output
------
質問ごとに、判定結果 OK または NG を1行に出力してください。
Sample Input
------------
```
5
2 5 9 2 8 9 2 11 6 5
2 5 9 2 8 9 2 11 12 6
2 5 9 2 8 9 2 11 11 9
14 1 25 7 17 12 17 9 20 5
14 1 25 7 17 12 22 13 20 5
```
Output for the Sample Input
---------------------------
```
OK
NG
NG
NG
OK
```
| /**************** Geometrical Library ****************/
#include<cmath>
#include<vector>
#include<algorithm>
#define EPS 1e-9
enum {CCW=1,CW=-1,ON=0};
#define mp make_pair
#define pb push_back
using namespace std;
class Point{
public:
double x,y;
Point(){}
Point(double xx,double yy):x(xx),y(yy){}
};
class Line:public vector<Point>{
public:
Line(const Point &a,const Point &b){
pb(a),pb(b);
}
};
class Segment:public Line{
public:
Segment(const Point &a,const Point &b):Line(a,b){}
};
class Polygon:public vector<Point>{};
class Circle{
public:
Point c;
double r;
Circle(const Point &cc,double rr) : c(cc),r(rr){}
};
Point &operator +=(Point &a,const Point &b){
a.x+=b.x,a.y+=b.y;
return a;
}
Point &operator -=(Point &a,const Point &b){
a.x-=b.x,a.y-=b.y;
return a;
}
Point &operator *=(Point &a,double c){
a.x*=c,a.y*=c;
return a;
}
Point &operator /=(Point &a,double c){
a.x/=c,a.y/=c;
return a;
}
Point operator +(const Point &a,const Point &b){
Point c=a;
return c+=b;
}
Point operator -(const Point &a,const Point &b){
Point c=a;
return c-=b;
}
Point operator *(double c,const Point &a){
Point b=a;
return b*=c;
}
Point operator /(const Point &a,double c){
Point b=a;
return b/=c;
}
bool operator <(const Point &a,const Point &b){
return (a.x==b.x)?(a.y<b.y):(a.x<b.x);
}
bool operator >(const Point &a,const Point &b){
return b<a;
}
double dot(const Point &a,const Point &b){
return a.x*b.x+a.y*b.y;
}
double cross(const Point &a,const Point &b){
return a.x*b.y-a.y*b.x;
}
double dis2(const Point &a,const Point &b){
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
int ccw(const Point &a,Point b,Point c){
b-=a,c-=a;
double rotdir=cross(b,c);
if(rotdir>EPS) return CCW;
if(rotdir<-EPS) return CW;
return ON;
}
Polygon ConvexHull(vector<Point> ps){
int n=ps.size();
sort(ps.begin(),ps.end());
vector<Point> ch_u(n);
int n_u=0;
for(int i=n-1;i>=0;i--){
ch_u[n_u++]=ps[i];
while(n_u>=3 && ccw(ch_u[n_u-3],ch_u[n_u-2],ch_u[n_u-1])!=CCW){
ch_u[n_u-2]=ch_u[n_u-1];
n_u--;
}
}
vector<Point> ch_l(n);
int n_l=0;
for(int i=0;i<n;i++){
ch_l[n_l++]=ps[i];
while(n_l>=3 && ccw(ch_l[n_l-3],ch_l[n_l-2],ch_l[n_l-1])!=CCW){
ch_l[n_l-2]=ch_l[n_l-1];
n_l--;
}
}
Polygon ch;
for(int i=0;i<n_u-1;i++) ch.pb(ch_u[i]);
for(int i=0;i<n_l-1;i++) ch.pb(ch_l[i]);
return ch;
}
/*
bool parallel(const Line &l,const Line &m){
return abs(cross(l[1]-l[0],m[1]-m[0]))<EPS;
}
bool orthogonal(const Line &l,const Line &m){
return abs(dot(l[1]-l[0],m[1]-m[0]))<EPS;
}
*/
inline void calc_abc(const Line &l,double &a,double &b,double &c){ // l : ax+by+c=0
a=l[0].y-l[1].y;
b=l[1].x-l[0].x;
c=l[0].x*l[1].y-l[1].x*l[0].y;
}
bool intersect(const Line &l,const Line &m,Point *p=NULL){
// this routine also returns true in case "M is on L", "M sessuru L", etc,.
if(abs(cross(l[1]-l[0],m[1]-m[0]))>EPS
|| abs(cross(l[1]-l[0],m[0]-l[0]))<EPS){
if(p){
double a1,b1,c1,a2,b2,c2;
calc_abc(l,a1,b1,c1);
calc_abc(m,a2,b2,c2);
double det=a1*b2-a2*b1;
if(abs(det)<EPS) *p=l[0]; // l == m
else{
p->x=(b1*c2-b2*c1)/det;
p->y=(a2*c1-a1*c2)/det;
}
}
return true;
}
return false;
}
bool intersect(const Segment &s,const Segment &t,Point *p=NULL){
if(max(s[0].x,s[1].x)<min(t[0].x,t[1].x)
|| max(t[0].x,t[1].x)<min(s[0].x,s[1].x)
|| max(s[0].y,s[1].y)<min(t[0].y,t[1].y)
|| max(t[0].y,t[1].y)<min(s[0].y,s[1].y)) return false;
if(ccw(s[0],s[1],t[0])*ccw(s[0],s[1],t[1])<=0
&& ccw(t[0],t[1],s[0])*ccw(t[0],t[1],s[1])<=0){
if(p){
double a1,b1,c1,a2,b2,c2;
calc_abc(s,a1,b1,c1);
calc_abc(t,a2,b2,c2);
double det=a1*b2-a2*b1;
if(abs(det)<EPS){ // s is parallel to t
Point q[3]={s[0],s[1],t[0]};
for(int i=0;i<3;i++){
if(dot(q[i]-s[0],q[i]-s[1])<EPS && dot(q[i]-t[0],q[i]-t[1])<EPS){
*p=q[i];
break;
}
}
}
else{
p->x=(b1*c2-b2*c1)/det;
p->y=(a2*c1-a1*c2)/det;
}
}
return true;
}
return false;
}
Point perp_foot(const Point &p,const Line &l){
double a,b,c;
calc_abc(l,a,b,c);
return p-(a*p.x+b*p.y+c)/(a*a+b*b)*Point(a,b);
}
Line perp_bisector(const Point &a,const Point &b){
return Line(Point((a.x+a.y+b.x-b.y)/2,(b.x+b.y-a.x+a.y)/2),
Point((b.x+b.y+a.x-a.y)/2,(a.x+a.y-b.x+b.y)/2));
}
/*
double area(const Point &a,const Point &b,const Point &c){
return abs(cross(b-a,c-a))/2;
}
*/
double area(const Polygon &pl){
int n=pl.size();
double a=0;
for(int i=0;i<n;i++) a+=cross(pl[i],pl[(i+1)%n]);
return abs(a)/2;
}
/**************** Library END ****************/
#include<cstdio>
int main(){
int n; scanf("%d",&n);
while(n--){
Point a,b,c,star[2];
scanf("%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf",
&a.x,&a.y,&b.x,&b.y,&c.x,&c.y,&star[0].x,&star[0].y,&star[1].x,&star[1].y);
bool in[2];
for(int i=0;i<2;i++){
int cnt=0;
cnt+=ccw(a,b,star[i]);
cnt+=ccw(b,c,star[i]);
cnt+=ccw(c,a,star[i]);
in[i]=(cnt==3*CCW || cnt==3*CW);
}
puts(in[0]==in[1]?"NG":"OK");
}
return 0;
} | /**************** Geometrical Library ****************/
#include<cmath>
#include<vector>
#include<algorithm>
#define EPS 1e-9
enum {CCW=1,CW=-1,ON=0};
#define mp make_pair
#define pb push_back
using namespace std;
class Point{
public:
double x,y;
Point(){}
Point(double xx,double yy):x(xx),y(yy){}
};
class Line:public vector<Point>{
public:
Line(const Point &a,const Point &b){
pb(a),pb(b);
}
};
class Segment:public Line{
public:
Segment(const Point &a,const Point &b):Line(a,b){}
};
class Polygon:public vector<Point>{};
class Circle{
public:
Point c;
double r;
Circle(const Point &cc,double rr) : c(cc),r(rr){}
};
Point &operator +=(Point &a,const Point &b){
a.x+=b.x,a.y+=b.y;
return a;
}
Point &operator -=(Point &a,const Point &b){
a.x-=b.x,a.y-=b.y;
return a;
}
Point &operator *=(Point &a,double c){
a.x*=c,a.y*=c;
return a;
}
Point &operator /=(Point &a,double c){
a.x/=c,a.y/=c;
return a;
}
Point operator +(const Point &a,const Point &b){
Point c=a;
return c+=b;
}
Point operator -(const Point &a,const Point &b){
Point c=a;
return c-=b;
}
Point operator *(double c,const Point &a){
Point b=a;
return b*=c;
}
Point operator /(const Point &a,double c){
Point b=a;
return b/=c;
}
bool operator <(const Point &a,const Point &b){
return (a.x==b.x)?(a.y<b.y):(a.x<b.x);
}
bool operator >(const Point &a,const Point &b){
return b<a;
}
double cross(const Point &a,const Point &b){
return a.x*b.y-a.y*b.x;
}
int ccw(const Point &a,Point b,Point c){
b-=a,c-=a;
double rotdir=cross(b,c);
if(rotdir>EPS) return CCW;
if(rotdir<-EPS) return CW;
return ON;
}
/**************** Library END ****************/
#include<cstdio>
int main(){
int n; scanf("%d",&n);
while(n--){
Point a,b,c,star[2];
scanf("%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf",
&a.x,&a.y,&b.x,&b.y,&c.x,&c.y,&star[0].x,&star[0].y,&star[1].x,&star[1].y);
bool in[2];
for(int i=0;i<2;i++){
int cnt=0;
cnt+=ccw(a,b,star[i]);
cnt+=ccw(b,c,star[i]);
cnt+=ccw(c,a,star[i]);
in[i]=(cnt==3*CCW || cnt==3*CW);
}
puts(in[0]==in[1]?"NG":"OK");
}
return 0;
} | C++ | Runtime Efficiency | PIE4PERF_HQTRAIN |
Twin Prime
==========
Prime numbers are widely applied for cryptographic and communication technology.
A twin prime is a prime number that differs from another prime number by 2.
For example, (5, 7) and (11, 13) are twin prime pairs.
In this problem, we call the greater number of a twin prime "size of the twin prime."
Your task is to create a program which reads an integer *n* and prints a twin prime which has the maximum size among twin primes less than or equals to *n*
You may assume that 5 ≤ *n* ≤ 10000.
Input
-----
The input is a sequence of datasets. The end of the input is indicated by a line containing one zero. Each dataset is formatted as follows:
```
n (integer)
```
Output
------
For each dataset, print the twin prime *p* and *q* (*p* < *q*). *p* and *q* should be separated by a single space.
Sample Input
------------
```
12
100
200
300
0
```
Output for the Sample Input
---------------------------
```
5 7
71 73
197 199
281 283
```
| #include<stdio.h>
const int MAX_X = 10000;
int prime[10000];
int main(){
int i, k, n;
int border[10];
for( i = 2; i <= MAX_X; i++) {
prime[i] = 1;
}
for(i = 2; i*i <= MAX_X; i++){
if(prime[i]) {
for(k = 2 * i; k <= MAX_X; k += i){
prime[k] = 0;
}
}
}
while(1){
scanf("%d", &n);
if( n == 0) break;
int max;
for( i = n; i >= 2; i--){
if( prime[i]){
if( prime[i - 2]){
max = i;
break;
}
}
}
printf("%d %d\n", max -2, max);
}
return 0;
} | #include<stdio.h>
int isprime(int n){
for(int i = 2; i * i <= n; i++){
if( n % i == 0) return 0;
}
return 1;
}
int main(){
int n, i;
while(1){
scanf("%d", &n);
if( n == 0) break;
int max = 0;
for( i = n; i >=2; i--){
if( isprime(i)){
if(isprime( i - 2)){
max = i;
break;
}
}
}
printf("%d %d\n", max - 2, max);
}
return 0;
} | C++ | Runtime Efficiency | PIE4PERF_HQTRAIN |
Twin Prime
==========
Prime numbers are widely applied for cryptographic and communication technology.
A twin prime is a prime number that differs from another prime number by 2.
For example, (5, 7) and (11, 13) are twin prime pairs.
In this problem, we call the greater number of a twin prime "size of the twin prime."
Your task is to create a program which reads an integer *n* and prints a twin prime which has the maximum size among twin primes less than or equals to *n*
You may assume that 5 ≤ *n* ≤ 10000.
Input
-----
The input is a sequence of datasets. The end of the input is indicated by a line containing one zero. Each dataset is formatted as follows:
```
n (integer)
```
Output
------
For each dataset, print the twin prime *p* and *q* (*p* < *q*). *p* and *q* should be separated by a single space.
Sample Input
------------
```
12
100
200
300
0
```
Output for the Sample Input
---------------------------
```
5 7
71 73
197 199
281 283
```
| #include "stdio.h"
const int MAX_V = 10000; // 1????????§????´???°??¨?????????
int prime[MAX_V+1]; // 1????´???°,0????´???°??§?????????
int main() {
int i, k, n;
for(i = 2; i <= MAX_V; i++){
prime[i] = 1; // 2??\???????´???°??¨??????
}
for(i = 2; i * i <= MAX_V; i++) {
if(prime[i]) {
for(k = 2 * i; k <= MAX_V; k += i) {
prime[k] = 0; // ?´???°???2?????\???????????°????´???°??§?????????
}
}
}
while (1) {
scanf("%d",&n);
if (n == 0)break;
// while(scanf("%d", &n) != 0) {
int p1 = 0,q1 = 0;
for(i = n;3 <= i;i--){
if(prime[i] == 1 && prime[i-2] == 1){ //??????????´???°
q1 = i;
p1 = i-2;
break;
}
}
printf("%d %d\n",p1,q1);//for?????????????????????
}
return 0;
}
//????¨??????¨???????????????????????? | #include "stdio.h"
//#include "time.h"
//?????§??????????´???°?????????????????????
// 5<= n <= 10000
//p1=?°????????´???°p2=??§???????´???°
int isprime(int n) {
int i;
for(i = 2; i * i <= n; i++) { //??????????§????????????¨??§??¶??°??§??????????´???°??¨??????
if(n % i == 0)return 0; //????????????????????§?´???°??§?????????
}
return 1;
}
int main(){
int i,n,p1,q1;
// clock_t start, now;//??£?¨?
while(1){
scanf("%d",&n);
// start = clock(); // ????§???????
if(n == 0)break;
for(i = n;3 <= i;i--){
if (isprime(i) == 1 && isprime(i - 2) == 1) break;
}
p1 = i-2;
q1 = i;
// now = clock(); // ????????????
// printf("%f\n", (double)(now - start) / CLOCKS_PER_SEC);//??¬???
printf("%d %d\n",p1,q1);
}
return 0;
} | C++ | Runtime Efficiency | PIE4PERF_HQTRAIN |
Twin Prime
==========
Prime numbers are widely applied for cryptographic and communication technology.
A twin prime is a prime number that differs from another prime number by 2.
For example, (5, 7) and (11, 13) are twin prime pairs.
In this problem, we call the greater number of a twin prime "size of the twin prime."
Your task is to create a program which reads an integer *n* and prints a twin prime which has the maximum size among twin primes less than or equals to *n*
You may assume that 5 ≤ *n* ≤ 10000.
Input
-----
The input is a sequence of datasets. The end of the input is indicated by a line containing one zero. Each dataset is formatted as follows:
```
n (integer)
```
Output
------
For each dataset, print the twin prime *p* and *q* (*p* < *q*). *p* and *q* should be separated by a single space.
Sample Input
------------
```
12
100
200
300
0
```
Output for the Sample Input
---------------------------
```
5 7
71 73
197 199
281 283
```
| #include <stdio.h>
int main() {
int i, k, v,an1,an2,suu;
int MAX_V = 10000;
int prime[MAX_V+1];
for(i = 0; i <= MAX_V; i++) {
prime[i] = 1;
}
for(i = 2; i*i <= MAX_V; i++) {
if(prime[i]) {
for(k = 2 * i; k <= MAX_V; k += i) {
prime[k] = 0;
}
}
}
while(1){
scanf("%d",&suu);
if(suu != 0){
for(k=2;k<=suu;k++){
if(prime[k-2]==1 && prime[k]==1){
an1=k-2;
an2=k;
}
}
printf("%d %d\n",an1,an2);
}
else{
break;
}
}
return 0;
} | #include <stdio.h>
int isprime(int n) {
int i;
for(int i = 2; i * i <= n; i++) {
if(n % i == 0) return 0;
}
return 1;
}
int main(){
int k,suu,an1,an2;
while(1){
scanf("%d",&suu);
if(suu == 0) break;
for(k=suu;k>0;k--){
if(isprime(k-2)==1 && isprime(k)==1){
an1=k-2;
an2=k;
break;
}
}
printf("%d %d\n",an1,an2);
}
} | C++ | Runtime Efficiency | PIE4PERF_HQTRAIN |
Twin Prime
==========
Prime numbers are widely applied for cryptographic and communication technology.
A twin prime is a prime number that differs from another prime number by 2.
For example, (5, 7) and (11, 13) are twin prime pairs.
In this problem, we call the greater number of a twin prime "size of the twin prime."
Your task is to create a program which reads an integer *n* and prints a twin prime which has the maximum size among twin primes less than or equals to *n*
You may assume that 5 ≤ *n* ≤ 10000.
Input
-----
The input is a sequence of datasets. The end of the input is indicated by a line containing one zero. Each dataset is formatted as follows:
```
n (integer)
```
Output
------
For each dataset, print the twin prime *p* and *q* (*p* < *q*). *p* and *q* should be separated by a single space.
Sample Input
------------
```
12
100
200
300
0
```
Output for the Sample Input
---------------------------
```
5 7
71 73
197 199
281 283
```
| #include <stdio.h>
const int MAX_V = 10000; // 1????????§????´???°??¨?????????
int main() {
int i, k, n, p, q;
int prime[MAX_V+1]; // 1????´???°???0????´???°??§?????????
for(i = 2; i <= MAX_V; i++) {
prime[i] = 1; // 2??\???????´???°??¨??????
}
for(i = 2; i*i <= MAX_V; i++) {
if(prime[i]) {
for(k = 2 * i; k <= MAX_V; k += i) {
prime[k] = 0; // ?´???°???2?????\???????????°????´???°??§?????????
}
}
}
//?´???°??¨fin
//while(scanf("%d",&n) != 0){
while(1){
scanf("%d",&n);
if(n==0)break;
for(i=n;i>=3;i--){//???????????¨????´???°????????????
if(prime[i] == 1 && prime[i-2] == 1){
p = i;
q = p-2;
break;
}
}
// ans[a]=i; a++;
// ans[a]=i-2; a++;
// }
// for(i=0;i<=a;i=i+2){
// printf("%d %d\n",ans[i],ans[i+1]);
// }
printf("%d %d\n",q,p);
}
return 0;
} | #include <stdio.h>
#include <time.h>
int isprime(int n){
for(int i = 2; i * i <= n; i++){
if(n % i == 0){
return 0;
}
}
return 1;
}
int main() {
int i,p,q,n;
while(1){
scanf("%d",&n);
if(n == 0){
break;
}
for(i=n;i>=2;i--){
if(isprime(i) && isprime(i-2) ){
break;
}
}
printf("%d %d\n",i-2,i);
}
return 0;
}
// clock_t start, now;
// now = clock();
// printf("%f\n",(double)(now - start) / CLOCKS_PER_SEC); | C++ | Runtime Efficiency | PIE4PERF_HQTRAIN |
城の堀
===
いま、忍者が城外から天守閣に忍び入ろうと計画を立てています。この忍者は地面を走ったり、堀の中を泳いだりすることは難なくできますが、堀から這い上がることはとても苦手なので、忍者は堀に入る回数をできるだけ少なくしたいと考えています。
お城の見取図を入力とし、城外から天守閣に至るまでに、堀から這い上がらなくてはならない最小回数を出力するプログラムを作成して下さい。お城の見取図は二次元格子として与えられます。見取り図に描かれた記号には、天守閣の位置を示す「&」と堀の位置を示す「#」の位置が記され、そのほかの地点には「.」(半角ピリオド)が記されています。なお、お城には天守閣は一つだけあるものとし、忍者は走ったり泳いだりして移動するときに東西南北方向に1マスずつ移動するものとし、斜めには移動しません。
Input
-----
複数のデータセットの並びが入力として与えられます。入力の終わりはゼロ2つの行で示されます。各データセットは以下の形式で与えられます。
```
n m
c1,1c1,2...c1,n
c2,1c2,2...c2,n
:
cm,1cm,2...cm,n
```
1行目に見取図の東西の幅 n と 南北の幅 m (1 ≤ n, m ≤ 100) が与えられる。続くm 行に見取り図の i 行目の情報が与えられる。各情報は記号「&」、「#」、「.」からなる長さ n の文字列である。
データセットの数は 50 を越えない。
Output
------
データセットごとに堀から這い上がらなくてはならない最小回数(整数)を1行に出力します。
Sample Input
------------
```
5 5
.###.
#...#
#.&.#
#...#
.###.
18 15
..####....####....
####..####....####
#...............##
.#.############.##
#..#..........#.##
.#.#.########.#.##
#..#.#......#.#.##
.#.#....&...#.#.##
#..#........#.#.##
.#.#.########.#.##
#..#..........#.##
.#.############.##
#...............##
.#################
##################
9 10
#########
........#
#######.#
#.....#.#
#.###.#.#
#.#&#.#.#
#.#...#.#
#.#####.#
#.......#
#########
9 3
###...###
#.#.&.#.#
###...###
0 0
</pre>
<H2>Output for the Sample Input</H2>
<pre>
1
2
0
0
</pre>
```
| #include<iostream>
#include<vector>
#include<string>
#include<stack>
using namespace std;
int main(){
int w,h;
string line;
while(true){
cin>>w>>h;
if(!w&&!h)
break;
vector<string> castle;
for(int i=0;i<h;i++){
cin>>line;
castle.push_back(line);
}
int count[w+2][h+2];
int sx,sy;
const int MAX = 1000;
for(int y=1;y<h+1;y++){
for(int x=1;x<w+1;x++){
count[x][y] = MAX;
if (castle[y-1][x-1]=='&'){
sx = x;
sy = y;
castle[y-1][x-1]='.';
}
}
}
int min = MAX;
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};
count[sx][sy] = 0;
stack<int> st;
st.push(sy*(w+2)+sx);
while(!st.empty()){
int x = st.top()%(w+2);
int y = st.top()/(w+2);
st.pop();
char c = castle[y-1][x-1];
int n;
for(int d=0;d<4;d++){
n = count[x][y];
if(x+dx[d]>0&&y+dy[d]>0&&x+dx[d]<w+1&&y+dy[d]<h+1){
if(c=='.'&&castle[y+dy[d]-1][x+dx[d]-1]=='#')
n++;
if(n<count[x+dx[d]][y+dy[d]]){
count[x+dx[d]][y+dy[d]] = n;
st.push((y+dy[d])*(w+2)+x+dx[d]);
}
}
else{
if(n<min)
min = n;
}
}
}
cout<<min<<endl;
}
return 0;
} | #include<iostream>
#include<vector>
#include<string>
#include<queue>
using namespace std;
int main(){
int w,h;
string line;
while(true){
cin>>w>>h;
if(!w&&!h)
break;
vector<string> castle;
for(int i=0;i<h;i++){
cin>>line;
castle.push_back(line);
}
int count[w+2][h+2];
int sx,sy;
const int MAX = 100;
for(int y=1;y<h+1;y++){
for(int x=1;x<w+1;x++){
count[x][y] = MAX;
if (castle[y-1][x-1]=='&'){
sx = x;
sy = y;
castle[y-1][x-1]='.';
}
}
}
int min = MAX;
int dx[4] = {1,0,-1,0};
int dy[4] = {0,1,0,-1};
count[sx][sy] = 0;
queue<int> st;
st.push(sy*(w+2)+sx);
while(!st.empty()){
int x = st.front()%(w+2);
int y = st.front()/(w+2);
st.pop();
char c = castle[y-1][x-1];
int n;
for(int d=0;d<4;d++){
n = count[x][y];
if(x+dx[d]>0&&y+dy[d]>0&&x+dx[d]<w+1&&y+dy[d]<h+1){
if(c=='.'&&castle[y+dy[d]-1][x+dx[d]-1]=='#')
n++;
if(n<count[x+dx[d]][y+dy[d]]){
count[x+dx[d]][y+dy[d]] = n;
st.push((y+dy[d])*(w+2)+x+dx[d]);
}
}
else{
if(n<min)
min = n;
}
}
}
cout<<min<<endl;
}
return 0;
} | C++ | Runtime Efficiency | PIE4PERF_HQTRAIN |
マトリョーシカ
=======

マトリョーシカとは女性像をかたどった木製の人形で、ロシアの代表的な民芸品です。マトリョーシカは、大きな人形の中にそれより小さな人形が入っている入れ子構造になっており、大きさの異なる複数の人形で構成されています。このような入れ子構造にするため、各人形の胴体は上下で分割できる筒状の構造になっています。マトリョーシカは職人の手で手作りされるため、一つ一つの人形は世界に一つしかない非常に貴重なものになります。
兄弟である一郎君と次郎君は、マトリョーシカで遊ぶのが大好きで、各自がそれぞれ1組のマトリョーシカを持っていました。一郎君のマトリョーシカは n 個の人形から構成されており、次郎君のマトリョーシカは m 個の人形から構成されています。
ある日、好奇心が旺盛な一郎君は、これら2組のマトリョーシカに含まれる人形たちを組み合わせて、より多くの人形を含む新たなマトリョーシカを作れないかと考えました。つまり、n + m 個の人形を使い、k 個の人形からなる1組のマトリョーシカを作ることを試みたのです。n と m の大きい方よりも k を大きくすることができれば、一郎君の目的は達成されます。
兄弟は2人仲良く、どのように人形を組み合わせれば k の値を最大にできるかを考えました。しかし、幼い2人にとってこの問題はあまりにも難しいので、年上のあなたはプログラムを作成して弟たちを助けることにしました。
一郎君と次郎君のマトリョーシカの人形の情報を入力とし、新たなマトリョーシカが含む人形の数 k を出力するプログラムを作成して下さい。入力される人形に大きさが同じものは存在しません。また、人形を高さ h 半径 r の円柱とみなした場合、高さh、半径 r の人形が含むことのできる人形は x < h かつ y < r を満たす高さ x 半径 y の人形です。
Input
-----
複数のデータセットの並びが入力として与えられます。入力の終わりはゼロひとつの行で示されます。各データセットは以下の形式で与えられます。
```
n
h1 r1
h2 r2
:
hn rn
m
h1 r1
h2 r2
:
hm rm
```
1行目に一郎君のマトリョーシカの人形の数 n (n ≤ 100)、続く n 行に一郎君の第 i の人形の高さ hi と半径 ri (hi, ri < 1000) が与えられます。
続く行に二郎君のマトリョーシカの人形の数 m (m ≤ 100)、続く m 行に二郎君の第 i の人形の高さ hi と半径 ri (hi, ri < 1000) が与えられます。
データセットの数は 20 を越えない。
Output
------
入力データセットごとに新たなマトリョーシカが含む人形の数 k を出力します。
Sample Input
------------
```
6
1 1
4 3
6 5
8 6
10 10
14 14
5
2 2
5 4
6 6
9 8
15 10
4
1 1
4 3
6 5
8 6
3
2 2
5 4
6 6
4
1 1
4 3
6 5
8 6
4
10 10
12 11
18 15
24 20
0
```
Output for the Sample Input
---------------------------
```
9
6
8
```
| #include<stdio.h>
#include<map>
#include<algorithm>
using namespace std;
typedef pair<int,int> P;
P mt[200];
int dp[1000][201];//hでn個のときのrの最小値
int min(int a,int b){return a<b?a:b;}
int max(int a,int b){return a>b?a:b;}
int main(){
int n,m;
int i,j,k;
while(1){
scanf("%d",&n);
if(n==0)return 0;
for(i=0;i<n;i++){
int a,b;
scanf("%d %d",&a,&b);
mt[i]=P(a,b);
}
scanf("%d",&m);
for(i=0;i<m;i++){
int a,b;
scanf("%d %d",&a,&b);
mt[n+i]=P(a,b);
}
sort(mt,mt+m+n);
for(i=0;i<1000;i++)for(j=0;j<=m+n;j++)dp[i][j]=1000;
for(i=0;i<m+n;i++){
int h=mt[i].first;
int r=mt[i].second;
dp[h][1]=min(dp[h][1],r);
for(j=0;j<h;j++){
for(k=0;k<m+n;k++){
if(r>dp[j][k])dp[h][k+1]=min(dp[h][k+1],r);
}
}
}
int ans=0;
for(i=0;i<1000;i++)for(j=0;j<=m+n;j++){
if(dp[i][j]!=1000)ans=max(ans,j);
}
printf("%d\n",ans);
}
} | #include<stdio.h>
int dp[200];//iが一番外の最大値
int h[200];
int r[200];
int m,n;
int max(int a,int b){return a>b?a:b;}
int dfs(int x){
if(dp[x]!=-1)return dp[x];
int i;
int ans=1;
for(i=0;i<m+n;i++){
if(h[i]<h[x]&&r[i]<r[x])ans=max(ans,dfs(i)+1);
}
return dp[x]=ans;
}
int main(){
while(1){
scanf("%d",&m);
if(m==0)return 0;
int i;
for(i=0;i<m;i++)scanf("%d %d",&h[i],&r[i]);
scanf("%d",&n);
for(i=0;i<n;i++)scanf("%d %d",&h[i+m],&r[i+m]);
int ans=1;
for(i=0;i<m+n;i++)dp[i]=-1;
for(i=0;i<m+n;i++)ans=max(ans,dfs(i));
printf("%d\n",ans);
}
} | C++ | Runtime Efficiency | PIE4PERF_HQTRAIN |
マトリョーシカ
=======

マトリョーシカとは女性像をかたどった木製の人形で、ロシアの代表的な民芸品です。マトリョーシカは、大きな人形の中にそれより小さな人形が入っている入れ子構造になっており、大きさの異なる複数の人形で構成されています。このような入れ子構造にするため、各人形の胴体は上下で分割できる筒状の構造になっています。マトリョーシカは職人の手で手作りされるため、一つ一つの人形は世界に一つしかない非常に貴重なものになります。
兄弟である一郎君と次郎君は、マトリョーシカで遊ぶのが大好きで、各自がそれぞれ1組のマトリョーシカを持っていました。一郎君のマトリョーシカは n 個の人形から構成されており、次郎君のマトリョーシカは m 個の人形から構成されています。
ある日、好奇心が旺盛な一郎君は、これら2組のマトリョーシカに含まれる人形たちを組み合わせて、より多くの人形を含む新たなマトリョーシカを作れないかと考えました。つまり、n + m 個の人形を使い、k 個の人形からなる1組のマトリョーシカを作ることを試みたのです。n と m の大きい方よりも k を大きくすることができれば、一郎君の目的は達成されます。
兄弟は2人仲良く、どのように人形を組み合わせれば k の値を最大にできるかを考えました。しかし、幼い2人にとってこの問題はあまりにも難しいので、年上のあなたはプログラムを作成して弟たちを助けることにしました。
一郎君と次郎君のマトリョーシカの人形の情報を入力とし、新たなマトリョーシカが含む人形の数 k を出力するプログラムを作成して下さい。入力される人形に大きさが同じものは存在しません。また、人形を高さ h 半径 r の円柱とみなした場合、高さh、半径 r の人形が含むことのできる人形は x < h かつ y < r を満たす高さ x 半径 y の人形です。
Input
-----
複数のデータセットの並びが入力として与えられます。入力の終わりはゼロひとつの行で示されます。各データセットは以下の形式で与えられます。
```
n
h1 r1
h2 r2
:
hn rn
m
h1 r1
h2 r2
:
hm rm
```
1行目に一郎君のマトリョーシカの人形の数 n (n ≤ 100)、続く n 行に一郎君の第 i の人形の高さ hi と半径 ri (hi, ri < 1000) が与えられます。
続く行に二郎君のマトリョーシカの人形の数 m (m ≤ 100)、続く m 行に二郎君の第 i の人形の高さ hi と半径 ri (hi, ri < 1000) が与えられます。
データセットの数は 20 を越えない。
Output
------
入力データセットごとに新たなマトリョーシカが含む人形の数 k を出力します。
Sample Input
------------
```
6
1 1
4 3
6 5
8 6
10 10
14 14
5
2 2
5 4
6 6
9 8
15 10
4
1 1
4 3
6 5
8 6
3
2 2
5 4
6 6
4
1 1
4 3
6 5
8 6
4
10 10
12 11
18 15
24 20
0
```
Output for the Sample Input
---------------------------
```
9
6
8
```
| #include <iostream>
#include <vector>
using namespace std;
int main() {
int n;
while ( cin >> n, n ) {
vector<int> h(n), r(n);
for (int i = 0; i < n; ++i) {
cin >> h[i] >> r[i];
}
int M; cin >> M;
h.resize(n+M);
r.resize(n+M);
for (int i = 0; i < M; ++i) {
cin >> h[n+i] >> r[n+i];
}
n += M;
int m[200][200] = {0};
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (h[i] < h[j] && r[i] < r[j]) {
m[i][j] = -1;
}
}
}
int ans = 0;
for (int k = 0; k < n; ++k) for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) {
if (m[i][k] && m[k][j]) {
m[i][j] = min(m[i][j], m[i][k] + m[k][j]);
}
ans = min(ans, m[i][j]);
}
cout << (-ans + 1) << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;
#define EACH(i,a) for (auto& i : a)
#define FOR(i,a,b) for (ll i=(a);i<(b);i++)
#define RFOR(i,a,b) for (ll i=(b)-1;i>=(a);i--)
#define REP(i,n) for (ll i=0;i<(n);i++)
#define RREP(i,n) for (ll i=(n)-1;i>=0;i--)
#define debug(x) cout<<#x<<": "<<x<<endl
#define pb push_back
#define ALL(a) (a).begin(),(a).end()
const ll linf = 1e18;
const int inf = 1e9;
const double eps = 1e-12;
const double pi = acos(-1);
template<typename T>
istream& operator>>(istream& is, vector<T>& vec) {
EACH(x,vec) is >> x;
return is;
}
/*
template<class... T>
ostream& operator<<(ostream& os, tuple<T...>& t) {
for (size_t i = 0; i < tuple_size< tuple<T...> >::value; ++i) {
if (i) os << " ";
os << get<0>(t);
}
return os;
}
*/
template<typename T>
ostream& operator<<(ostream& os, vector<T>& vec) {
REP(i,vec.size()) {
if (i) os << " ";
os << vec[i];
}
return os;
}
template<typename T>
ostream& operator<<(ostream& os, vector< vector<T> >& vec) {
REP(i,vec.size()) {
if (i) os << endl;
os << vec[i];
}
return os;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
int N;
while ( cin >> N, N ) {
vector<P> v;
REP(i, N) {
int h, r; cin >> h >> r;
v.pb( P(h, r) );
}
int M; cin >> M;
REP(i, M) {
int h, r; cin >> h >> r;
v.pb( P(h, r) );
}
sort( ALL(v) );
N += M;
vector< vector<int> > dp(N+1, vector<int>(N+1, -1)); dp[0][0] = 0;
REP(i, N) {
REP(j, N+1) {
if (dp[i][j] < 0) continue;
dp[i+1][j] = max(dp[i+1][j], dp[i][j]);
if (j == 0 || (v[j-1].first < v[i].first && v[j-1].second < v[i].second)) {
dp[i+1][i+1] = max(dp[i+1][i+1], dp[i][j]+1);
}
}
}
int ans = -1;
REP(i, N+1) ans = max(ans, dp[N][i]);
cout << ans << endl;
}
} | C++ | Runtime Efficiency | PIE4PERF_HQTRAIN |
体育祭Sport Meet
=============
秋の体育祭が行われます。種目は徒競走、ボール運び、障害物競走、リレーの4種目です。参加チームは n チームで、この4種目の合計タイムが最も小さいチームが「優勝」、次に小さいチームが「準優勝」、そして、最下位より2番目のチームを「ブービー賞」として表彰したいと思います。
各チームの成績を入力として、「優勝」、「準優勝」、「ブービー賞」のチームを出力するプログラムを作成してください。
ただし、チームにはそれぞれ 1 から n のチーム番号が割り当てられています。
Input
-----
複数のデータセットの並びが入力として与えられます。入力の終わりはゼロひとつの行で示されます。
各データセットは以下の形式で与えられます。
```
n
record1
record2
:
recordn
```
1行目に対象となるチームの数 n (4 ≤ n ≤ 100000)、続く n 行に i 番目のチームの情報が与えられます。各チームの情報は以下の形式で与えられます。
```
id m1 s1 m2 s2 m3 s3 m4 s4
```
id (1 ≤ id ≤ n)はチーム番号、m1, s1 はそれぞれ徒競走のタイムの分と秒、m2, s2 はそれぞれボール運びのタイムの分と秒、m3, s3 はそれぞれ障害物競走のタイムの分と秒、m4, s4 はそれぞれリレーのタイムの分と秒を表します。分と秒はともに0 以上 59 以下の整数です。また、合計タイムが複数のチームで同じになるような入力はないものとします。
Output
------
入力データセットごとに以下の形式でチーム番号を出力します。
1 行目 優勝のチーム番号
2 行目 準優勝のチーム番号
3 行目 ブービー賞のチーム番号
Sample Input
------------
```
8
34001 3 20 3 8 6 27 2 25
20941 3 5 2 41 7 19 2 42
90585 4 8 3 12 6 46 2 34
92201 3 28 2 47 6 37 2 58
10001 3 50 2 42 7 12 2 54
63812 4 11 3 11 6 53 2 22
54092 3 33 2 54 6 18 2 19
25012 3 44 2 58 6 45 2 46
4
1 3 23 1 23 1 34 4 44
2 5 12 2 12 3 41 2 29
3 5 24 1 24 2 0 3 35
4 4 49 2 22 4 41 4 23
0
```
Output for the Sample Input
---------------------------
```
54092
34001
10001
1
3
2
```
| #include<iostream>
#include<algorithm>
using namespace std;
#define MAX 1000000
int n;
pair<int, int> T[MAX];
main(){
int id, m, s;
while( cin >> n && n ){
for ( int i = 0; i < n; i++ ){
cin >> id;
int sum = 0;
for ( int j = 0; j < 4; j++ ){
cin >> m >> s;
sum += m*60 + s;
}
T[i] = make_pair(sum, id);
}
sort( T, T + n );
cout << T[0].second << endl;
cout << T[1].second << endl;
cout << T[n-2].second << endl;
}
} | #include<cstdio>
#define MAX 100000
#define INF (1<<21)
int id[MAX], total[MAX];
int n;
int searchMin(){
int minv = INF;
int target;
for ( int i = 0; i < n; i++ ){
if ( total[i] < 0 ) continue;
if ( total[i] < minv ){
minv = total[i];
target = i;
}
}
total[target] = -1;
return id[target];
}
int searchMax(){
int maxv = 0;
int target;
for ( int i = 0; i < n; i++ ){
if ( total[i] < 0 ) continue;
if ( total[i] > maxv ){
maxv = total[i];
target = i;
}
}
total[target] = -1;
return id[target];
}
main(){
int m, s;
while(1){
scanf("%d", &n);
if ( n == 0 ) break;
for ( int i = 0; i < n; i++ ){
scanf("%d", &id[i]);
total[i] = 0;
for ( int j = 0; j < 4; j++ ){
scanf("%d %d", &m, &s);
total[i] += m*60+s;
}
}
printf("%d\n", searchMin());
printf("%d\n", searchMin());
searchMax();
printf("%d\n", searchMax());
}
} | C++ | Runtime Efficiency | PIE4PERF_HQTRAIN |
観音堂
===
一郎君の家の裏山には観音堂があります。この観音堂まではふもとから 30 段の階段があり、一郎君は、毎日のように観音堂まで遊びに行きます。一郎君は階段を1足で3段まで上がることができます。遊んでいるうちに階段の上り方の種類(段の飛ばし方の個数)が非常にたくさんあることに気がつきました。
そこで、一日に 10 種類の上り方をし、すべての上り方を試そうと考えました。しかし数学を熟知しているあなたはそんなことでは一郎君の寿命が尽きてしまうことを知っているはずです。
一郎君の計画が実現不可能であることを一郎君に納得させるために、階段の段数 n を入力とし、一日に 10 種類の上り方をするとして、一郎君がすべての上り方を実行するのに要する年数を出力するプログラムを作成してください。一年は 365 日として計算してください。一日でも必要なら一年とします。365 日なら 1 年であり、366 日なら 2 年となります。
Input
-----
複数のデータセットの並びが入力として与えられます。入力の終わりはゼロひとつの行で示されます。
各データセットとして、段数を表す1つの整数 n (1 ≤ n ≤ 30) が1行に与えられます。
データセットの数は 30 を超えません。
Output
------
データセットごとに一郎君がすべての上り方を実行するのに必要な年数(整数)を1行に出力します。
Sample Input
------------
```
1
10
20
25
0
```
Output for the Sample Input
---------------------------
```
1
1
34
701
```
| #include<iostream>
using namespace std;
class Answer
{
public:
Answer(){day = 10; year = 365;}
int N; //??\???
int day;
int year;
unsigned long long count(int cnt); //??¨??¢?´¢(???)
void set(); //??\???
void OutPut(); //???????????????
bool empty(); //???????????¶?????????
bool ans(); //?§£???
};
unsigned long long Answer::count(int cnt)
{
if( cnt < 0)
return 0;
else if(cnt == 0)
return 1;
return count(cnt - 1) + count(cnt - 2) + count(cnt - 3);
}
void Answer::OutPut()
{
if( N == 0 )
return;
int ans_ = 1; //??????????????´???
ans_ += (count( N )) / (day * year); //??\???
cout << ans_ << endl;;
}
void Answer::set()
{
cin >> N;
}
bool Answer::empty()
{
if( N == 0 )return false;
else return true;
}
bool Answer::ans()
{
set();
OutPut();
return empty();
}
int main()
{
Answer ans;
while( ans.ans() );
return 0;
} | #include<iostream>
using namespace std;
#define MEMO_MAX 100
typedef unsigned long long ULL;
bool Flag_memo_g = true; //?????¢??????????????????
class Answer
{
public:
Answer()
{
day = 10; year = 365;
for(int i = 0; i < MEMO_MAX; i++)
{
memo[i] = 0;
Flag_memo[i] = false;
}
}
int N; //??\???
int day;
int year;
ULL memo[MEMO_MAX];
bool Flag_memo[MEMO_MAX];
ULL count(int cnt); //??¨??¢?´¢(??±???)
ULL count_memo(int cnt); //??¨??¢?´¢(??±???)(?????¢???ver.)
void set(); //??\???
void OutPut(); //???????????????
bool empty(); //???????????¶?????????
bool ans(); //?§£???
};
ULL Answer::count(int cnt)
{
if( cnt < 0 )
return 0;
else if( cnt == 0 )
return 1;
return count(cnt - 1) + count(cnt - 2) + count(cnt - 3);
}
ULL Answer::count_memo(int cnt)
{
if( cnt < 0 )return 0;
else if( cnt == 0 )return 1;
if( !(Flag_memo[cnt]) )
{
memo[cnt] = count_memo(cnt - 1) + count_memo(cnt - 2) + count_memo(cnt - 3);
Flag_memo[cnt] = true;
}
return memo[cnt];
}
void Answer::OutPut()
{
if( N == 0 )
return;
int ans_ = 1; //??????????????´???
if( Flag_memo_g )
ans_ += count_memo( N ) / (day * year);
else if( !Flag_memo_g )
ans_ += (count( N )) / (day * year); //??\???
cout << ans_ << endl;
}
void Answer::set()
{
cin >> N;
}
bool Answer::empty()
{
if( N == 0 )return false;
else return true;
}
bool Answer::ans()
{
set();
OutPut();
return empty();
}
int main()
{
Answer ans;
while( ans.ans() );
return 0;
} | C++ | Runtime Efficiency | PIE4PERF_HQTRAIN |
観音堂
===
一郎君の家の裏山には観音堂があります。この観音堂まではふもとから 30 段の階段があり、一郎君は、毎日のように観音堂まで遊びに行きます。一郎君は階段を1足で3段まで上がることができます。遊んでいるうちに階段の上り方の種類(段の飛ばし方の個数)が非常にたくさんあることに気がつきました。
そこで、一日に 10 種類の上り方をし、すべての上り方を試そうと考えました。しかし数学を熟知しているあなたはそんなことでは一郎君の寿命が尽きてしまうことを知っているはずです。
一郎君の計画が実現不可能であることを一郎君に納得させるために、階段の段数 n を入力とし、一日に 10 種類の上り方をするとして、一郎君がすべての上り方を実行するのに要する年数を出力するプログラムを作成してください。一年は 365 日として計算してください。一日でも必要なら一年とします。365 日なら 1 年であり、366 日なら 2 年となります。
Input
-----
複数のデータセットの並びが入力として与えられます。入力の終わりはゼロひとつの行で示されます。
各データセットとして、段数を表す1つの整数 n (1 ≤ n ≤ 30) が1行に与えられます。
データセットの数は 30 を超えません。
Output
------
データセットごとに一郎君がすべての上り方を実行するのに必要な年数(整数)を1行に出力します。
Sample Input
------------
```
1
10
20
25
0
```
Output for the Sample Input
---------------------------
```
1
1
34
701
```
| #include <iostream>
#include <string>
#include <algorithm>
#include <stack>
#include <queue>
using namespace std;
int func(int kaidan,int now){
int a=0,b=0,c=0;
if(now==kaidan){
return 1;
}else if(now>kaidan){
return 0;
}else{
a+=func(kaidan,now+1);
b+=func(kaidan,now+2);
c+=func(kaidan,now+3);
}
return a+b+c;
}
int main(){
int n,ans;
while(cin>>n){
if(n==0)break;
ans=func(n,0);
cout<<ans/10/365+1<<endl;
}
return 0;
} | #include <iostream>
using namespace std;
int memo[31];
int main(){
int n,ans;
memo[0]=1;
memo[1]=1;
memo[2]=2;
for(int i=3;i<31;i++){
memo[i]=memo[i-1]+memo[i-2]+memo[i-3];
}
while(cin>>n){
if(n==0)break;
cout<<memo[n]/(365*10)+1<<endl;
}
return 0;
} | C++ | Runtime Efficiency | PIE4PERF_HQTRAIN |
観音堂
===
一郎君の家の裏山には観音堂があります。この観音堂まではふもとから 30 段の階段があり、一郎君は、毎日のように観音堂まで遊びに行きます。一郎君は階段を1足で3段まで上がることができます。遊んでいるうちに階段の上り方の種類(段の飛ばし方の個数)が非常にたくさんあることに気がつきました。
そこで、一日に 10 種類の上り方をし、すべての上り方を試そうと考えました。しかし数学を熟知しているあなたはそんなことでは一郎君の寿命が尽きてしまうことを知っているはずです。
一郎君の計画が実現不可能であることを一郎君に納得させるために、階段の段数 n を入力とし、一日に 10 種類の上り方をするとして、一郎君がすべての上り方を実行するのに要する年数を出力するプログラムを作成してください。一年は 365 日として計算してください。一日でも必要なら一年とします。365 日なら 1 年であり、366 日なら 2 年となります。
Input
-----
複数のデータセットの並びが入力として与えられます。入力の終わりはゼロひとつの行で示されます。
各データセットとして、段数を表す1つの整数 n (1 ≤ n ≤ 30) が1行に与えられます。
データセットの数は 30 を超えません。
Output
------
データセットごとに一郎君がすべての上り方を実行するのに必要な年数(整数)を1行に出力します。
Sample Input
------------
```
1
10
20
25
0
```
Output for the Sample Input
---------------------------
```
1
1
34
701
```
| #include <iostream>
using namespace std;
int n;
int saiki(int ima)
{
if(ima == n)
return 1;
if(ima > n)
return 0;
int res = 0;
res += saiki(ima + 1) + saiki(ima + 2) + saiki(ima + 3);
return res;
}
int main() {
while (true){
cin >> n;
if (n == 0)
break;
int ans,a;
ans = saiki(0);
a = ans;
ans /= 3650;
a %= 3650;
if( a == 0 )
{
cout << ans << endl;
}
else if( a != 0 )
{
ans++;
cout << ans << endl;
}
}
} | #include <iostream>
using namespace std;
int main()
{
int n, cnt, y;
int c[31];
c[0] = 1;
c[1] = 1;
c[2] = 2;
for( int i=3; i<=30; i++ )
c[i] = c[i-1] + c[i-2] + c[i-3];
while( cin >> n, n != 0 ){
y = c[n]/(365*10);
y += !( c[n]%(365*10) == 0 );
cout << y << endl;
}
return 0;
} | C++ | Runtime Efficiency | PIE4PERF_HQTRAIN |
End of preview. Expand in Data Studio
README.md exists but content is empty.
- Downloads last month
- 3