Problem 1: 斐波那契
时间限制: 1.00s
内存限制: 500.00MB
题目描述:
输入格式:
输出格式:
输入输出样例
输入
666 10086
输出
7126
说明/提示
思路:
这不是sb题吗?,开个玩笑,活跃一下气氛!
这道题我们可用三数组来进行迭代递推,
然后每次做运算时要%k,没了!
注意一点:可能
k
=
1
k=1
k=1,so…,
当是求
F
1
F_1
F1时候,我们也不要忘了来个%k
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#define fre(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout);
#define ll long long
using namespace std;
const int MAX=2147483647;
const int N=1e6;
ll n, k, f[5];
int main()
{
//fre();
scanf("%lld%lld", &n, &k);
f[0]=0, f[1]=1;
if(n == 1)
{printf("%lld", f[1] % k); return 0;}
for(int i = 2; i <= n; i++)
{
f[2] = (f[0] % k + f[1] % k) % k;
f[0] = f[1], f[1]= f[2];
}
printf("%lld", f[2] % k);
return 0;
}
Problem 2: 复习大战
时间限制: 1.00s
内存限制: 500.00MB
题目描述:
输入格式
输出格式
输入输出样例
输入
10
Chinese
chinese
Maths
Maths
Chinese
History
Oi
OI
oI
oi
输出
Chinese 2
History 1
Maths 2
OI 1
Oi 1
chinese 1
oI 1
oi 1
说明/提示
数据规模与约定
提示
思路:
这道题连我多会做,随便逼逼吧!
首先用sort根据字典序将多段字符串按升序排序,
(sort是个好东西,大佬还用map?)
那我们只用算出有多少个连续相同的字符串,
输出这个串及连续个数,重复以上操作就欧了!
#include<algorithm>
#include<iostream>
#include<cstdio>
using namespace std;
int n,o;
string s[1005];
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)cin>>s[i];
sort(s+1,s+n+1);
o=1;
for(int i=1;i<=n;i++)
if(s[i]!=s[i+1])cout<<s[i]<<' '<<o<<endl,o=1;
else o++;
return 0;
}
Problem 3: 数位问题
时间限制: 1.00s
内存限制: 500.00MB
题目描述:
输入格式:
输出格式:
输入输出样例
输入
20
0 0 2 0 1 0 0 0 0
0 0 1 2 0 0 0 0 0
0 0 0 0 1 1 0 0 0
3 0 2 1 0 1 0 0 0
3 4 0 0 0 0 0 0 0
5 1 0 0 0 0 1 0 0
15 0 0 0 1 0 0 0 0
14 1 1 0 0 0 0 0 0
15 1 0 0 0 0 0 0 0
97 0 1 0 0 0 1 0 0
91 2 0 0 0 0 0 0 0
97 2 0 1 0 0 0 0 0
99 0 0 0 0 0 1 0 0
94 5 1 0 0 0 0 0 0
97 1 0 0 0 0 0 0 0
95 5 0 0 0 0 0 0 0
5 92 2 1 0 0 0 0 0
96 1 3 0 0 0 0 0 0
96 2 2 0 0 0 0 0 0
96 4 0 0 0 0 0 0 0
输出
5
5
3
9
13
11
19
23
25
101
101
105
105
103
107
105
101
103
100
100
说明/提示
思路:
看看数据,不是很大,暴搜油然而生!
前置知识,对于一个数x,我们判断11
的方法为:将x的奇数位与偶数位求
个和,然后判断
t
o
t
j
(
奇
数
位
和
)
−
t
o
t
o
(
偶
数
位
和
)
tot_j(奇数位和) - tot_o(偶数位和)
totj(奇数位和)−toto(偶数位和)
是否是11的倍数,就可以判断一个大数x是不是11的
倍数了。那么,再看回本题,也就是说题目中的若干个0
只是为了将一位数从当奇数位数或偶数位数的隔板(这里)。知道这个后,我们就开始暴搜了。
大家好好理解
定义dfs(int x, int tot_j, int tot_o, int cnt_j, int cnt_o),
x
表
示
i
(
看
题
意
)
,
t
o
t
j
/
o
表
示
奇
/
偶
数
位
的
和
,
x表示i(看题意),tot_{j/o}表示奇/偶数位的和,
x表示i(看题意),totj/o表示奇/偶数位的和,
c
n
t
j
/
o
表
示
奇
/
偶
位
的
所
选
了
多
少
个
数
cnt_{j/o}表示奇/偶位的所选了多少个数
cntj/o表示奇/偶位的所选了多少个数
那dfs的过程想必大家应该都到了,就是一个选数吗?
for(int i = 0; i <= d[x]; i++)
{
tot_j = tot_j + i * x, tot_o = tot_o + (d[x] - i) * x;
dfs(x + 1, tot_j, tot_o, cnt_j + i, cnt_o + (d[x] - i));
tot_j = tot_j - i * x, tot_o = tot_o - (d[x] - i) * x;
}
那最后选完后,如何考虑需要多少个0呢?
其实很简单,我们只用考虑奇偶位各选的个数,
算出他们的差,然后差多少就补多少个0就行了!
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#define fre(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout);
#define ll long long
using namespace std;
const int MAX=2147483647;
const int N=1e6;
int t, d[30], ans;
void dfs(int x, int tot_j, int tot_o, int cnt_j, int cnt_o)
{
if(x > 9)
{
if(!(abs(tot_j - tot_o) % 11))
{
int temp = cnt_j + cnt_o;
if(cnt_j == cnt_o) ans=min(ans, temp);
else ans=min(ans, temp+ abs(cnt_j - cnt_o) - 1);
}
return;
}
for(int i = 0; i <= d[x]; i++)
{
tot_j = tot_j + i * x, tot_o = tot_o + (d[x] - i) * x;
dfs(x + 1, tot_j, tot_o, cnt_j + i, cnt_o + (d[x] - i));
tot_j = tot_j - i * x, tot_o = tot_o - (d[x] - i) * x;
}
}
int main()
{
//fre();
scanf("%d", &t);
while(t--)
{
bool flag=0; ans=MAX;
for(int i = 1; i <= 9; i++) scanf("%d", &d[i]);
dfs(1, 0, 0, 0, 0);
if(ans == MAX) printf("-1\n");
else printf("%d\n", ans);
}
return 0;
}