A.添加逗号
题目描述
输入描述:
一行一个整数 N
输出描述:
一行一个字符串表示添加完逗号的结果
思路:一眼签到题 直接上代码
#include <iostream>
#include <cstring>
#include <set>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 1e6+11;
int prime[maxn],cnt;
bool is_prime[maxn];
int main(){
ll n;
char str[15];
char ans[20];
while(cin>>str){
int len = strlen(str);
int cnt = 0;
int a_c = 0;
for(int i=len-1;i>=0;i--){
ans[a_c++] = str[i];
cnt++;
if(cnt==3){
if(i==0) break;
ans[a_c++] = ',';
cnt = 0;
}
}
for(int i=a_c-1;i>=0;i--) cout<<ans[i];
cout<<endl;
}
}
B.对称
题目描述
输入描述:
一行两个整数 N,M
输出描述:
一行一个整数,即需要的棋子数
思路:只有n和m是奇数时才能分成4部分.
比如样例 n=7 m=15
第一次 : n%2==1 m%2==1 时可以拆的 ,这次放置了1个棋子
第二次: (n/2)%2==1 (m/2)%2==1 也满足,这次放了4个棋子
第三次 ....... 这次放了16个棋子
你会发现 其实是个等比数列,公比是4
只要我们求出 n(即能拆多少次)就能求出等比数列和
不过这里注意一个坑点,就是当n==1||m==1时 ans=1;
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <set>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
typedef unsigned long long ll;
const int maxn = 1e6+11;
int prime[maxn],cnt;
bool is_prime[maxn];
struct qwe{
int h,m,s;
}q[5015];
ll q_mod(ll x,ll n){
ll res = 1;
while(n){
if(n&1) res =res*x;
x = x*x;
n>>=1;
}
return res;
}
int main(){
ll n,m;
while(scanf("%lld%lld",&n,&m)!=EOF){
ll cnt = 0;
ll ans = 0;
if((n==1&&m!=1)||(n!=1&&m==1)){
cout<<"1"<<endl;
}else{
while((n%2==1)&&(m%2==1)){
cnt++;
if(n==1||m==1) break;
n/=2;
m/=2;
}
ans += (q_mod(4,cnt)-1)/3;
printf("%lld\n",ans);
}
}
return 0;
}
C.竞赛技巧
题目描述
输入描述:
第 1 行,一个整数 N 第 2~n+1 行,每行 3 个整数,表示时,分,秒
输出描述:
共 n 行,每行 3 个整数,表示排序完后的结果
思路:写个排序函数 直接sort
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <set>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 1e6+11;
int prime[maxn],cnt;
bool is_prime[maxn];
struct qwe{
int h,m,s;
}q[5015];
int cmp(qwe a,qwe b){
if(a.h!=b.h){
return a.h<b.h;
}
else if(a.m!=b.m){
return a.m<b.m;
}
else return a.s<b.s;
}
int main(){
int n;
while(scanf("%d",&n)!=EOF){
for(int i=0;i<n;i++){
scanf("%d%d%d",&q[i].h,&q[i].m,&q[i].s);
}
sort(q,q+n,cmp);
for(int i=0;i<n;i++){
printf("%d %d %d\n",q[i].h,q[i].m,q[i].s);
}
}
return 0;
}
D.训练技巧
题目描述
输入描述:
第一行:两个用空格隔开的整数:N和K,1≤N≤100000,1≤K≤N 第二行到N+1行:第i+1行有一个整数,表示第N天的训练效果是Ei,(0 <= Ei <= 1,000,000,000)
输出描述:
第一行:单个整数,表示最大的能力之和
思路:这应该是10道题里最好得一题 (大佬勿喷)
题目要求我们求最大得训练效果,我们转换下只要求出最小得损失效果,然后用总效果减去即可
怎么求最小损失效果?
我们用dp[i]表示在第i天休息得情况下前i天损失得最小效果,很容易想到dp方程为dp[i]=min(dp[j])+a[i] (j是从i-k-->i-1)
那么这样复杂度就是O(n*k)了
那么我们可以用递增得优先队列维护下[i-k,i-1]内得最小值即可。
#include <bits/stdc++.h>
#include <cstdio>
#include <cmath>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
typedef long long ll;
const int maxn = 100005;
const ll mod = 1e9+7;
const ll INF = 1e18;
const double eps = 1e-9;
int n,k;
int a[maxn];
ll dp[maxn];
struct qwe{
ll x;
int i;
bool operator < (const qwe& a)const{
return x>a.x;
}
};
int main()
{
while(scanf("%d%d",&n,&k)!=EOF)
{
priority_queue<qwe>q;
qwe tp,num;
mst(dp,0);
ll sum=0;
for(int i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
sum+=a[i];
}
for(int i=1;i<=n;i++)
{
if(k+1>=i){
dp[i] = a[i];
num.x=a[i];
}else{
while(q.top().i<(i-k-1)) q.pop();
if(q.top().i>=i-k-1){
dp[i] = q.top().x+a[i];
}
num.x=dp[i];
}
//cout<<dp[i]<<endl;
num.i=i;
q.push(num);
}
ll ans=INF;
for(int i=n-k;i<=n;i++) //求出损失的最小训练效果
{
ans=min(ans,dp[i]);
}
printf("%lld\n",sum-ans);
}
return 0;
}
E.这是道数学题
题目描述
输入描述:
第一行输入四个数 n,A0,An,Q
输出描述:
对于每组查询输出Al到Ar的和
思路:
化简一下得:Ai=Ao*C(n-1,i)/C(n,i)+An*C(n-1,i-1)/C(n,i);
再简一下得 Ai = Ao*(n-i)/n+An*i/n ps:为什么?自己再纸上搞搞吧
设组数据看下 例如 n=3
A1 = Ao*2/3+An*1/3
A2 = Ao*1/3+An*2/3
所以 Ao+A1+A2+A3 = Ao*(3/3+2/3+1/3)+An*(1/3+2/3+3/3);可以发现分子和是等差和
那么根据给定得区间以及公式即可求出答案
#include<iostream>
#include<cstdlib>
using namespace std;
typedef long long ll;
int main(){
ll n,Ao,An,q;
//cin >> n >> Ao >> An >> q;
scanf("%lld%lld%lld%lld",&n,&Ao,&An,&q);
for(int i = 1 ; i <=q ;i++)
{
double l,r,a,b;
scanf("%lf%lf",&l,&r);
a = (2*n-l-r)*(r-l+1)*1.0/2.0;
b = (l+r)*(r-l+1)*1.0/2.0;
double s = (a*Ao+b*An)*1.0/n*1.0;
printf("%.0f\n",s);
}
return 0;
}
G 零下e度
来源:牛客网
题目描述
输入描述:
一行一个整数n 1<=n<=10^8
输出描述:
一行一个整数,即题目描述中所求,由于这个数字可能很大,我们只需要知道mod 998244353后的结果(出题人负责任地告诉你,这个数字是个质数)
思路 :
后面才知道 原来是错排公式 (太弱了 好好补充下数论知识才行
#include <iostream>
using namespace std;
const int mod = 998244353;
typedef long long ll;
int main(){
ll n ;
cin>>n;
ll ans;
if(n==1) ans=1;
if(n==2) ans=2;
ll x=0,y=1;
for(int i=3;i<=n;i++){
ll tem = (x+y)*(i-1)%mod;
x=y;y=tem;
}
if(n>2)
ans = y;
cout<<ans<<endl;
return 0;
}
J同分异构体
这题没什么好说,签到题,数据n<=9 直接推出前9种即可
#include <iostream>
#include <cstring>
#include <set>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 1e6+11;
int prime[maxn],cnt;
bool is_prime[maxn];
int main(){
int n;
while(cin>>n){
if(n<=3) cout<<"1"<<endl;
else if(n==4){
cout<<"2"<<endl;
}
else if(n==5){
cout<<"3"<<endl;
}
else if(n==6){
cout<<"5"<<endl;
}
else if(n==7){
cout<<"9"<<endl;
}
else if(n==8){
cout<<"18"<<endl;
}
else cout<<"35"<<endl;
}
return 0;
}