layout: post
title: 数学基础
author: "luowentaoaa"
catalog: true
mathjax: true
tags:
---
基本计数方法
Chess QueenUVA - 11538 (平方和公式由(n+1)^3推出)
#include
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<
Triangle CountingUVA - 11401 (从1-n中取出三个不同整数组成三角形的方法)
#include
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<
Triangle CountingUVA - 11401(容斥定理)
#include
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>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 "<
递推关系
Ingenuous CubrencyUVA - 11137 (背包)
题意
求将n写成诺干个正整数的立方和有多少种方案
#include
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<
Exploring PyramidsUVALive - 3516 (记忆化搜索)
#include
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<
Investigating Div-Sum PropertyUVA - 11361 (数位DP)
#include
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<
数论
Always an integer UVALive - 4119 (欧拉函数)
#include
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&&n){
cout<
Emoogle GridUVA - 11916 (大步小步算法)
#include
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 >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));
mapx;
x[1]=0;
for(i=1;i>t;
for(int p=1;p<=t;p++){
cin>>n>>k>>b>>r;
bset.clear();
m=1;
for(int i=0;i>x[i]>>y[i];
if(x[i]>m)m=x[i];
bset.insert(make_pair(x[i],y[i]));
}
cout<<"Case "<
组合游戏
Playing With StonesUVALive - 5059 (sg函数)
#include
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>a;
v^=sg(a);
}
if(v)cout<<"YES\n";
else cout<<"NO\n";
}
return 0;
}