October 24, 2020 Popularity Group (luogu team)

Problem 1: Fibonacci

Time
limit: 1.00s Memory limit: 500.00MB


Title description:
Insert picture description here

Input format:

Insert picture description here

Output format:
Insert picture description here

Input and output sample
input

666 10086

Output

7126

Instructions/tips
Insert picture description here

Idea:
Isn't this an sb question? , Just kidding, enliven the atmosphere!
For this problem, we can use three arrays to perform iterative recursion,
and then we need %k every time we do an operation, no more!
Note that: maybe k = 1 k=1k=1 , so...,
when it isF 1 F_1F1Time, let’s not forget to come to %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: Review the battle

Time
limit: 1.00s Memory limit: 500.00MB


Title description:
Insert picture description here
input format
Insert picture description here

Output format
Insert picture description here

Input and output sample
input

10
Chinese
chinese
Maths
Maths
Chinese
History
Oi
OI
oI
oi

Output

Chinese 2
History 1
Maths 2
OI 1
Oi 1
chinese 1
oI 1
oi 1

Description/Tips
Data scale and convention

Insert picture description here

prompt
Insert picture description here

Idea:
I know how to do this question. Feel free to force it!
First use sort to sort multiple strings in ascending order according to lexicographical order,
( sort is a good thing, and the big guys still use map? )
Then we only need to calculate how many consecutive identical strings,
output this string and the number of consecutive, repeat The above operation is ok!

#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: Digital problem

Time
limit: 1.00s Memory limit: 500.00MB


Title description:
Insert picture description here
Input format:
Insert picture description here
Output format:
Insert picture description here

Input and output sample
input

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

Output

5
5
3
9
13
11
19
23
25
101
101
105
105
103
107
105
101
103
100
100

Instructions/tips
Insert picture description here

Idea:
Look at the data, it's not very big, and the search spontaneously!
Pre-knowledge, for a number x, the way we judge 11
is: sum the odd and even bits of x
, and then judge totj (sum of odd bits) − toto (sum of even bits) tot_j (sum of odd bits)- tot_o (even bit sum)t o tj( Odd number of bits and )t o tthe( Even- number bits and )
whether it is a multiple of 11, based on a large number of 11 x is not
a multiple of. Then, look back to this question again, that is to say, the number of zeros in the question is
just to remove a single digit from an odd or even digit partition (here
everyone understandsitwell
). After knowing this, we started a raid.
Define dfs(int x, int tot_j, int tot_o, int cnt_j, int cnt_o),
x means i (see the meaning of the question), totj / o means the sum of odd/even digits, x means i (see the meaning of the question), tot_{ j/o} represents the sum of odd/even digits,x Table shows I ( see title meaning ) , T O Tj/oTable shows the odd / even number of bits in and ,
CNT- / O represents an odd / even bit by how many cnt_ {j / o} represents the number of the selected number of odd / even bit of the selectedcntj/oTable shows the odd / even bits of the election of more than a small one number
that dfs process surely we should have to, that is a number you choose?

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;
	}

After the final selection, how to consider how many 0s are needed?
It's actually very simple. We only need to consider the number of odd and even bits selected,
calculate their difference, and then fill in as many 0s as the difference is!

#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;
}

Guess you like

Origin blog.csdn.net/bigwinner888/article/details/109273593