intmain(){ longlong res = 0; int n =2021; for(int a = 1;a<=500;a++){ int an = n-a; for(int b = a;b<=1000;b++){ int bn = an -b; for(int c = b;c<=1500;c++){ int cn = bn -c ; for(int d = c;d<=cn;d++){ int e = cn - d; int t = 0; if(e >= d){ //五个相等 if(a == e) t =1; //四个相等 elseif((a==d) || (b==e)) t =5; //三个相等 elseif((a==c) || (b==d) || (c==e)) t= 20; //两对相等 elseif((a==b && c==d) || (a==b && d==e) || (b==c && d ==e)) t= 30; //一对相等 elseif(a==b || b==c || c==d || d==e) t = 60; //全不相等 else t = 120; }
res += t; } } } } cout<< res <<endl; return0; } //691677277715
解法二-转换思路的三重循环:
分析:三重循环枚举三个数,差是m,和为m的两个正整数可能是m-1种
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include<bits/stdc++.h> usingnamespace std;
intmain() { ll res = 0; int n = 2021; for(int a = 1;a<=n;a++) for(int b = 1;b<=n;b++) for(int c = 1;c<=n;c++) { int d = n -a -b -c; if(d <= 1) break; res += d-1; } cout<< res <<endl; return0; } //691677274345
int res = 0; for(int i=0;i<5;i++){ int q = a[i] /1000; int b = a[i] %1000 / 100; int s = a[i] %100 / 10; int g = a[i] %10; if(q == s && g == b +1) { res ++; } } cout<< res; return0; }
G: 小平方
分析:暴力枚举,数据规模小(1 ≤ n ≤ 10000)
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13
#include<bits/stdc++.h> usingnamespace std;
intmain(){ int n; cin >> n; int res = 0; for(int i =1;i<n;i++){ if( (i*i%n) < (n /2.0)) res++; } cout<< res; return0; }
#define maxn 200010 int n, m; int arr[maxn]; int a, b, c, d; structNode { int endTime, id , val; booloperator< (const Node &node)const { return endTime > node.endTime; } };
priority_queue<Node> q; //释放算力 voidgetd(){ while(!q.empty()){ Node t= q.top(); if(t.endTime > a) break; q.pop(); int i =t.id, v = t.val; arr[i] += v; } } //消耗算力 voidtask(){ getd(); if(arr[b] >= d){ q.push({a+c, b ,d});//算力排队 arr[b] -= d; cout<< arr[b]; }else{ cout<<"-1"; } } intmain(){ cin>>n>>m; memset(arr, 0 , sizeof arr); for(int i =1;i<=n;i++) cin>>arr[i]; while(m--){ cin>> a>> b >>c>>d; task(); if(m>0) cout<<endl; } return0; }