#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<functional>
#include<cmath>
using namespace std;
#define MAXN (10000+10)
int n,a[MAXN];
int f[MAXN][2][2]={0}; //Middle and 1
int main()
{
cin>>n;
for (int i=1;i<=n;i++) cin>>a[i];
switch (a[1])
{
case 2:f[1][1][1]++;break;
case 0:f[1][0][0]++;break;
default:f[1][0][1]=f[1][1][0]=1;
}
for (int i=2;i<=n;i++)
{
switch (a[i])
{
case 3:f[i][1][1]=f[i-1][1][1];break;
case 0:f[i][0][0]=f[i-1][0][0];break;
case 2:f[i][1][0]=f[i-1][1][1];f[i][0][1]=f[i-1][1][0];f[i][1][1]=f[i-1][0][1];break;
case 1:f[i][1][0]=f[i-1][0][1];f[i][0][1]=f[i-1][0][0];f[i][0][0]=f[i-1][1][0];break;
}
}
cout<<f[n][1][0]+f[n][0][0]<<endl;
return 0;
}
#include<cstdio>
#include<cstdlib>
#include<iostream>
using namespace std;
#define MAXN (20+10)
int n;
long long f[MAXN]={0,0,1};
int main()
{
for (int i=3;i<=20;i++) f[i]=(i-1)*f[i-1]+f[i-2]*(i-1);
while (cin>>n) cout<<f[n]<<endl;
}
数列 n 有 a1, a2, ..., an 个数(严格递增),请从中任意删去一些数,使序列相邻2数都不互质。
问删后序列最长长度.
Input
第一行 n (1 ≤ n ≤ 105)
第二行序列 a1, a2, ..., an (1 ≤ ai ≤ 105; ai < ai + 1).
Output
删后序列最长长度.
Sample test(s)
input
5
2 3 4 6 9
output
4
input
9
1 2 3 5 6 7 8 9 10
output
4
Note
In the first example, the following sequences are examples of good sequences: [2; 4; 6; 9], [2; 4; 6], [3; 9], [6]. The length of the longest good sequence is 4.
错解:枚举开头即可。X
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<functional>
#include<algorithm>
#include<cctype>
using namespace std;
#define MAXN (100000+10)
int n,a[MAXN];
bool b[MAXN]={0};
int gcd(int a,int b){return (b==0)?a:gcd(b,a%b);};
int main()
{
scanf("%d",&n);for (int i=1;i<=n;i++) scanf("%d",&a[i]);
int ans=0;
for (int i=1;i<=n;i++)
if (!b[i])
{
int len=1;
b[i]=1;
int head=i,tail=i+1;
while (tail<=n)
{
if (gcd(a[tail],a[head])==1) tail++;
else {b[head]=1;head=tail;tail++;len++; }
}
ans=max(ans,len);
}
cout<<ans<<endl;
return 0;
}
Squirrel Liss lived in a forest peacefully, but unexpected trouble happens. Stones fall from a mountain. Initially Squirrel Liss occupies an interval [0, 1]. Next, n stones
will fall and Liss will escape from the stones. The stones are numbered from 1 to n in order.
The stones always fall to the center of Liss's interval. When Liss occupies the interval [k - d, k + d] and a stone falls to k,
she will escape to the left or to the right. If she escapes to the left, her new interval will be [k - d, k]. If she escapes to the right,
her new interval will be [k, k + d].
You are given a string s of length n. If the i-th
character of s is "l" or "r",
when the i-th stone falls Liss will escape to the left or to the right, respectively. Find the sequence of stones' numbers from left to right after all
the n stones falls.
Input
The input consists of only one line. The only line contains the string s (1 ≤ |s| ≤ 106).
Each character in s will be either "l" or "r".
Output
Output n lines — on the i-th line you should print
the i-th stone's number from the left.
Sample test(s)
input
llrlr
output
3
5
4
2
1
input
rrlll
output
1
2
5
4
3
input
lrlrr
output
2
4
5
3
1
Note
In the first example, the positions of stones 1, 2, 3, 4, 5 will be , respectively. So you should print the sequence: 3, 5, 4, 2, 1.
不能用模拟double+除法,会爆精度啊!!(long double 也不行)
其实只要根据性质,在序列前后添加即可。
靠,人生中的处女Hack,竟然是被Hack…(受?)
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<functional>
#include<algorithm>
#include<cctype>
using namespace std;
#define MAXN (1000000+10)
//pair<double,int> a[MAXN];
char s[MAXN];
int n,a[MAXN];
int main()
{
scanf("%s",&s);
n=strlen(s);
int l=1,r=n;
for (int i=0;i<n;i++)
{
if (s[i]=='l') a[r--]=i+1;
else a[l++]=i+1;
}
for (int i=1;i<=n;i++) cout<<a[i]<<endl;
}