Too Rich
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
#include<iomanip>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<sstream>
using namespace std ;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %I64d\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
For(j,m-1 ) cout <<a[i][j]<<' ' ;\
cout <<a[i][m]<<endl; \
}
#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
int x=0 ,f=1 ; char ch=getchar();
while (!isdigit (ch)) {if (ch=='-' ) f=-1 ; ch=getchar();}
while (isdigit (ch)) { x=x*10 +ch-'0' ; ch=getchar();}
return x*f;
}
#define MAXN (10)
ll p,tot;
ll c[MAXN]={1 ,5 ,10 ,20 ,50 ,100 ,200 ,500 ,1000 ,2000 };
ll d[MAXN];
ll d2[MAXN];
bool b[MAXN]={0 ,1 ,0 ,0 ,1 ,0 ,0 ,1 ,0 ,0 };
ll work(int st,ll p,ll &ans)
{
memcpy (d2,d,sizeof (d));
ll t=p,an=0 ;
Rep(i,10 ) {
if ((1 <<i)&st) {
if (d[i] && t>=c[i]) {
t-=c[i]; d[i]--;
an++;
}
}
}
RepD(j,9 ) if (b[j]){
ll cost=c[j]*2 ;
ll ad=min(t/cost,d[j]/2 );
an+=ad*2 ;
t-=cost*ad;
} else {
ll cost=c[j];
ll ad=min(t/cost,d[j]);
an+=ad;
t-=cost*ad;
}
memcpy (d,d2,sizeof (d));
if (!t) ans=min(ans,an);
}
int main()
{
int T=read();
while (T--) {
p=read(); tot=0 ;
Rep(i,10 ) d[i]=read(),tot+=d[i]*c[i];
if (tot<p) {
puts ("-1" ); continue ;
} else p=tot-p;
int S=1 <<10 ;
ll ans=INF;
for (int i=0 ;i<=2 ;i+=2 ) {
for (int j=0 ;j<=16 ;j+=16 )
for (int k=0 ;k<=128 ;k+=128 ) {
work(i+j+k,p,ans);
}
}
int cnt=0 ;
Rep(i,10 ) cnt+=d[i];
if (ans==INF) {
puts ("-1" );
} else cout <<cnt-ans<<endl;
}
return 0 ;
}
Count a * b
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
#include<iomanip>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<sstream>
using namespace std ;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %I64d\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
For(j,m-1 ) cout <<a[i][j]<<' ' ;\
cout <<a[i][m]<<endl; \
}
#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
int x=0 ,f=1 ; char ch=getchar();
while (!isdigit (ch)) {if (ch=='-' ) f=-1 ; ch=getchar();}
while (isdigit (ch)) { x=x*10 +ch-'0' ; ch=getchar();}
return x*f;
}
#define MAXN (100000+10)
int p[MAXN],tot;
bool b[MAXN]={0 };
void make_prime(int n)
{
tot=0 ;
Fork(i,2 ,n)
{
if (!b[i]) p[++tot]=i;
For(j,tot)
{
if (i*p[j]>n) break ;
b[i*p[j]]=1 ;
if (i%p[j]==0 ) break ;
}
}
}
ull a[MAXN],d[MAXN],n2;
void work(int sz) {
ull ans1=1 ;
ull ans2=n2;
For(i,sz) ans2*=d[i]+1 ;
For(i,sz) {
ull t=1 ,p=1 ;
For(j,d[i]) {
p*=(ull)a[i]*a[i];
t+=p;
}
ans1*=t;
}
printf ("%I64u\n" ,ans1-ans2);
}
int main()
{
int T=read();
make_prime(sqrt (1e9 ));
while (T--) {
ll n=read();
n2=n;
int sz=0 ;
For(i,tot) {
if (p[i]>n) break ;
if (n%p[i]==0 ) {
a[++sz]=p[i]; d[sz]=0 ;
while (n%p[i]==0 ) n/=p[i],d[sz]++;
}
}
if (n>1 ) a[++sz]=n,d[sz]=1 ;
work(sz);
}
return 0 ;
}
Dancing Stars on Me
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
#include<iomanip>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<sstream>
using namespace std ;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %I64d\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
For(j,m-1 ) cout <<a[i][j]<<' ' ;\
cout <<a[i][m]<<endl; \
}
#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
int x=0 ,f=1 ; char ch=getchar();
while (!isdigit (ch)) {if (ch=='-' ) f=-1 ; ch=getchar();}
while (isdigit (ch)) { x=x*10 +ch-'0' ; ch=getchar();}
return x*f;
}
ll sqr(ll a){return a*a;}
ld sqr(ld a){return a*a;}
double sqr(double a){return a*a;}
class P{
public :
double x,y;
P(double x=0 ,double y=0 ):x(x),y(y){}
friend long double dis2(P A,P B){return sqr(A.x-B.x)+sqr(A.y-B.y); }
friend long double Dot(P A,P B) {return A.x*B.x+A.y*B.y; }
friend long double Length(P A) {return sqrt (Dot(A,A)); }
friend long double Angle(P A,P B) {return acos (Dot(A,B) / Length(A) / Length(B) ); }
friend P operator - (P A,P B) { return P(A.x-B.x,A.y-B.y); }
friend P operator + (P A,P B) { return P(A.x+B.x,A.y+B.y); }
friend P operator * (P A,double p) { return P(A.x*p,A.y*p); }
friend P operator / (P A,double p) { return P(A.x/p,A.y/p); }
friend bool operator < (const P& a,const P& b) {return a.x<b.x||(a.x==b.x&& a.y<b.y);}
};
const double eps=1e-10 ;
int dcmp(double x) {
if (fabs (x)<eps) return 0 ; else return x<0 ? -1 : 1 ;
}
bool operator ==(const P& a,const P& b) {
return dcmp(a.x-b.x)==0 && dcmp(a.y-b.y) == 0 ;
}
typedef P V;
double Cross(V A,V B) {return A.x*B.y - A.y*B.x;}
bool SegmentProperIntersection(P a1,P a2,P b1,P b2) {
double c1 = Cross(a2-a1,b1-a1) , c2 = Cross(a2-a1,b2-a1),
c3 = Cross(b2-b1,a1-b1) , c4 = Cross(b2-b1,a2-b1);
return dcmp(c1)*dcmp(c2)<0 && dcmp(c3)*dcmp(c4)<0 ;
}
bool ConvexPolygon(P &A,P &B,P &C,P &D) {
if (SegmentProperIntersection(A,C,B,D)) return 1 ;
swap(B,C);
if (SegmentProperIntersection(A,C,B,D)) return 1 ;
swap(D,C);
if (SegmentProperIntersection(A,C,B,D)) return 1 ;
return 0 ;
}
bool IsParallel(P A,P B,P C,P D) {
return dcmp(Cross(B-A,D-C))==0 ;
}
bool IsPerpendicular(V A,V B) {
return dcmp(Dot(A,B))==0 ;
}
bool IsTrapezium(P A,P B,P C,P D){
return IsParallel(A,B,C,D)^IsParallel(B,C,A,D);
}
bool IsParallelogram(P A,P B,P C,P D) {
return IsParallel(A,B,C,D)&&IsParallel(B,C,A,D);
}
bool IsSquare(P A,P B,P C,P D) {
return IsParallelogram(A,B,C,D)&&IsPerpendicular(B-A,D-A)&&dcmp(Length(B-A)-Length(C-B))==0 ;
}
P A,B,C,D;
P read_point() {
P a;
scanf ("%lf%lf" ,&a.x,&a.y);
return a;
}
int main()
{
int T=read();
while (T--) {
int n=read();
if (n!=4 ) {
For(i,n) A=read_point();
puts ("NO" );
} else {
A=read_point();
B=read_point();
C=read_point();
D=read_point();
bool fl=ConvexPolygon(A,B,C,D);
if (fl) fl=IsSquare(A,B,C,D);
if (fl) puts ("YES" ); else puts ("NO" );
}
}
return 0 ;
}
House Building
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
#include<iomanip>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<sstream>
using namespace std ;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=Pre[x];p;p=Next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=Next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %I64d\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) { \
For(j,m-1 ) cout <<a[i][j]<<' ' ;\
cout <<a[i][m]<<endl; \
}
#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
int x=0 ,f=1 ; char ch=getchar();
while (!isdigit (ch)) {if (ch=='-' ) f=-1 ; ch=getchar();}
while (isdigit (ch)) { x=x*10 +ch-'0' ; ch=getchar();}
return x*f;
}
int a[100 ][100 ];
int main()
{
int T=read();
while (T--) {
int n=read(),m=read();
MEM(a)
For(i,n) For(j,m) scanf ("%d" ,&a[i][j]);
ll t=0 ;
For(i,n) For(j,m) {
int k=a[i][j];
if (a[i][j-1 ]<k) t+=k-a[i][j-1 ];
if (a[i][j+1 ]<k) t+=k-a[i][j+1 ];
if (a[i-1 ][j]<k) t+=k-a[i-1 ][j];
if (a[i+1 ][j]<k) t+=k-a[i+1 ][j];
if (k) t++;
}
cout <<t<<endl;
}
return 0 ;
}