2016ACM-ICPC广西邀请赛(三题)
1001 A Math Problem
送分题,问有多少个数 k^k<=n。打表即可。
#include<bits/stdc++.h>
using namespace std;
long long a[16]={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
int main(){
for(int i=1;i<=15;i++){
for(int j=1;j<=i;j++){
a[i]*=i;
}
}
// long long ans=1;
// for(int i=1;i<=15;i++)ans*=15;
// cout<<ans<<endl;
// cout<<pow(15,15)<<endl;
// long long 不宜用pow
long long n;
while(cin>>n){
if(n>=437893890380859375)cout<<"15"<<endl;
else cout<<upper_bound(a,a+15,n)-a-1<<endl;
}
return 0;
}
1004 Covering
dp+矩阵快速幂。或者直接线性递推前10项,用杜教模版暴力杠。
如果我们考虑状态压缩DP,用0-15表示当前列的被覆盖的情况,可以压缩对称状态。并统计方案数,时间复杂度O(n)。然后利用矩阵快速幂加速即可,矩阵仅需6*6。时间复杂度O(216logn)
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <cassert>
using namespace std;
#define rep(i,a,n) for (ll i=a;i<n;i++)
#define per(i,a,n) for (ll i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((ll)(x).size())
typedef long long ll;
typedef vector<ll> VI;
typedef pair<ll,ll> PII;
const ll mod=1000000007;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
// head
ll _;
ll n;
namespace linear_seq {
const ll N=1e7;
ll res[N],base[N],_c[N],_md[N];
vector<ll> Md;
void mul(ll *a,ll *b,ll k) {
rep(i,0,k+k) _c[i]=0;
rep(i,0,k) if (a[i]) rep(j,0,k) _c[i+j]=(_c[i+j]+a[i]*b[j])%mod;
for (ll i=k+k-1;i>=k;i--) if (_c[i])
rep(j,0,SZ(Md)) _c[i-k+Md[j]]=(_c[i-k+Md[j]]-_c[i]*_md[Md[j]])%mod;
rep(i,0,k) a[i]=_c[i];
}
ll solve(ll n,VI a,VI b) { // a 系数 b 初值 b[n+1]=a[0]*b[n]+...
// printf("%d\n",SZ(b));
ll ans=0,pnt=0;
ll k=SZ(a);
assert(SZ(a)==SZ(b));
rep(i,0,k) _md[k-1-i]=-a[i];_md[k]=1;
Md.clear();
rep(i,0,k) if (_md[i]!=0) Md.push_back(i);
rep(i,0,k) res[i]=base[i]=0;
res[0]=1;
while ((1ll<<pnt)<=n) pnt++;
for (ll p=pnt;p>=0;p--) {
mul(res,res,k);
if ((n>>p)&1) {
for (ll i=k-1;i>=0;i--) res[i+1]=res[i];res[0]=0;
rep(j,0,SZ(Md)) res[Md[j]]=(res[Md[j]]-res[k]*_md[Md[j]])%mod;
}
}
rep(i,0,k) ans=(ans+res[i]*b[i])%mod;
if (ans<0) ans+=mod;
return ans;
}
VI BM(VI s) {
VI C(1,1),B(1,1);
ll L=0,m=1,b=1;
rep(n,0,SZ(s)) {
ll d=0;
rep(i,0,L+1) d=(d+(ll)C[i]*s[n-i])%mod;
if (d==0) ++m;
else if (2*L<=n) {
VI T=C;
ll c=mod-d*powmod(b,mod-2)%mod;
while (SZ(C)<SZ(B)+m) C.pb(0);
rep(i,0,SZ(B)) C[i+m]=(C[i+m]+c*B[i])%mod;
L=n+1-L; B=T; b=d; m=1;
} else {
ll c=mod-d*powmod(b,mod-2)%mod;
while (SZ(C)<SZ(B)+m) C.pb(0);
rep(i,0,SZ(B)) C[i+m]=(C[i+m]+c*B[i])%mod;
++m;
}
}
return C;
}
ll gao(VI a,ll n) {
VI c=BM(a);
c.erase(c.begin());
rep(i,0,SZ(c)) c[i]=(mod-c[i])%mod;
return solve(n,c,VI(a.begin(),a.begin()+SZ(c)));
}
};
int main()
{
while(~scanf("%lld",&n))
printf("%d\n",linear_seq::gao(VI{1,5,11,36,95,281,781,2245,6336,18061},n-1));
return 0;
}
或者快速幂做法:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <set>
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
struct matrix {
ll m[4][4];
};
matrix multi(matrix a, matrix b) {
matrix tmp;
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
tmp.m[i][j] = 0;
for(int k = 0; k < 4; k++) {
tmp.m[i][j] = (tmp.m[i][j] % mod + ((a.m[i][k] % mod) * (b.m[k][j] % mod)) % mod) % mod;
}
}
}
return tmp;
}
void print(matrix a){
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
cout<<a.m[i][j]<<" ";
}
cout<<endl;
}
puts("");
}
matrix fill(matrix &a, int x) {
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
a.m[i][j] = x;
}
}
// print(a);
return a;
}
matrix quick_mod(ll n) {
matrix base, ans;
fill(ans, 0);
fill(base, 0);
// print(ans);
base.m[0][0] = 1, base.m[0][1] = 5 ,base.m[0][2] = 1, base.m[0][3] =-1;
base.m[1][0] = base.m[2][1] = base.m[3][2] = 1;
ans.m[0][0] = ans.m[1][1] = ans.m[2][2] = ans.m[3][3] = 1;
while(n) {
if(n & 1) ans = multi(ans, base);
base = multi(base, base);
n >>= 1;
}
return ans;
}
int main() {
ll n;
while(~scanf("%lld", &n)) {
matrix save = quick_mod(n - 1);
matrix ans;
fill(ans, 0);
ans.m[0][0]=36;
ans.m[1][0]=11;
ans.m[2][0]=5;
ans.m[3][0]=1;
// print(ans);
ans = multi(save, ans);
// print(ans);
ll res = (ans.m[3][0] % mod + mod) % mod;
printf("%lld\n", res);
}
}
统计前n个数的and or xor,只需统计每个数的每个位置上的01的个数。有0的and肯定是0,有1的or肯定是1,xor就是用了性质:x^b^b=x。
#include<bits/stdc++.h>
using namespace std;
int a[100005],b[100005];
int main(){
int n,p;
while(~scanf("%d%d",&n,&p)){
memset(b,0,sizeof(b));
int ans_and=0xffffffff,ans_or=0,ans_xor=0;
// cout<<"1 "<<ans_and<<" "<<ans_or<<" "<<ans_xor<<endl;
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
int x = a[i];
ans_and = (ans_and&a[i]);
ans_or = (ans_or|a[i]);
ans_xor = (ans_xor^a[i]);
// cout<<i<<" "<<ans_and<<" "<<ans_or<<" "<<ans_xor<<endl;
for(int j=0;x;j++,x>>=1)
b[j]+=x%2;
}
for(int i=1;i<=p;i++){
int co;
scanf("%d",&co);
int temp = a[co];
int A = ans_and , O = ans_or , X = ans_xor;
X=X^temp;
for(int j=0;j<=30;j++,temp>>=1)
{
if(b[j]==n-1&&temp%2==0)A+=(1<<j);
if(b[j]==1 &&temp%2 )O-=(1<<j);
}
printf("%d %d %d\n",A,O,X);
}
}
return 0;
}
1007 Duizi and Shunzi
签到题
排序,从小到大枚举每个整数。
对于每个整数{
当前数能作为某个顺子的最大值,则取顺子;
否则能取对子,则取对子。
}