1213C - Book Reading - CodeForces Solution


math *1200

Please click on ads to support us..

Python Code:

q=int(input())
for _ in range(q):
    n,m=map(int,input().split())
    x=n//m
    y=x%10
    if (m%10)==0:
        print(0)
        continue
    d=m%10
    if d==5:
        tot=(x//2)*5
        if x%2==1:
            tot+=5
        print(tot)
        continue
    if d%2==0:
        tot=(x//10)*40
        for i in range(y):
            tot+=(m*(i+1))%10
    elif d%2==1:
        tot=(x//10)*45
        for i in range(y):
            tot+=(m*(i+1))%10
    print(tot)

C++ Code:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll N = 1e5 + 7;
const ll M = 1e7 + 7;
ll arr[N];
ll P[N];
#define all(v) sort(v.begin(),v.end())
#define rall(v) sort(v.rbegin(),v.rend())
#define ar(a) a.begin(),a.end()
#define bs binary_search
#define lb lower_bound
#define ub upper_bound
#define rev(a) reverse(a.begin(),a.end())
#define f0(i,a,n) for(int i=a;i<n;i++)
#define f1(i,b,a) for(int i=b;i>=a;i--)
#define bbl(a) __builtin_popcountll(a)
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define aaja(v)         \
    for (auto &x : v) \
        cin >> x;
#define cout1(v)             \
    for (auto &x : v)      \
    {                      \
        cout << x << endl; \
    }                      \
    nxt;
#define zout1(v)                                     \
    for (auto &x : v)                               \
    {                                               \
        cout << x.first << " " << x.second << endl; \
    }       

// bool sorting(const pair<ll, ll> &a, const pair<ll, ll> &b)
// {
//    if (a.first>b.first)
//    {
//        return (a.second <= b.second);
//    }
   
  
// }
bool isprime(int n){ if(n==1) return false;if(n==2) return true;
for(int i=2;i*i<=n;i++){if(n%i==0) return false;}return true;}
void xorprop(ll n){if (n % 4 == 1){cout<<1<<endl;}else if (n % 4 == 2){ cout<<n+1<<endl;}if (n % 4 == 3){cout<<0<<endl;}if (n % 4 == 0){cout<<n<<endl;}}
//ll kthNonDivisible(ll N, ll K){   ll L = 1;ll H = LLONG_MAX;ll ans = 0;while (L <= H){ll mid = (L + H) / 2;ll sol = mid - mid / N;if (sol > K){H = mid - 1;}else if (sol < K){ L = mid + 1;}else{ans = mid;H = mid - 1;}}return ans;}
void printbinary(int n){for (int i = 6; i >=0; i--){cout<<((n>>i)&1)<<" ";}cout<<endl;} 
bool isSubstring(string s1, string s2){if (s2.find(s1) != string::npos) return true;return false;}
ll ok(ll n)
    {
        ll sum = 0;
        while (n != 0) {
            sum = sum + n % 10;
            n = n / 10;
        }
        return sum;
    }
void solve(){
    ll n, m;
    cin>>n>>m;
    ll ans = 10*m;
    ll ans1=0;
    f0(i, 1 ,10){
        ll p = m*i;
        ans1+=(p%10);
    }
    ans1=(n/(m*10))*ans1;

    ll ans2 = n%(m*10);
     f0(i, 1 ,10){
        ll p = m*i;
        if (p>ans2)
        {
            break;
        }
        
        ans1+=(p%10);
    }
    cout<<ans1<<endl;

    
}
int32_t main()
{
   fast;
    ll test;
    cin>>test;
    while (test--)
    {
       solve();
    }

   return 0;
}


Comments

Submit
0 Comments
More Questions

1302. Deepest Leaves Sum
1209. Remove All Adjacent Duplicates in String II
994. Rotting Oranges
983. Minimum Cost For Tickets
973. K Closest Points to Origin
969. Pancake Sorting
967. Numbers With Same Consecutive Differences
957. Prison Cells After N Days
946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST