久違的寫篇博客吧 ios
題意:c++
給你一個只包含數字的字符串,你能夠將字符串隨機排列,問最後組成的數字可否被60整除git
分析:ide
咱們先考慮能被6整除的數的特色:ui
①各個位數和 % 3 == 0spa
②末尾數必須爲 0 2 4 6 8中的一個debug
再考慮能被10整除的數的特色:指針
末尾數必須爲0code
由於60中6在十位部分 ,10在個位部分,因此最後組成的數除了各個位數和 % 3 == 0 外十位部分必須爲0 、2 、四、六、8 中的一個, 個位數必須爲0。blog
#include<bits/stdc++.h> #define ios std::ios::sync_with_stdio(false) #define sd(n) scanf("%d",&n) #define sdd(n,m) scanf("%d%d",&n,&m) #define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k) #define pd(n) printf("%d\n", (n)) #define pdd(n,m) printf("%d %d\n", n, m) #define pld(n) printf("%lld\n", n) #define pldd(n,m) printf("%lld %lld\n", n, m) #define sld(n) scanf("%lld",&n) #define sldd(n,m) scanf("%lld%lld",&n,&m) #define slddd(n,m,k) scanf("%lld%lld%lld",&n,&m,&k) #define sf(n) scanf("%lf",&n) #define sff(n,m) scanf("%lf%lf",&n,&m) #define sfff(n,m,k) scanf("%lf%lf%lf",&n,&m,&k) #define rep(i,a,n) for (int i=a;i<=n;i++) #define per(i,n,a) for (int i=n;i>=a;i--) #define mm(a,n) memset(a, n, sizeof(a)) #define pb push_back #define all(x) (x).begin(),(x).end() #define fi first #define se second #define il inline #define ll long long #define lson rt << 1 #define rson rt << 1 | 1 #define MOD 1000000007 #define pi 3.14159265358979323 #define debug(x) cout <<#x<<": "<<x<<endl #define debug2(x, y) cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl; #define debug3(x, y, z) cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl; #define debug4(a, b, c, d) cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl; using namespace std; const ll INF (0x3f3f3f3f3f3f3f3fll); const int inf (0x3f3f3f3f); template<typename T>void read(T &res){bool flag=false;char ch;while(!isdigit(ch=getchar()))(ch=='-')&&(flag=true); for(res=ch-48;isdigit(ch=getchar());res=(res<<1)+(res<<3)+ch - 48);flag&&(res=-res);} template<typename T>void Out(T x){if(x<0)putchar('-'),x=-x;if(x>9)Out(x/10);putchar(x%10+'0');} ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} ll lcm(ll a,ll b){return a*b/gcd(a,b);} ll pow_mod(ll x,ll n,ll mod){ll res=1;while(n){if(n&1)res=res*x%mod;x=x*x%mod;n>>=1;}return res;} ll fact_pow(ll n,ll p){ll res=0;while(n){n/=p;res+=n;}return res;} ll mult(ll a,ll b,ll p){a%=p;b%=p;ll r=0,v=a;while(b){if(b&1){r+=v;if(r>p)r-=p;}v<<=1;if(v>p)v-=p;b>>=1;}return r;} ll quick_pow(ll a,ll b,ll p){ll r=1,v=a%p;while(b){if(b&1)r=mult(r,v,p);v=mult(v,v,p);b>>=1;}return r;} bool CH(ll a,ll n,ll x,ll t) {ll r=quick_pow(a,x,n);ll z=r;for(ll i=1;i<=t;i++){r=mult(r,r,n);if(r==1&&z!=1&&z!=n-1)return true;z=r;}return r!=1;} bool Miller_Rabin(ll n) {if(n<2)return false;if(n==2)return true;if(!(n&1))return false;ll x=n-1,t=0;while(!(x&1)){x>>=1;t++;}srand(time(NULL)); ll o=8;for(ll i=0;i<o;i++){ll a=rand()%(n-1)+1;if(CH(a,n,x,t))return false;}return true;} /* int prime[30000010],minprime[30000010]; void euler(int n) {int c=0,i,j;for(i=2;i<=n;i++){if(!minprime[i])prime[++c]=i,minprime[i]=i;for(j=1;j<=c&&i*prime[j]<=n;j++) {minprime[i*prime[j]]=prime[j];if(i%prime[j]==0)break;}}} struct Tree{ll l,r,sum,lazy,maxn,minn;}tree[1000000]; il void push_up(ll rt) {tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum; tree[rt].maxn=max(tree[rt<<1].maxn,tree[rt<<1|1].maxn); tree[rt].minn=min(tree[rt<<1].minn,tree[rt<<1|1].minn);} il void push_down(ll rt , ll now) {if(tree[rt].lazy){tree[rt<<1].lazy+=tree[rt].lazy;tree[rt<<1|1].lazy+=tree[rt].lazy; tree[rt<<1].sum+=(now-(now>>1))*tree[rt].lazy;tree[rt<<1|1].sum+=(now>>1)*tree[rt].lazy; tree[rt<<1].minn+=tree[rt].lazy;tree[rt<<1|1].minn+=tree[rt].lazy; tree[rt<<1].maxn+=tree[rt].lazy;tree[rt<<1|1].maxn+=tree[rt].lazy;tree[rt].lazy=0;}} il void build(ll l , ll r , ll rt , ll *aa) {tree[rt].lazy=0;tree[rt].l=l;tree[rt].r=r;if(l==r) {tree[rt].sum=aa[l];tree[rt].minn=tree[rt].sum;tree[rt].maxn=tree[rt].sum;return;} ll mid=(l+r)>>1;build(l,mid,rt<<1,aa);build(mid+1,r,rt<<1|1,aa);push_up(rt);} il void update_range(ll L , ll R , ll key , ll rt) {if(L<=tree[rt].l&&R>=tree[rt].r){tree[rt].sum+=(tree[rt].r-tree[rt].l+1)*key; tree[rt].minn+=key;tree[rt].maxn+=key;tree[rt].lazy+=key;return;}push_down(rt,tree[rt].r-tree[rt].l+1); ll mid=(tree[rt].r+tree[rt].l)>>1;if(L<=mid)update_range(L,R,key,lson); if(R>mid)update_range(L,R,key,rson);push_up(rt);} il ll query_range(ll L, ll R, ll rt) {if(L<=tree[rt].l&&R>=tree[rt].r){return tree[rt].sum;}push_down(rt,tree[rt].r-tree[rt].l+1); ll mid=(tree[rt].r+tree[rt].l)>>1;ll ans=0;if(L<=mid)ans+=query_range(L,R,lson); if(R>mid)ans+=query_range(L,R,rson);return ans;} il ll query_min(ll L,ll R,ll rt) {if(L<=tree[rt].l&&R>=tree[rt].r){return tree[rt].minn;}push_down(rt,tree[rt].r-tree[rt].l+1); ll mid=(tree[rt].r+tree[rt].l)>>1;ll ans=(0x3f3f3f3f3f3f3f3fll);if(L<=mid)ans=min(ans,query_min(L,R,lson)); if(R>mid)ans=min(ans,query_min(L,R,rson));return ans;} il ll query_max(ll L, ll R, ll rt) {if(L<=tree[rt].l&&R>=tree[rt].r){return tree[rt].maxn;}push_down(rt,tree[rt].r-tree[rt].l+1); ll mid=(tree[rt].r+tree[rt].l)>>1;ll ans=-(0x3f3f3f3f3f3f3f3fll);if(L<=mid)ans=max(ans,query_max(L,R,lson)); if(R>mid)ans=max(ans,query_max(L,R,rson));return ans;} */ const int N = 1e3 + 10; ll a[N][N]; map<int , int>ha; int main() { ios; int n ; cin >> n; while(n --) { ha.clear(); string s; cin >> s; int len = s.size() - 1; int tot = 0; rep(j , 0 , len) { ha[s[j] - '0'] ++; tot += s[j] - '0'; } if(ha[0] && (ha[2] || ha[4] || ha[6] || ha[8] || (ha[0] - 1)) && tot % 3 == 0) cout << "red" << '\n'; else cout << "cyan" << '\n'; } return 0; }
題意:
你有無限個骰子,你能夠從中選出任意個來拼搭,問「沒有被擋住的點數的和」可否組成某個數X
分析:
首先一個骰子全部點數和爲21 , 而且1的對立面爲6,2的對立面爲5,3對立面爲4。
當骰子只有一個的時候,它被擋住的只會它的底部,那麼它能夠組成的數就爲21 - (1~6) = 15 ~ 20
當骰子有兩個的時候,由於它們要拼搭在一塊兒,因此第一個骰子會被第二個骰子擋住一個面,第二個骰子會被第一個骰子擋住一個面,而且這兩個面是對立的
因此當第二個骰子加入的時候,總的點數爲原先點數 + 21(第二個骰子的總點數) - 7(第二個骰子加入時減去的部分) = (15 ~ 20) + 14
加入第三個第四個第五個。。。第n個同理 , 最後的總點數就爲 (15 ~ 20) + 14 * (n - 1) , 因此若是能夠拼湊出X則 X % 14 的答案爲 (15 ~ 20 )% 14 = 1 ~ 6
因此最後只要判斷X是否大於等於15而且 X % 14 在不在 1 ~ 6 這個範圍就能夠了
#include<bits/stdc++.h> #define ios std::ios::sync_with_stdio(false) #define sd(n) scanf("%d",&n) #define sdd(n,m) scanf("%d%d",&n,&m) #define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k) #define pd(n) printf("%d\n", (n)) #define pdd(n,m) printf("%d %d\n", n, m) #define pld(n) printf("%lld\n", n) #define pldd(n,m) printf("%lld %lld\n", n, m) #define sld(n) scanf("%lld",&n) #define sldd(n,m) scanf("%lld%lld",&n,&m) #define slddd(n,m,k) scanf("%lld%lld%lld",&n,&m,&k) #define sf(n) scanf("%lf",&n) #define sff(n,m) scanf("%lf%lf",&n,&m) #define sfff(n,m,k) scanf("%lf%lf%lf",&n,&m,&k) #define rep(i,a,n) for (int i=a;i<=n;i++) #define per(i,n,a) for (int i=n;i>=a;i--) #define mm(a,n) memset(a, n, sizeof(a)) #define pb push_back #define all(x) (x).begin(),(x).end() #define fi first #define se second #define il inline #define ll long long #define lson rt << 1 #define rson rt << 1 | 1 #define MOD 1000000007 #define pi 3.14159265358979323 #define debug(x) cout <<#x<<": "<<x<<endl #define debug2(x, y) cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl; #define debug3(x, y, z) cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl; #define debug4(a, b, c, d) cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl; using namespace std; const ll INF (0x3f3f3f3f3f3f3f3fll); const int inf (0x3f3f3f3f); template<typename T>void read(T &res){bool flag=false;char ch;while(!isdigit(ch=getchar()))(ch=='-')&&(flag=true); for(res=ch-48;isdigit(ch=getchar());res=(res<<1)+(res<<3)+ch - 48);flag&&(res=-res);} template<typename T>void Out(T x){if(x<0)putchar('-'),x=-x;if(x>9)Out(x/10);putchar(x%10+'0');} ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} ll lcm(ll a,ll b){return a*b/gcd(a,b);} ll pow_mod(ll x,ll n,ll mod){ll res=1;while(n){if(n&1)res=res*x%mod;x=x*x%mod;n>>=1;}return res;} ll fact_pow(ll n,ll p){ll res=0;while(n){n/=p;res+=n;}return res;} ll mult(ll a,ll b,ll p){a%=p;b%=p;ll r=0,v=a;while(b){if(b&1){r+=v;if(r>p)r-=p;}v<<=1;if(v>p)v-=p;b>>=1;}return r;} ll quick_pow(ll a,ll b,ll p){ll r=1,v=a%p;while(b){if(b&1)r=mult(r,v,p);v=mult(v,v,p);b>>=1;}return r;} bool CH(ll a,ll n,ll x,ll t) {ll r=quick_pow(a,x,n);ll z=r;for(ll i=1;i<=t;i++){r=mult(r,r,n);if(r==1&&z!=1&&z!=n-1)return true;z=r;}return r!=1;} bool Miller_Rabin(ll n) {if(n<2)return false;if(n==2)return true;if(!(n&1))return false;ll x=n-1,t=0;while(!(x&1)){x>>=1;t++;}srand(time(NULL)); ll o=8;for(ll i=0;i<o;i++){ll a=rand()%(n-1)+1;if(CH(a,n,x,t))return false;}return true;} /* int prime[30000010],minprime[30000010]; void euler(int n) {int c=0,i,j;for(i=2;i<=n;i++){if(!minprime[i])prime[++c]=i,minprime[i]=i;for(j=1;j<=c&&i*prime[j]<=n;j++) {minprime[i*prime[j]]=prime[j];if(i%prime[j]==0)break;}}} struct Tree{ll l,r,sum,lazy,maxn,minn;}tree[1000000]; il void push_up(ll rt) {tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum; tree[rt].maxn=max(tree[rt<<1].maxn,tree[rt<<1|1].maxn); tree[rt].minn=min(tree[rt<<1].minn,tree[rt<<1|1].minn);} il void push_down(ll rt , ll now) {if(tree[rt].lazy){tree[rt<<1].lazy+=tree[rt].lazy;tree[rt<<1|1].lazy+=tree[rt].lazy; tree[rt<<1].sum+=(now-(now>>1))*tree[rt].lazy;tree[rt<<1|1].sum+=(now>>1)*tree[rt].lazy; tree[rt<<1].minn+=tree[rt].lazy;tree[rt<<1|1].minn+=tree[rt].lazy; tree[rt<<1].maxn+=tree[rt].lazy;tree[rt<<1|1].maxn+=tree[rt].lazy;tree[rt].lazy=0;}} il void build(ll l , ll r , ll rt , ll *aa) {tree[rt].lazy=0;tree[rt].l=l;tree[rt].r=r;if(l==r) {tree[rt].sum=aa[l];tree[rt].minn=tree[rt].sum;tree[rt].maxn=tree[rt].sum;return;} ll mid=(l+r)>>1;build(l,mid,rt<<1,aa);build(mid+1,r,rt<<1|1,aa);push_up(rt);} il void update_range(ll L , ll R , ll key , ll rt) {if(L<=tree[rt].l&&R>=tree[rt].r){tree[rt].sum+=(tree[rt].r-tree[rt].l+1)*key; tree[rt].minn+=key;tree[rt].maxn+=key;tree[rt].lazy+=key;return;}push_down(rt,tree[rt].r-tree[rt].l+1); ll mid=(tree[rt].r+tree[rt].l)>>1;if(L<=mid)update_range(L,R,key,lson); if(R>mid)update_range(L,R,key,rson);push_up(rt);} il ll query_range(ll L, ll R, ll rt) {if(L<=tree[rt].l&&R>=tree[rt].r){return tree[rt].sum;}push_down(rt,tree[rt].r-tree[rt].l+1); ll mid=(tree[rt].r+tree[rt].l)>>1;ll ans=0;if(L<=mid)ans+=query_range(L,R,lson); if(R>mid)ans+=query_range(L,R,rson);return ans;} il ll query_min(ll L,ll R,ll rt) {if(L<=tree[rt].l&&R>=tree[rt].r){return tree[rt].minn;}push_down(rt,tree[rt].r-tree[rt].l+1); ll mid=(tree[rt].r+tree[rt].l)>>1;ll ans=(0x3f3f3f3f3f3f3f3fll);if(L<=mid)ans=min(ans,query_min(L,R,lson)); if(R>mid)ans=min(ans,query_min(L,R,rson));return ans;} il ll query_max(ll L, ll R, ll rt) {if(L<=tree[rt].l&&R>=tree[rt].r){return tree[rt].maxn;}push_down(rt,tree[rt].r-tree[rt].l+1); ll mid=(tree[rt].r+tree[rt].l)>>1;ll ans=-(0x3f3f3f3f3f3f3f3fll);if(L<=mid)ans=max(ans,query_max(L,R,lson)); if(R>mid)ans=max(ans,query_max(L,R,rson));return ans;} */ const int N = 1e3 + 10; ll a[N][N]; map<int , int>ha; int main() { ios; int t; cin >> t; while(t --) { ll x; cin >> x; if(x < 15) { cout << "NO" << '\n'; continue; } x %= 14; if(x > 6 || x < 1) cout << "NO" << '\n'; else cout << "YES" << '\n'; } return 0; }
題意:
要求構造一個 r 行 c 列的矩陣, 而且矩陣的每一行的gcd和每一列的gcd的都只出現過一次,且每行的gcd都要求最小
分析:
首先當 r = 1 && c = 1 的時候, 它的行gcd和列gcd爲同一元素, 因此是沒法構造出來的
當 r = 1 或者 c = 1 時 , 咱們能夠 1 2 3 4 5 6 ... 輸出
對於其它的可能,咱們考慮這麼構造:
假設 r = 2 , c = 2 , A 、B 、C 、D 爲矩陣元素
1 | A C
2 | B D 咱們可讓第一行的gcd結果爲1 , 第二行gcd結果爲2,第一列gcd結果爲3,第二列gcd結果爲4
3 4
則 A = 1 * 3 , B = 2 * 3 , C = 1 * 4 , D = 2 * 4 這樣構造便可
#include<bits/stdc++.h> #define ios std::ios::sync_with_stdio(false) #define sd(n) scanf("%d",&n) #define sdd(n,m) scanf("%d%d",&n,&m) #define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k) #define pd(n) printf("%d\n", (n)) #define pdd(n,m) printf("%d %d\n", n, m) #define pld(n) printf("%lld\n", n) #define pldd(n,m) printf("%lld %lld\n", n, m) #define sld(n) scanf("%lld",&n) #define sldd(n,m) scanf("%lld%lld",&n,&m) #define slddd(n,m,k) scanf("%lld%lld%lld",&n,&m,&k) #define sf(n) scanf("%lf",&n) #define sff(n,m) scanf("%lf%lf",&n,&m) #define sfff(n,m,k) scanf("%lf%lf%lf",&n,&m,&k) #define rep(i,a,n) for (int i=a;i<=n;i++) #define per(i,n,a) for (int i=n;i>=a;i--) #define mm(a,n) memset(a, n, sizeof(a)) #define pb push_back #define all(x) (x).begin(),(x).end() #define fi first #define se second #define il inline #define ll long long #define lson rt << 1 #define rson rt << 1 | 1 #define MOD 1000000007 #define pi 3.14159265358979323 #define debug(x) cout <<#x<<": "<<x<<endl #define debug2(x, y) cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl; #define debug3(x, y, z) cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl; #define debug4(a, b, c, d) cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl; using namespace std; const ll INF (0x3f3f3f3f3f3f3f3fll); const int inf (0x3f3f3f3f); template<typename T>void read(T &res){bool flag=false;char ch;while(!isdigit(ch=getchar()))(ch=='-')&&(flag=true); for(res=ch-48;isdigit(ch=getchar());res=(res<<1)+(res<<3)+ch - 48);flag&&(res=-res);} template<typename T>void Out(T x){if(x<0)putchar('-'),x=-x;if(x>9)Out(x/10);putchar(x%10+'0');} ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} ll lcm(ll a,ll b){return a*b/gcd(a,b);} ll pow_mod(ll x,ll n,ll mod){ll res=1;while(n){if(n&1)res=res*x%mod;x=x*x%mod;n>>=1;}return res;} ll fact_pow(ll n,ll p){ll res=0;while(n){n/=p;res+=n;}return res;} ll mult(ll a,ll b,ll p){a%=p;b%=p;ll r=0,v=a;while(b){if(b&1){r+=v;if(r>p)r-=p;}v<<=1;if(v>p)v-=p;b>>=1;}return r;} ll quick_pow(ll a,ll b,ll p){ll r=1,v=a%p;while(b){if(b&1)r=mult(r,v,p);v=mult(v,v,p);b>>=1;}return r;} bool CH(ll a,ll n,ll x,ll t) {ll r=quick_pow(a,x,n);ll z=r;for(ll i=1;i<=t;i++){r=mult(r,r,n);if(r==1&&z!=1&&z!=n-1)return true;z=r;}return r!=1;} bool Miller_Rabin(ll n) {if(n<2)return false;if(n==2)return true;if(!(n&1))return false;ll x=n-1,t=0;while(!(x&1)){x>>=1;t++;}srand(time(NULL)); ll o=8;for(ll i=0;i<o;i++){ll a=rand()%(n-1)+1;if(CH(a,n,x,t))return false;}return true;} /* int prime[30000010],minprime[30000010]; void euler(int n) {int c=0,i,j;for(i=2;i<=n;i++){if(!minprime[i])prime[++c]=i,minprime[i]=i;for(j=1;j<=c&&i*prime[j]<=n;j++) {minprime[i*prime[j]]=prime[j];if(i%prime[j]==0)break;}}} struct Tree{ll l,r,sum,lazy,maxn,minn;}tree[1000000]; il void push_up(ll rt) {tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum; tree[rt].maxn=max(tree[rt<<1].maxn,tree[rt<<1|1].maxn); tree[rt].minn=min(tree[rt<<1].minn,tree[rt<<1|1].minn);} il void push_down(ll rt , ll now) {if(tree[rt].lazy){tree[rt<<1].lazy+=tree[rt].lazy;tree[rt<<1|1].lazy+=tree[rt].lazy; tree[rt<<1].sum+=(now-(now>>1))*tree[rt].lazy;tree[rt<<1|1].sum+=(now>>1)*tree[rt].lazy; tree[rt<<1].minn+=tree[rt].lazy;tree[rt<<1|1].minn+=tree[rt].lazy; tree[rt<<1].maxn+=tree[rt].lazy;tree[rt<<1|1].maxn+=tree[rt].lazy;tree[rt].lazy=0;}} il void build(ll l , ll r , ll rt , ll *aa) {tree[rt].lazy=0;tree[rt].l=l;tree[rt].r=r;if(l==r) {tree[rt].sum=aa[l];tree[rt].minn=tree[rt].sum;tree[rt].maxn=tree[rt].sum;return;} ll mid=(l+r)>>1;build(l,mid,rt<<1,aa);build(mid+1,r,rt<<1|1,aa);push_up(rt);} il void update_range(ll L , ll R , ll key , ll rt) {if(L<=tree[rt].l&&R>=tree[rt].r){tree[rt].sum+=(tree[rt].r-tree[rt].l+1)*key; tree[rt].minn+=key;tree[rt].maxn+=key;tree[rt].lazy+=key;return;}push_down(rt,tree[rt].r-tree[rt].l+1); ll mid=(tree[rt].r+tree[rt].l)>>1;if(L<=mid)update_range(L,R,key,lson); if(R>mid)update_range(L,R,key,rson);push_up(rt);} il ll query_range(ll L, ll R, ll rt) {if(L<=tree[rt].l&&R>=tree[rt].r){return tree[rt].sum;}push_down(rt,tree[rt].r-tree[rt].l+1); ll mid=(tree[rt].r+tree[rt].l)>>1;ll ans=0;if(L<=mid)ans+=query_range(L,R,lson); if(R>mid)ans+=query_range(L,R,rson);return ans;} il ll query_min(ll L,ll R,ll rt) {if(L<=tree[rt].l&&R>=tree[rt].r){return tree[rt].minn;}push_down(rt,tree[rt].r-tree[rt].l+1); ll mid=(tree[rt].r+tree[rt].l)>>1;ll ans=(0x3f3f3f3f3f3f3f3fll);if(L<=mid)ans=min(ans,query_min(L,R,lson)); if(R>mid)ans=min(ans,query_min(L,R,rson));return ans;} il ll query_max(ll L, ll R, ll rt) {if(L<=tree[rt].l&&R>=tree[rt].r){return tree[rt].maxn;}push_down(rt,tree[rt].r-tree[rt].l+1); ll mid=(tree[rt].r+tree[rt].l)>>1;ll ans=-(0x3f3f3f3f3f3f3f3fll);if(L<=mid)ans=max(ans,query_max(L,R,lson)); if(R>mid)ans=max(ans,query_max(L,R,rson));return ans;} */ const int N = 1e3 + 10; ll a[N][N]; int main() { ios; ll r , c; cin >> r >> c; if(r == 1 && c == 1) return cout << 0 << '\n' , 0; if(r == 1) { int tot = 2; rep(i , 1 , c) cout << tot ++ << ' '; return 0; } if(c == 1) { int tot = 2; rep(i , 1 , r) cout << tot ++ << "\n"; return 0; } rep(i , 1 , r) { rep(j , 1 , c) { a[i][j] = i * (j + r); } } rep(i , 1 , r) { rep(j , 1 , c) cout << a[i][j] << " "; cout << '\n'; } return 0; }
題意:
給你幾個債務關係,如A欠B五元, B欠C五元,此時總的債務爲 5 + 5 = 10 。
咱們能夠把關係轉換爲A欠C五元,那這樣總的債務爲 5
問不限轉換次數 , 怎麼把總債務化爲最小。
分析:
當把總債務化爲最小時 , 每一個人的債務關係也將是最簡的(以上述例子闡述,對B , 它欠C五元,這時候它的支出爲5,可是A欠它五元,因此它的收入爲5,因此它最後對總債務的關係爲abs(5 - 5)= 0)
對於A、C也是同樣的,因此最後A的最簡形式爲A支出5元,B的最簡形式爲0,C的最簡形式爲收入5元。按照這樣,咱們只要將A指向C便可
(由於總的收入和總的支出必定是相同的且題目也說債務關係數量最小,因此即便某我的 D 收入的金額大於另外一我的 E 支出的金額,也能夠先將E連向D,而後D的收入金額減去E的支出金額)
咱們用 in 容器來存最簡形式爲收入的人的編號, ou容器來存最簡形式爲支出的人的編號,再用指針one指向in裏的元素,two指向ou裏的元素,他們的債務能夠表示爲min(a[one] , a[two]) , 將關係存於ans容器裏。
當one == in.size() || two == ou.size() 即已經沒有支出的人或已經沒有收入的人了,則債務關係能夠結束儲存(題目要求最後輸出債務關係)
#include<bits/stdc++.h> #define ios std::ios::sync_with_stdio(false) #define sd(n) scanf("%d",&n) #define sdd(n,m) scanf("%d%d",&n,&m) #define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k) #define pd(n) printf("%d\n", (n)) #define pdd(n,m) printf("%d %d\n", n, m) #define pld(n) printf("%lld\n", n) #define pldd(n,m) printf("%lld %lld\n", n, m) #define sld(n) scanf("%lld",&n) #define sldd(n,m) scanf("%lld%lld",&n,&m) #define slddd(n,m,k) scanf("%lld%lld%lld",&n,&m,&k) #define sf(n) scanf("%lf",&n) #define sff(n,m) scanf("%lf%lf",&n,&m) #define sfff(n,m,k) scanf("%lf%lf%lf",&n,&m,&k) #define rep(i,a,n) for (int i=a;i<=n;i++) #define per(i,n,a) for (int i=n;i>=a;i--) #define mm(a,n) memset(a, n, sizeof(a)) #define pb push_back #define all(x) (x).begin(),(x).end() #define fi first #define se second #define il inline #define ll long long #define lson rt << 1 #define rson rt << 1 | 1 #define MOD 1000000007 #define pi 3.14159265358979323 #define debug(x) cout <<#x<<": "<<x<<endl #define debug2(x, y) cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl; #define debug3(x, y, z) cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl; #define debug4(a, b, c, d) cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl; using namespace std; const ll INF (0x3f3f3f3f3f3f3f3fll); const int inf (0x3f3f3f3f); template<typename T>void read(T &res){bool flag=false;char ch;while(!isdigit(ch=getchar()))(ch=='-')&&(flag=true); for(res=ch-48;isdigit(ch=getchar());res=(res<<1)+(res<<3)+ch - 48);flag&&(res=-res);} template<typename T>void Out(T x){if(x<0)putchar('-'),x=-x;if(x>9)Out(x/10);putchar(x%10+'0');} ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} ll lcm(ll a,ll b){return a*b/gcd(a,b);} ll pow_mod(ll x,ll n,ll mod){ll res=1;while(n){if(n&1)res=res*x%mod;x=x*x%mod;n>>=1;}return res;} ll fact_pow(ll n,ll p){ll res=0;while(n){n/=p;res+=n;}return res;} ll mult(ll a,ll b,ll p){a%=p;b%=p;ll r=0,v=a;while(b){if(b&1){r+=v;if(r>p)r-=p;}v<<=1;if(v>p)v-=p;b>>=1;}return r;} ll quick_pow(ll a,ll b,ll p){ll r=1,v=a%p;while(b){if(b&1)r=mult(r,v,p);v=mult(v,v,p);b>>=1;}return r;} bool CH(ll a,ll n,ll x,ll t) {ll r=quick_pow(a,x,n);ll z=r;for(ll i=1;i<=t;i++){r=mult(r,r,n);if(r==1&&z!=1&&z!=n-1)return true;z=r;}return r!=1;} bool Miller_Rabin(ll n) {if(n<2)return false;if(n==2)return true;if(!(n&1))return false;ll x=n-1,t=0;while(!(x&1)){x>>=1;t++;}srand(time(NULL)); ll o=8;for(ll i=0;i<o;i++){ll a=rand()%(n-1)+1;if(CH(a,n,x,t))return false;}return true;} /* int prime[30000010],minprime[30000010]; void euler(int n) {int c=0,i,j;for(i=2;i<=n;i++){if(!minprime[i])prime[++c]=i,minprime[i]=i;for(j=1;j<=c&&i*prime[j]<=n;j++) {minprime[i*prime[j]]=prime[j];if(i%prime[j]==0)break;}}} struct Tree{ll l,r,sum,lazy,maxn,minn;}tree[1000000]; il void push_up(ll rt) {tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum; tree[rt].maxn=max(tree[rt<<1].maxn,tree[rt<<1|1].maxn); tree[rt].minn=min(tree[rt<<1].minn,tree[rt<<1|1].minn);} il void push_down(ll rt , ll now) {if(tree[rt].lazy){tree[rt<<1].lazy+=tree[rt].lazy;tree[rt<<1|1].lazy+=tree[rt].lazy; tree[rt<<1].sum+=(now-(now>>1))*tree[rt].lazy;tree[rt<<1|1].sum+=(now>>1)*tree[rt].lazy; tree[rt<<1].minn+=tree[rt].lazy;tree[rt<<1|1].minn+=tree[rt].lazy; tree[rt<<1].maxn+=tree[rt].lazy;tree[rt<<1|1].maxn+=tree[rt].lazy;tree[rt].lazy=0;}} il void build(ll l , ll r , ll rt , ll *aa) {tree[rt].lazy=0;tree[rt].l=l;tree[rt].r=r;if(l==r) {tree[rt].sum=aa[l];tree[rt].minn=tree[rt].sum;tree[rt].maxn=tree[rt].sum;return;} ll mid=(l+r)>>1;build(l,mid,rt<<1,aa);build(mid+1,r,rt<<1|1,aa);push_up(rt);} il void update_range(ll L , ll R , ll key , ll rt) {if(L<=tree[rt].l&&R>=tree[rt].r){tree[rt].sum+=(tree[rt].r-tree[rt].l+1)*key; tree[rt].minn+=key;tree[rt].maxn+=key;tree[rt].lazy+=key;return;}push_down(rt,tree[rt].r-tree[rt].l+1); ll mid=(tree[rt].r+tree[rt].l)>>1;if(L<=mid)update_range(L,R,key,lson); if(R>mid)update_range(L,R,key,rson);push_up(rt);} il ll query_range(ll L, ll R, ll rt) {if(L<=tree[rt].l&&R>=tree[rt].r){return tree[rt].sum;}push_down(rt,tree[rt].r-tree[rt].l+1); ll mid=(tree[rt].r+tree[rt].l)>>1;ll ans=0;if(L<=mid)ans+=query_range(L,R,lson); if(R>mid)ans+=query_range(L,R,rson);return ans;} il ll query_min(ll L,ll R,ll rt) {if(L<=tree[rt].l&&R>=tree[rt].r){return tree[rt].minn;}push_down(rt,tree[rt].r-tree[rt].l+1); ll mid=(tree[rt].r+tree[rt].l)>>1;ll ans=(0x3f3f3f3f3f3f3f3fll);if(L<=mid)ans=min(ans,query_min(L,R,lson)); if(R>mid)ans=min(ans,query_min(L,R,rson));return ans;} il ll query_max(ll L, ll R, ll rt) {if(L<=tree[rt].l&&R>=tree[rt].r){return tree[rt].maxn;}push_down(rt,tree[rt].r-tree[rt].l+1); ll mid=(tree[rt].r+tree[rt].l)>>1;ll ans=-(0x3f3f3f3f3f3f3f3fll);if(L<=mid)ans=max(ans,query_max(L,R,lson)); if(R>mid)ans=max(ans,query_max(L,R,rson));return ans;} */ const int N = 3e5 + 10; ll a[N]; vector<ll>in , ou; vector<pair<pair<ll , ll>, ll> > ans; int main() { ios; ll n , m ; cin >> n >> m; rep(i , 1 , m) { ll x , y , z; cin >> x >> y >> z; a[x] += z; a[y] -= z; } rep(i , 1 , n) { if(a[i] > 0) in.pb(i); else if(a[i] < 0) ou.pb(i); } ll one = 0 , two = 0; while(true) { if(one == in.size() || two == ou.size()) break; ll ha = min(a[in[one]] , -a[ou[two]]); ans.pb(make_pair(make_pair(in[one] , ou[two]) , ha)); a[in[one]] -= ha; a[ou[two]] += ha; if(!a[in[one]]) one ++; if(!a[ou[two]]) two ++; } ll len = ans.size(); cout << len << '\n'; rep(i , 0 , len - 1) cout << ans[i].fi.fi << " " << ans[i].fi.se << " " << ans[i].se << "\n"; return 0; }
題意:
你要建個飛船 , 建飛船須要 n 種資源 , 每種資源的需求量爲 a[i] 。每一回合你只能選擇一種資源並生產一個。而後你有里程牌???
里程碑包含三個元素 S 、 T 、 U , 意思是若是你有 T 個 S 資源, 你就能夠得到一個U資源。(0 < T < a[S])
不一樣的里程碑 S 和 T 不相同 (若是S 相同且 T 相同則它們是同一個里程碑)
給你 q 次詢問, 每次詢問會加入一個里程碑(若是該里程碑先前存在過 , 則用如今的里程碑代替以前的里程碑 , 若是U == 0 , 則銷燬該里程碑)
問建造飛船最少須要多少回合
分析:
i 種資源 每種須要 a[i] 個,那麼當沒有里程碑的狀況下總的回合數爲 a[1] + a[2] + ... + a[n] , 記爲sum
首先咱們要知道的是,不論如何,最後飛船是確定能夠建成的。又由於對任意里程碑 T < a[S] , 因此里程碑起做用的條件是確定能夠知足的 ,那麼對於當前詢問加入的里程碑來講
若是它先前出現過,則咱們判斷它先前出現的時候是否會對sum起影響(由於可能在該里程碑起做用以前,資源U已經採集的夠了,因此該里程碑不起到做用。若是在起做用前U採集還不夠,則它起到做用了, sum 會減一)
若是不影響,sum保持不變,影響了,則sum++(把先前形成的影響去掉)
而後咱們判斷新的里程碑是否起做用(若是起做用sum -- , 不然sum不變),對於每一個詢問輸出sum便可。(只需判斷當前類型里程碑,其餘里程碑不受影響)
簡單模擬題。
#include<bits/stdc++.h> #define ios std::ios::sync_with_stdio(false) #define sd(n) scanf("%d",&n) #define sdd(n,m) scanf("%d%d",&n,&m) #define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k) #define pd(n) printf("%d\n", (n)) #define pdd(n,m) printf("%d %d\n", n, m) #define pld(n) printf("%lld\n", n) #define pldd(n,m) printf("%lld %lld\n", n, m) #define sld(n) scanf("%lld",&n) #define sldd(n,m) scanf("%lld%lld",&n,&m) #define slddd(n,m,k) scanf("%lld%lld%lld",&n,&m,&k) #define sf(n) scanf("%lf",&n) #define sff(n,m) scanf("%lf%lf",&n,&m) #define sfff(n,m,k) scanf("%lf%lf%lf",&n,&m,&k) #define rep(i,a,n) for (int i=a;i<=n;i++) #define per(i,n,a) for (int i=n;i>=a;i--) #define mm(a,n) memset(a, n, sizeof(a)) #define pb push_back #define all(x) (x).begin(),(x).end() #define fi first #define se second #define il inline #define ll long long #define lson rt << 1 #define rson rt << 1 | 1 #define MOD 1000000007 #define pi 3.14159265358979323 #define debug(x) cout <<#x<<": "<<x<<endl #define debug2(x, y) cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl; #define debug3(x, y, z) cout <<#x<<": "<<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl; #define debug4(a, b, c, d) cout <<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl; using namespace std; const ll INF (0x3f3f3f3f3f3f3f3fll); const int inf (0x3f3f3f3f); template<typename T>void read(T &res){bool flag=false;char ch;while(!isdigit(ch=getchar()))(ch=='-')&&(flag=true); for(res=ch-48;isdigit(ch=getchar());res=(res<<1)+(res<<3)+ch - 48);flag&&(res=-res);} template<typename T>void Out(T x){if(x<0)putchar('-'),x=-x;if(x>9)Out(x/10);putchar(x%10+'0');} ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} ll lcm(ll a,ll b){return a*b/gcd(a,b);} ll pow_mod(ll x,ll n,ll mod){ll res=1;while(n){if(n&1)res=res*x%mod;x=x*x%mod;n>>=1;}return res;} ll fact_pow(ll n,ll p){ll res=0;while(n){n/=p;res+=n;}return res;} ll mult(ll a,ll b,ll p){a%=p;b%=p;ll r=0,v=a;while(b){if(b&1){r+=v;if(r>p)r-=p;}v<<=1;if(v>p)v-=p;b>>=1;}return r;} ll quick_pow(ll a,ll b,ll p){ll r=1,v=a%p;while(b){if(b&1)r=mult(r,v,p);v=mult(v,v,p);b>>=1;}return r;} bool CH(ll a,ll n,ll x,ll t) {ll r=quick_pow(a,x,n);ll z=r;for(ll i=1;i<=t;i++){r=mult(r,r,n);if(r==1&&z!=1&&z!=n-1)return true;z=r;}return r!=1;} bool Miller_Rabin(ll n) {if(n<2)return false;if(n==2)return true;if(!(n&1))return false;ll x=n-1,t=0;while(!(x&1)){x>>=1;t++;}srand(time(NULL)); ll o=8;for(ll i=0;i<o;i++){ll a=rand()%(n-1)+1;if(CH(a,n,x,t))return false;}return true;} /* int prime[30000010],minprime[30000010]; void euler(int n) {int c=0,i,j;for(i=2;i<=n;i++){if(!minprime[i])prime[++c]=i,minprime[i]=i;for(j=1;j<=c&&i*prime[j]<=n;j++) {minprime[i*prime[j]]=prime[j];if(i%prime[j]==0)break;}}} struct Tree{ll l,r,sum,lazy,maxn,minn;}tree[1000000]; il void push_up(ll rt) {tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum; tree[rt].maxn=max(tree[rt<<1].maxn,tree[rt<<1|1].maxn); tree[rt].minn=min(tree[rt<<1].minn,tree[rt<<1|1].minn);} il void push_down(ll rt , ll now) {if(tree[rt].lazy){tree[rt<<1].lazy+=tree[rt].lazy;tree[rt<<1|1].lazy+=tree[rt].lazy; tree[rt<<1].sum+=(now-(now>>1))*tree[rt].lazy;tree[rt<<1|1].sum+=(now>>1)*tree[rt].lazy; tree[rt<<1].minn+=tree[rt].lazy;tree[rt<<1|1].minn+=tree[rt].lazy; tree[rt<<1].maxn+=tree[rt].lazy;tree[rt<<1|1].maxn+=tree[rt].lazy;tree[rt].lazy=0;}} il void build(ll l , ll r , ll rt , ll *aa) {tree[rt].lazy=0;tree[rt].l=l;tree[rt].r=r;if(l==r) {tree[rt].sum=aa[l];tree[rt].minn=tree[rt].sum;tree[rt].maxn=tree[rt].sum;return;} ll mid=(l+r)>>1;build(l,mid,rt<<1,aa);build(mid+1,r,rt<<1|1,aa);push_up(rt);} il void update_range(ll L , ll R , ll key , ll rt) {if(L<=tree[rt].l&&R>=tree[rt].r){tree[rt].sum+=(tree[rt].r-tree[rt].l+1)*key; tree[rt].minn+=key;tree[rt].maxn+=key;tree[rt].lazy+=key;return;}push_down(rt,tree[rt].r-tree[rt].l+1); ll mid=(tree[rt].r+tree[rt].l)>>1;if(L<=mid)update_range(L,R,key,lson); if(R>mid)update_range(L,R,key,rson);push_up(rt);} il ll query_range(ll L, ll R, ll rt) {if(L<=tree[rt].l&&R>=tree[rt].r){return tree[rt].sum;}push_down(rt,tree[rt].r-tree[rt].l+1); ll mid=(tree[rt].r+tree[rt].l)>>1;ll ans=0;if(L<=mid)ans+=query_range(L,R,lson); if(R>mid)ans+=query_range(L,R,rson);return ans;} il ll query_min(ll L,ll R,ll rt) {if(L<=tree[rt].l&&R>=tree[rt].r){return tree[rt].minn;}push_down(rt,tree[rt].r-tree[rt].l+1); ll mid=(tree[rt].r+tree[rt].l)>>1;ll ans=(0x3f3f3f3f3f3f3f3fll);if(L<=mid)ans=min(ans,query_min(L,R,lson)); if(R>mid)ans=min(ans,query_min(L,R,rson));return ans;} il ll query_max(ll L, ll R, ll rt) {if(L<=tree[rt].l&&R>=tree[rt].r){return tree[rt].maxn;}push_down(rt,tree[rt].r-tree[rt].l+1); ll mid=(tree[rt].r+tree[rt].l)>>1;ll ans=-(0x3f3f3f3f3f3f3f3fll);if(L<=mid)ans=max(ans,query_max(L,R,lson)); if(R>mid)ans=max(ans,query_max(L,R,rson));return ans;} */ const int N = 2e5 + 10; ll a[N] , sum; map<ll , ll> vis[N]; int main() { ios;cin.tie(0); int n , q; cin >> n ; rep(i , 1 , n) cin >> a[i] , sum += a[i]; cin >> q; rep(i , 1 , q) { ll s , t , u; cin >> s >> t >> u; if(vis[s][t]) { a[vis[s][t]] ++; if(a[vis[s][t]] > 0) sum ++; vis[s][t] = 0; } if(u) { vis[s][t] = u; a[u] --; if(a[u] >= 0) sum --; } cout << sum << '\n'; } return 0; }