知识点:组合数学
题解:
用总体减不满足的,m行n列有一共有s1 = (m+1)(n+1)个交点,所以总体是C(s1,3).不能组成三角形的情况就是三点共线。水平||竖直情况s2 = C(n+1,3)m+C(m+1,3)n;斜着只看斜率小于0的即可,大于0是对称的。设A(0,0), B(i,j) 显然,只有2<=i<=m, 2<=j<=n,线段AB才是一条斜线。斜边可以选的点为gcd(i,j)+1。第三个点有gcd(i,j)-1个位置.接下来平移这条线段(三个点),往下可以移动最多(m-i+1)个点,往右最多可以移动(n-j+1)个点,所以斜边的情况是s3 = (gcd(i,j)-1)(m-i+1)(n-j+1).另一种斜边是完全对称的,最后的答案即s1-s2-2s3;
I 前缀和
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll mod=1000000007;
ll a[2005];
ll s[2005],ans[2005];
ll pow_mod(ll a,ll b)
{
ll ret=1;
while(b)
{
if(b&1)
ret=(ret*a)%mod;
a=(a*a)%mod;
b>>=1;
}
return ret;
}
int main()
{
int t;
cin>>t;
while(t--)
{
ll n,k;
cin>>n>>k;
for(int i=1; i<=n; i++)
cin>>a[i];
s[1]=1;
for(ll i=1; i<n; i++)
{
s[i+1]=s[i]*(((i+k-1)*pow_mod(i,mod-2))%mod)%mod;
}
for(int i=1; i<=n; i++)
{
ans[i]=0;
for(int j=1; j<=i; j++)
{
ans[i]+=a[j]*s[i-j+1]%mod;
ans[i]%=mod;
}
}
cout<<ans[1];
for(int i=2; i<=n; i++)
cout<<" "<<ans[i];
cout<<endl;
}
}
J 计算几何
#include<bits/stdc++.h>
using namespace std;
#define mp make_pair
typedef long long ll;
const double inf=1e200;
const double eps=1e-12;
const double pi=4*atan(1.0);
int dcmp(double x)
{
return fabs(x)<eps?0:(x<0?-1:1);
}
struct point
{
double x,y;
point(double a=0,double b=0):x(a),y(b) {}
};
point operator +(point A,point B)
{
return point(A.x+B.x,A.y+B.y);
}
point operator -(point A,point B)
{
return point(A.x-B.x,A.y-B.y);
}
point operator *(point A,double p)
{
return point(A.x*p,A.y*p);
}
point operator /(point A,double p)
{
return point(A.x/p,A.y/p);
}
bool operator ==(const point& a,const point& b)
{
return fabs(a.x-b.x)<eps&&fabs(a.y-b.y)<eps;
}
double dot(point A,point B)
{
return A.x*B.x+A.y*B.y;
}
double det(point A,point B)
{
return A.x*B.y-A.y*B.x;
}
double det(point O,point A,point B)
{
return det(A-O,B-O);
}
double length(point A)
{
return sqrt(dot(A,A));
}
double seg(point O,point A,point B)
{
if(dcmp(B.x-A.x)==0)
return (O.y-A.y)/(B.y-A.y);
return (O.x-A.x)/(B.x-A.x);
}
vector<point>pp[1010];
pair<double,int>s[1010*60];
double polyunion(vector<point>*p,int N)
{
double res=0;
for(int i=0; i<N; i++)
{
int sz=p[i].size();
for(int j=0; j<sz; j++)
{
int m=0;
s[m++]=mp(0,0);
s[m++]=mp(1,0);
point a=p[i][j],b=p[i][(j+1)%sz];
for(int k=0; k<N; k++)
{
if(i!=k)
{
int sz2=p[k].size();
for(int ii=0; ii<sz2; ii++)
{
point c=p[k][ii],d=p[k][(ii+1)%sz2];
int c1=dcmp(det(b-a,c-a));
int c2=dcmp(det(b-a,d-a));
if(c1==0&&c2==0)
{
if(dcmp(dot(b-a,d-c)))
{
s[m++]=mp(seg(c,a,b),1);
s[m++]=mp(seg(c,a,b),-1);
}
}
else
{
double s1=det(d-c,a-c);
double s2=det(d-c,b-c);
if(c1>=0&&c2<0)
s[m++]=mp(s1/(s1-s2),1);
else if(c1<0&&c2>=0)
s[m++]=mp(s1/(s1-s2),-1);
}
}
}
}
sort(s,s+m);
double pre=min(max(s[0].first,0.0),1.0),now,sum=0;
int cov=s[0].second;
for(int j=1; j<m; j++)
{
now=min(max(s[j].first,0.0),1.0);
if(!cov)
sum+=now-pre;
cov+=s[j].second;
pre=now;
}
res+=det(a,b)*sum;
}
}
return res/2;
}
int main()
{
int N,d,i,j;
point tp;
scanf("%d",&N);
for(i=0; i<N; i++)
{
scanf("%lf%lf%d",&tp.x,&tp.y,&d);
tp.x=tp.x-d;
pp[i].push_back(tp);
tp.x=tp.x+0.5*d;
tp.y=tp.y+1.7320508075689/2*d;
pp[i].push_back(tp);
tp.x=tp.x+d;
pp[i].push_back(tp);
tp.x=tp.x+0.5*d;
tp.y=tp.y-1.7320508075689/2*d;
pp[i].push_back(tp);
tp.y=tp.y-1.7320508075689/2*d;
tp.x=tp.x-0.5*d;
pp[i].push_back(tp);
tp.x=tp.x-d;
pp[i].push_back(tp);
}
double t=polyunion(pp,N);
printf("%.4lf\n",-t);
return 0;
}
F 模拟
#include <bits/stdc++.h>
using namespace std;
const int maxn=100007;
const int mod=1e9+7;
inline int is_alp(char c)
{
return (c>='a'&&c<='z')||(c>='A'&&c<='Z');
}
inline int is_num(char c)
{
return (c>='0'&&c<='9');
}
inline int is_op(char c)
{
return (c=='+'||c=='-'||c=='/'||c=='*');
}
double str2int(string S)
{
stringstream st(S);
double ret=0;
st>>ret;
return ret;
}
double cal(string &S,int L,int R,map<string,double> &h);
class func
{
public:
string name;
string ex;
vector<string> par;
double get_val(string &S,int L,int R)
{
map<string,double> emp,hs;
int cnt=0,tmp=0,last=0;
for(int i=L; i<=R; i++)
{
if(cnt==0&&i!=R&&S[i]=='(')
last=i+1;
if(S[i]=='(')
cnt++;
else if(S[i]==')')
cnt--;
if(cnt==1&&S[i]==',')
{
hs[par[tmp++]]=cal(S,last,i-1,emp);
last=i+1;
}
else if(cnt==0&&S[i]==')')
{
hs[par[tmp++]]=cal(S,last,i-1,emp);
last=i+1;
}
}
return cal(ex,0,ex.size()-1,hs);
}
};
map<string,func>func_map;
double cal(string &S,int L,int R,map<string,double> &h)
{
int cnt=0;
for(int i=R; i>=L; i--)
{
if(S[i]=='(')
cnt++;
else if(S[i]==')')
cnt--;
if(cnt==0&&S[i]=='+')
return cal(S,L,i-1,h)+cal(S,i+1,R,h);
if(cnt==0&&S[i]=='-'&&i-1>=L&&is_op(S[i-1]))
continue;
if(cnt==0&&S[i]=='-'&&i!=L)
return cal(S,L,i-1,h)-cal(S,i+1,R,h);
if(cnt==0&&S[i]=='-'&&i==L)
return 0-cal(S,i+1,R,h);
}
for(int i=R; i>=L; i--)
{
if(S[i]=='(')
cnt++;
else if(S[i]==')')
cnt--;
if(cnt==0&&S[i]=='*')
return cal(S,L,i-1,h)*cal(S,i+1,R,h);
if(cnt==0&&S[i]=='/')
{
return cal(S,L,i-1,h)/cal(S,i+1,R,h);
}
}
if(is_alp(S[L])&&S[R]==')')
{
string name="";
for(int i=L; is_alp(S[i]); i++)
name+=S[i];
return func_map[name].get_val(S,L,R);
}
if(is_alp(S[L])&&is_alp(S[R]))
return h[S.substr(L,R-L+1)];
if(is_num(S[L])&&is_num(S[R]))
return str2int(S.substr(L,R-L+1));
if(S[L]=='('&&S[R]==')')
return cal(S,L+1,R-1,h);
}
int main()
{
string s;
int n,q;
//freopen ("in.txt","r",stdin);
//freopen ("out.txt","w",stdout);
cin>>n;
for(int ii=0; ii<n; ii++)
{
string a,b;
cin>>a>>b;
func new_f;
new_f.ex=b;
int i=0;
for(; is_alp(a[i]); i++)
new_f.name+=a[i];
while(i<a.size())
{
string tmp= "";
i++;
for(; is_alp(a[i]); i++)
tmp+=a[i];
if(tmp.size()>0)
new_f.par.push_back(tmp);
}
func_map[new_f.name]=new_f;
}
cin>>q;
while(q--)
{
map <string, double> emp;
cin>>s;
cout<<fixed<< setprecision(2) <<cal(s,0,s.size()-1,emp)<<endl;
}
return 0;
}
/***
4
f(a,b) a*b+2*b
g(k) k+3
h(f,g) (f+g)*2/1
one(r,t) (r-t)+r*t/r/t+(t-r)
8
one(1223,4532)
100+f(1,2)-g(h(g(2),g(3)))
g(100+f(1,2)-g(h(g(2),g(3))))
g(100+f(1,2)-g(h(g(2),g(3))))/2
g(100+f(1,2)-g(h(g(2),g(3))))/(1+(1+0)*2-1)
(1+(1+0)*2-1)
1+g(100+f(1,2)-g(h(g(2),g(3))))/(g((1+(1+0)*2-1))-3)-1
16+h(1+g(100+f(1,2)-g(h(g(2),g(3))))/(g((1+(1+0)*2-1))-3)-1,1+g(100+f(1,2)-g(h(g(2),g(3))))/(g((1+(1+0)*2-1))-3)-1)/2
***/
K
贪心
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll getvalue(ll a) {
ll sum = 1;
while (a != 0) {
sum *= a % 10;
a /= 10;
}
return sum;
}
ll getlenth(ll v) {
ll k = 0;
while (v != 0) {
k++;
v /= 10;
}
return k;
}
int main()
{
ll n, t;
cin >> t;
while (t--)
{
cin >> n;
if (n < 10) { cout << n << endl; continue; }
ll length = getlenth(n), res = 0;
ll maxmum = getvalue(n);
for (int i = 1; i <= length; i++)
{
ll last = pow(10, i);
ll x = n % last;
if (x >= last - 1) res = n / last * last + last - 1;
else res = (n / last - 1)*last + last - 1;
maxmum = max(maxmum, getvalue(res));
}
cout << maxmum << endl;
}
return 0;
}