constint N = 110, INF = 0x3f3f3f3f; int a[N][N]; intmain(){ int n, m; cin>>n>>m; int k = INF; for(int i=0;i<n;i++) for(int j=0;j<m;j++) { scanf("%d", &a[i][j]); k = min(k, a[i][j]); } int res = 0; for(int i=0;i<n;i++) for(int j=0;j<m;j++) res += a[i][j] - k; cout<<res<<endl; return0; }
C - Unlucky 7 Number of digits in octal and decimal format from 1 to N.
intexgcd(int a, int b, ll &x, ll &y){ if(!b){ x = 1, y = 0; return a; } int d= exgcd(b, a % b, y, x); y -= a/b * (ll)x; return d; }
intmain(){ int t; cin>>t;
while (t--) { ll n,s,k; cin>>n>>s>>k; ll x, y; s = n-s; int g=exgcd(k, n, x, y); if (s%g!=0) { cout<<"-1\n"; continue; }
x = (ll)x * (s/g); x = (x%(n/g) + n/g) % (n/g); cout<<x<<endl; }
return0; }
F - Rook on Grid Given a 2-d grid and some obstacles. Print the number of cells that can be reached with the following two moves.
Move down followed by move right;
Move right followed by move down;
Idea: First add all the cells that can be reached with approach 1. Second add remaining cells that can only be reached with approach 2. Use sweep line + bit tree to get O(NlogN) complexity. For each line, consider the first obstacle. Cells left of this obstacle can use approach 1. For cells right of this obstacle, it may be reached with approach 2. tr[i] is the number of rows from 1~ith row, that has an obstacle on the left of the current enumerating column.