数学基础
基本计数方法
Chess QueenUVA - 11538 (平方和公式由(n+1)^3推出)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=998244353;
const int maxn=1e6+10;
const ll inf=0x3f3f3f3f3f3f3f3fLL;
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
ll n,m;
while(cin>>n>>m&&n&&m){
if(n>m)swap(n,m);
cout<<n*m*(m-1)+n*m*(n-1)+2*n*(n-1)*(3*m-n-1)/3<<endl;
}
return 0;
}
Triangle CountingUVA - 11401 (从1-n中取出三个不同整数组成三角形的方法)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=998244353;
const int maxn=1e6+10;
const ll inf=0x3f3f3f3f3f3f3f3fLL;
ll dp[maxn];
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
dp[3]=0;
for(ll x=4;x<=1e6;x++)
dp[x]=dp[x-1]+((x-1)*(x-2)/2-(x-1)/2)/2;
int n;
while(cin>>n){
if(n<3)break;
cout<<dp[n]<<endl;
}
return 0;
}
Triangle CountingUVA - 11401(容斥定理)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e6+7;
const int maxn=1e6+10;
const ll inf=0x3f3f3f3f3f3f3f3fLL;
int c[550][550];
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
c[0][0]=1;
for(int i=0;i<=500;i++){
c[i][0]=c[i][i]=1;
for(int j=1;j<i;j++)
c[i][j]=(c[i-1][j]+c[i-1][j-1])%mod;
}
int t;
cin>>t;
for(int cas=1;cas<=t;cas++){
int n,m,k,sum=0;
cin>>n>>m>>k;
for(int sta=0;sta<(1<<4);sta++){
int b=0,r=n,cc=m;
if(sta&1)r--,b++;
if(sta&2)r--,b++;
if(sta&4)cc--,b++;
if(sta&8)cc--,b++;
if(b&1)sum=(sum-c[r*cc][k]+mod)%mod;
else sum=(sum+c[r*cc][k])%mod;
}
cout<<"Case "<<cas<<": "<<sum<<endl;
}
return 0;
}
递推关系
Ingenuous CubrencyUVA - 11137 (背包)
题意
求将n写成诺干个正整数的立方和有多少种方案
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e6+7;
const int maxn=1e6+10;
const ll inf=0x3f3f3f3f3f3f3f3fLL;
ll dp[maxn];
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
dp[0]=1;
for(int i=1;i<=21;i++){
for(int j=0;j<=10000;j++){
dp[i*i*i+j]+=dp[j];
}
}
int n;
while(cin>>n)cout<<dp[n]<<endl;
return 0;
}
Exploring PyramidsUVALive - 3516 (记忆化搜索)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9;
const int maxn=1e6+10;
const ll inf=0x3f3f3f3f3f3f3f3fLL;
char s[maxn];
ll dp[400][400];
ll dfs(int i,int j){
if(i==j)return 1;
if(s[i]!=s[j])return 0;
ll &ans=dp[i][j];
if(ans>=0)return ans;ans=0;
for(int k=i+2;k<=j;k++)
if(s[i]==s[k])
ans=(ans+1LL*dfs(i+1,k-1)*1LL*dfs(k,j))%mod;
return ans;
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
while(cin>>s){
memset(dp,-1,sizeof(dp));
cout<<dfs(0,strlen(s)-1)<<endl;
}
return 0;
}
Investigating Div-Sum PropertyUVA - 11361 (数位DP)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9;
const int maxn=1e6+10;
const ll inf=0x3f3f3f3f3f3f3f3fLL;
ll dp[30][150][150];
int nu[30];
int k;
ll dfs(int pos,int num,int sum,bool limit){
if(pos==-1)return (num%k==0)&&(sum%k==0);
if(!limit&&dp[pos][num][sum]!=-1)return dp[pos][num][sum];
int up=limit?nu[pos]:9;
int ans=0;
for(int i=0;i<=up;i++){
ans+=dfs(pos-1,(num+i)%k,(sum*10+i)%k,limit&&i==nu[pos]);
}
if(!limit)dp[pos][num][sum]=ans;
return ans;
}
ll ac(ll x){
ll pos=0;
while(x){
nu[pos++]=x%10;
x/=10;
}
return dfs(pos-1,0,0,true);
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
int t;
cin>>t;
while(t--){
ll a,b;
cin>>a>>b>>k;
if(k>90){
cout<<0<<endl;continue;
}
memset(dp,-1,sizeof(dp));
cout<<ac(b)-ac(a-1)<<endl;
}
return 0;
}
/*
999
1 1000 4
*/
数论
Always an integer UVALive - 4119 (欧拉函数)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9;
const int maxn=4e6+10;
const ll inf=0x3f3f3f3f3f3f3f3fLL;
const int N = 4e6+10;
int phi[N] = {0, 1};
void caleuler()
{
for (int i = 2; i < N; i++)
if (!phi[i])
for (int j = i; j < N; j += i)
{
if (!phi[j]) phi[j] = j;
phi[j] = phi[j] / i * (i - 1);
}
}
ll s[maxn],f[maxn];
void init(){
caleuler();
for(int i=1;i<N;i++){
for(int j=i*2;j<N;j+=i)f[j]+=i*phi[j/i];
}
for(int i=2;i<N;i++)s[i]=s[i-1]+f[i];
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
init();
int n;
while(cin>>n&&n){
cout<<s[n]<<endl;
}
return 0;
}
Emoogle GridUVA - 11916 (大步小步算法)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e8+7;
const int maxn=550;
const ll inf=0x3f3f3f3f3f3f3f3fLL;
int n,m,k,b,r,x[maxn],y[maxn];
set<pair<int,int> >bset;
ll fastpow(ll a,ll n){
ll ans=1;
while(n){
if(n&1)ans=(ans*a)%mod;
a=(a*a)%mod;
n>>=1;
}
return ans;
}
ll inv(ll a){
return fastpow(a,mod-2);
}
ll mul_mod(ll a,ll b){
return 1LL*a*b%mod;
}
int log_mod(int a,int b){
int m,v,e=1,i;
m=(int)sqrt(mod);
v=inv(fastpow(a,m));
map<int,int>x;
x[1]=0;
for(i=1;i<m;i++){
e=mul_mod(e,a);
if(!x.count(e))x[e]=i;
}
for(i=0;i<m;i++){
if(x.count(b))return i*m+x[b];
b=mul_mod(b,v);
}
return -1;
}
int count(){
int c=0;
for(int i=0;i<b;i++){
if(x[i]!=m&&!bset.count(make_pair(x[i]+1,y[i])))c++;
}
c+=n;
for(int i=0;i<b;i++)if(x[i]==1)c--;
return mul_mod(fastpow(k,c),fastpow(k-1,1LL*n*m-b-c));
}
int doit(){
int cnt=count();
if(cnt==r)return m;
int c=0;
for(int i=0;i<b;i++)
if(x[i]==m)c++;
m++;
cnt=mul_mod(cnt,fastpow(k,c));
cnt=mul_mod(cnt,fastpow(k-1,n-c));
if(cnt==r)return m;
return log_mod(fastpow(k-1,n),mul_mod(r,inv(cnt)))+m;
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
int t;
cin>>t;
for(int p=1;p<=t;p++){
cin>>n>>k>>b>>r;
bset.clear();
m=1;
for(int i=0;i<b;i++){
cin>>x[i]>>y[i];
if(x[i]>m)m=x[i];
bset.insert(make_pair(x[i],y[i]));
}
cout<<"Case "<<p<<": "<<doit()<<endl;
}
return 0;
}
组合游戏
Playing With StonesUVALive - 5059 (sg函数)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e8+7;
const int maxn=550;
const ll inf=0x3f3f3f3f3f3f3f3fLL;
ll sg(ll x){
return x%2==0?x/2:sg(x/2);
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout.tie(0);
int t;
cin>>t;
while(t--){
ll n,a,v=0;
cin>>n;
for(int i=0;i<n;i++){
cin>>a;
v^=sg(a);
}
if(v)cout<<"YES\n";
else cout<<"NO\n";
}
return 0;
}