

直接模擬即可,注意特判
#include<iostream>
#include<cstdio>
#include<ctime>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#define maxn 200010
#define LL long long
using namespace std;
int main()
{
int a,b;
cin>>a>>b;
if(a==1){
cout<<1;return 0;
}
if(b==1){
cout<<a;return 0;
}
LL ans=1;
while(b&&ans<=1e9){
ans*=a;
b--;
}
if(ans>1e9){
cout<<-1;return 0;
}
cout<<ans;
return 0;
}

其實(shí)就是數(shù)學(xué)題,解方程而已,也可以二分因?yàn)閜<q
#include<iostream>
#include<cstdio>
#include<ctime>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#define maxn 200010
#define LL long long
using namespace std;
//這道題可以用二分,也可以直接帶進(jìn)去式子算(一元二次方程 帶公式,看算的出來(lái)不)
int main()
{
LL k,n,e,d,p,q;
cin>>k;
while(k--){
cin>>n>>e>>d;
LL delta=sqrt((n-e*d+2)*(n-e*d+2)-4*n);
LL b=n-e*d+2;
LL p=(b+delta)/2;
LL q=b-p;
if(p*q==n&&e*d==(p-1)*(q-1)+1&&p&&q){
cout<<min(p,q)<<" "<<max(p,q)<<endl;
}
else cout<<"NO"<<endl;
}
return 0;
}

用棧把中綴轉(zhuǎn)后綴表達(dá)式,在加上一些巧妙地操作。。。
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<stack>
#include<cstdio>
#include<queue>
#include<map>
#include<ctime>
#include<vector>
#include<set>
using namespace std;
typedef long long LL;
const int maxn=1e6+10;
using namespace std;
//表達(dá)式求值:后綴表達(dá)式
//但是有其他巧妙地操作:統(tǒng)計(jì)略過(guò)的計(jì)算次數(shù) (短路)
stack<int> st;
stack<pair<int,int> > duan; //短路或、與的次數(shù)
string s;
stack<char> op;
map<char,int> mp; //運(yùn)算符優(yōu)先級(jí)的
void merg_duan(pair<int,int> aa,pair<int,int> bb,int a,int b,char p){
//如果 a|b a為1,那么或短路次數(shù)為a.first+1 與為aa.second
//如果a為0 那么就是aa+bb
pair<int,int> c;
if(a==1&&p=='|'){
c.first=aa.first+1;
c.second=aa.second;
}
//如果 a&b a為0,那么或次數(shù)不變,與次數(shù)+1
else if(a==0&&p=='&'){
c=aa;
c.second++;
}
else{
c.first=aa.first+bb.first;
c.second=aa.second+bb.second;
}
duan.push(c); //把新的短路次數(shù)也加入
}
void calc(){
int b=st.top();st.pop();
int a=st.top();st.pop();
int num=0;
pair<int,int> bb=duan.top();duan.pop();
pair<int,int> aa=duan.top();duan.pop();
merg_duan(aa,bb,a,b,op.top());
if(op.top()=='|') num=a|b;
else if(op.top()=='&') num=a&b;
st.push(num);
op.pop();
return;
}
void solve(){
int len=s.length();
for(int i=0;i<len;i++){
if(isdigit(s[i])) st.push(s[i]-'0'),duan.push({0,0});
//遇到數(shù)字,就入棧,數(shù)字和短路次數(shù)
else{
if(s[i]=='(') op.push('(');
if(s[i]==')'){
while(!op.empty()&&op.top()!='(') calc();
if(!op.empty()) op.pop(); //把(彈出去
}
if(s[i]!='('&&s[i]!=')'){
//如果遇到運(yùn)算符,就先把棧里面優(yōu)先級(jí)高的都彈出
while(!op.empty()&&mp[op.top()]>=mp[s[i]]) calc();
op.push(s[i]);
}
}
}
while(!op.empty()){
calc();
}
cout<<st.top()<<endl<<duan.top().second<<" "<<duan.top().first<<endl;
}
int main(){
cin>>s;
mp['&']=2;
mp['|']=1;
solve();
return 0;
}

典型的dp
f[i][j]表示走到i,用了插入的j個(gè)點(diǎn)能取到的最大值
#include <bits/stdc++.h>
using namespace std;
const int maxn=505;
int f[maxn][maxn]; //f[i][j]: 前 i 個(gè)點(diǎn),插入了 j 個(gè)點(diǎn)后最大長(zhǎng)度
pair<int,int> a[maxn];
int main()
{
int n,k;
cin>>n>>k;
for(int i=1;i<=n;i++) cin>>a[i].first>>a[i].second;
sort(a+1,a+1+n);
for(int i=1;i<=n;i++){
for(int j=0;j<=k;j++) f[i][j]=1+j;
}
//最長(zhǎng)上升子序列
for(int i=2;i<=n;i++){
for(int j=i-1;j>=1;j--){
if(a[j].second>a[i].second) continue;
//從j到i要插入d個(gè)點(diǎn)
int d=a[i].first-a[j].first+a[i].second-a[j].second-1;
for(int p=d;p<=k;p++) f[i][p]=max(f[i][p],f[j][p-d]+1+d);
}
}
int ans=0;
for(int i=1;i<=n;i++) ans=max(ans,f[i][k]);
cout<<ans<<endl;
return 0;
}
posted on
浙公網(wǎng)安備 33010602011771號(hào)